Skip to content

Instantly share code, notes, and snippets.

@m-zakeri
Last active December 21, 2019 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-zakeri/5fd1ca7e86010ed76c4304400be6e175 to your computer and use it in GitHub Desktop.
Save m-zakeri/5fd1ca7e86010ed76c4304400be6e175 to your computer and use it in GitHub Desktop.
Some C++ code snips and toy examples used for teaching basic programming.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!";
}
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
void read(int A[],const int k)
{
int i;
for(i=0;i<k;i++){
cout << "A["<<i<<"] = ";
cin >> A[i];
}
}
void main()
{
int i,max;
const int k = 5;
int A[k];
read(A,k);
max = A[0];
for (i=0;i<k;i++)
if(max < A[i])
max = A[i];
cout << "\n\n ArrayMax = " << max<<endl;
_getch();
}
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int maxF(int A[], const int k , int i, int max);
void read(int A[], const int k)
{
int i;
for(i=0;i<k;i++){
cout << "A["<<i<<"] = ";
cin >> A[i];
}
}
void main()
{
int i = 0,max;
const int k = 5;
int A[k];
read(A,k);
max = A[i];
cout << "\n\n ArrayMax = " << maxF(A,k,i,max)<< endl;
_getch();
}
int maxF(int A[], const int k , int i, int max)
{
if(k == i)
return max;
else if( max<A[i])
return ( maxF( A,k,i+1,A[i] ) );
else
return ( maxF( A,k,i+1,max ) );
}
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
double a=1;
double b=2;
double c=1.5;
double y;
while(abs(c*c-2) > 0.0000001)
{
c = (a+b)/2;
y = c*c-2;
if(y*(a*a-2)<0)
b = c;
else if(y*(b*b-2)<0)
a=c;
//else if(y==0)
// cout << c;
}
cout << c;
_getch();
return 0;
}
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <limits>
#ifdef _MSC_VER
#undef max
#endif
using namespace std;
void main()
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int color = 0, i;
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | 0x0030);
cout << "Blue text with green background" << endl;
for (i = 0; i < 16; i++)
{
SetConsoleTextAttribute(hConsole, color);
cout << "Hello C++" << endl;
color++;
}
cout << "Press Enter to continue..." << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//system("pause");
//_getche();
}
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <dos.h>
#include <limits>
#ifdef _MSC_VER
#undef max
#endif
using namespace std;
char far *v = (char far *)0xb8000000;
void printc(char c,int row,int colum,int color){
* (v+((160*row)+(2*colum))) = c;
* (v+((160*row)+((2*colum)+1))) = color;
}
void main()
{
int color = 0, i,j;
//v = new char far [100];
//printc('a',1,1,10);
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | 1000);
cout << "Blue text with green background" << endl;
cout << "Hi ok kfhekfffd\n\n\n\n\n";
for (i = 0,j = 1 ; i < 25; i++, j=3*j)
{
SetConsoleTextAttribute(hConsole, color |j);
cout << "Hello C++" << endl;
color++;
}
cout << "Press Enter to continue..." << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
//system("pause");
//_getche();
}
/*Zarb*/
//******************************** H E A D E R F I L E ********************************
#include <iostream>
#include <conio.h>
using namespace std;
//******************************** F U N C T I O N ********************************
int mul(int *num1,int *num2,int *num3,int,int);
void calc(char[],char[]);
//******************************** M I A N ********************************
int main()
{
char cnum1[500],cnum2[500];
cout<<endl;
cout<<" Enter num1 : ";
cin>>cnum1;
cout<<endl;
cout<<" Enter num2 : ";
cin>>cnum2;
calc(cnum1,cnum2);
getch();
return 0;
}
//******************************** C A L C ********************************
void calc(char cnum1[],char cnum2[])
{
int i,x,limit,len1,len2,len3;
len1=strlen(cnum1);
len2=strlen(cnum2);
int *num1,*num2,*num3;
num1=(int *)calloc(len1,sizeof(int));
num2=(int *)calloc(len2,sizeof(int));
len3=(len1>len2)? len1:len2;
len3=len3*2;
num3=(int *)calloc(len3,sizeof(int));
for(x=0;x<=strlen(cnum1);x++)
num1[x]=char(cnum1[x])-48;
for(x=0;x<=strlen(cnum2);x++)
num2[x]=char(cnum2[x])-48;
len3=mul(num1,num2,num3,len1,len2);
limit=len3;
for(i=len3;i>=0;i--)
if(num3[i]==0)
limit-=1;
else
break;
cout<<endl<<" Result is : ";
for(i=limit;i>=0;i--)
cout<<num3[i];
getch();
}
//******************************** M U L ********************************
int mul(int *num1,int *num2,int *num3,int len1,int len2)
{
int counter1,counter2,counter3,likenum3,sumnum3,Q,set;
counter3=0;
set=-1;
sumnum3=0;
for(counter1=len1;counter1>=1;counter1--)
{
set++;
counter3=set;
Q=0;
sumnum3=0;
for(counter2=len2;counter2>=1;counter2--)
{
likenum3=num2[counter2-1]*num1[counter1-1]+Q;
Q=likenum3/10;
num3[counter3]+=likenum3%10+sumnum3;
if(sumnum3!=0)
sumnum3=0;
if(num3[counter3]>=10)
{
sumnum3=num3[counter3]/10;
num3[counter3]=num3[counter3]%10;
}
counter3++;
}
num3[counter3]=Q+sumnum3;
}
return counter3;
getch();
}
//******************************** E N D O F P R O G R A M M ********************************
#include <stdio.h>
#include <conio.h>
int nza(int a[][10],int);
int main()
{
const int r=10,c=10;
int a[r][c];
nza(a,r);
getch();
return 0;
}//end of main()
int nza(int a[][10],int r)
{
int m=0,n=0,k=0,p=0,t=0,i=0,j=0,mix=0,flag;
printf("Enter number of rows:");
scanf("%d",&n);
printf("Enter number of columns:");
scanf("%d",&m);
for(i=0;i<n;i++){
printf("Enter row %d : ",i);
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);}//end of for()
for(k=0;k<n;k++){
mix=a[k][t];flag=1;
for(j=1;j<m;j++)
if((a[k][j]<mix) && (a[k][j]!=0)){
mix=a[k][j];
t=j;p=k;}//end of if()
for(i=0;i<n;i++){
if(i!=p)
if(a[i][t]>mix){
flag=0;t=0;break;}
if(i==n-1 && flag==1)
printf("Noghteye Zin Asbi = %d",mix);
}//end of for()
}//end of for()
if(flag==0)
return -1;
}//end of nza()
// C++ program to illustrate Saddle point
/*
* Saddle point in a matrix:
* Given a matrix of n x n size, the task is to find saddle point of the matrix.
* A saddle point is an element of the matrix such that it is the minimum element in its row and maximum in its column.
*/
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
// Function to find saddle point
bool findSaddlePoint(int mat[MAX][MAX], int n)
{
// Process all rows one by one
for (int i = 0; i < n; i++)
{
// Find the minimum element of row i.
// Also find column index of the minimum element
int min_row = mat[i][0], col_ind = 0;
for (int j = 1; j < n; j++)
{
if (min_row > mat[i][j])
{
min_row = mat[i][j];
col_ind = j;
}
}
// Check if the minimum element of row is also
// the maximum element of column or not
int k;
for (k = 0; k < n; k++)
// Note that col_ind is fixed
if (min_row < mat[k][col_ind])
break;
// If saddle point is present in this row then
// print it
if (k == n)
{
cout << "Value of Saddle Point " << min_row;
return true;
}
}
// If Saddle Point not found
return false;
}
// Driver code
int main()
{
int mat[MAX][MAX] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int n = 3;
if (findSaddlePoint(mat, n) == false)
cout << "No Saddle Point ";
return 0;
}
/* Matrix sum Project
By : Morteza Zakeri 902
Date : 90/12/05
::Run this program with Microsoft Visual Stdio 2010 (Visual C++ 10)
*/
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
void main()
{
const int k = 100;
int m ,n, i, j;
float a[k][k]={0} , b[k][k]={0};
cout << "\n ::Enter number of M1 rows: ";
cin >> m;
cout << "\n ::Enter number of M1 columns: ";
cin >>n;
cout << "\n M2 has " << m << " Row & " << n <<" Column, too!\n ";
cout << "\n::Enter M1 data:\n\n";
for(i = 0; i < m; i++){
for(j = 0; j < n; j++ ){
cout << "\tA["<<i+1<<"]["<<j+1<<"]: ";
cin >> a[i][j];
}
cout <<"\n";
}//M1
cout << "\n::Enter M2 data:\n\n";
for(i = 0; i < m; i++){
for(j = 0; j < n; j++ ){
cout << "\tB["<<i+1<<"]["<<j+1<<"]: ";
cin >> b[i][j];
}
cout <<"\n";
}//M2
for(i = 0; i < m; i++){
for(j = 0; j < n; j++ ){
cout << "\tA+B["<<i+1<<"]["<<j+1<<"] = ";
cout << a[i][j] + b[i][j];
cout <<"\n";
}
cout <<"\n";
}//M1 + M2
cout << "Press a key to Exit ... ";
_getch();
}
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int maxF(int A[], const int k , int i, int max);
void read(int A[], const int k)
{
int i;
for(i=0;i<k;i++){
cout << "A["<<i<<"] = ";
cin >> A[i];
}
}
void main()
{
int i = 0,max;
const int k = 5;
int A[k];
read(A,k);
max = A[i];
cout << "\n\n ArrayMax = " << maxF(A,k,i,max)<< endl;
_getch();
}
int maxF(int A[], const int k , int i, int max)
{
if(k == i)
return max;
else if( max<(A[i+1]-A[i]))
return ( maxF( A,k,i+1,A[i+1]-A[i] ) );
else
return ( maxF( A,k,i+1,max ) );
}
#include <iostream>
#include <conio.h>
using namespace std;
void main(){
int i,j,k;
cout <<"Enter 1:";
int A[5], B[4], C[8];
for(i = 0; i<8; i++)
C[i] = 0;
for(i = 0; i<5; i++){
cout << "Enter " <<i<<" ^x : ";
cin >>A[i];
}
cout <<"Enter 2:";
for(i = 0; i<4; i++){
cout << "Enter " <<i<<" ^x : ";
cin >>B[i];
}
for(i = 0;i<5; i++)
for(j=0; j<4 ;j++)
C[i+j]+=A[i]*B[j];
for(i = 7; i>=0; i--)
cout << C[i] << "X^" <<i<<" + ";
_getch();
}
/* Numbers1 Project
By : Morteza Zakeri 902
Date : 90/12/10
::Run this program with Microsoft Visual Stdio 2010 (Visual C++ 10)
*/
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
void main()
{
int i=0, j, k,m, n, a=0;
cout << "::Enter a Number: ";
cin >> n;
system("cls");
a = n;
for (i = a,m = 0; i>0; m++,i--){
for (j=0 ; j < m ; j++)
cout << " ";
for (k=2*i+1; k > 2 ; k--)
cout << i ;
cout << "\n";
a++;
}
a = n;
for (i = 1; i <= n; i++){
for (j=a-1 ; j > 0 ; j--)
cout << " ";
for (k=2 ; k < 2*i+1 ; k++)
cout << i ;
cout << "\n";
a--;
}
_getch();
}
/* Numbers2 Project
By : Morteza Zakeri 902
Date : 90/12/10
::Run this program with Microsoft Visual Stdio 2010 (Visual C++ 10)
*/
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
void main()
{
int i, j, k, m, n, a;
cout << "::Enter a Number: ";
cin >> n;
system("cls");
a = n;
i = a;
m = 0;
while(i){
j=0;
while(j<m){
cout << " ";
j++;
}
k=2*i+1;
while(k>2){
cout << i;
k--;
}
cout << "\n";
a++;
m++;
i--;
}
a = n;
i = 1;
while(i<=n){
j=0;
j = a-1;
while(j){
cout << " ";
j--;
}
k=2;
while(k<2*i+1){
cout << i;
k++;
}
cout << "\n";
a--;
i++;
}
_getch();
}
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int i,j,k,l,m=31;
cout<<"\t\t\t\tWeek Menu"<<endl<<endl;
cout<<"\t\t1-Sat\t2-Sun\t3-Mon\t4-Tus\t5-Wed\t6-Tur\t7-Fri\n";
cout<<"\n\t\t\t Enter First day of year : ";
cin>>l;
//system("cls");
for(i=1;i<=12;i++){
cout<<endl<<endl;
switch(i){
case 1:
cout<<"\t\t\t ***Farvardin***"<<endl;
break;
case 2:
cout<<"\t\t\t ***Ordibehesht***"<<endl;
break;
case 3:
cout<<"\t\t\t ***Khordad***"<<endl;
break;
case 4:
cout<<"\t\t\t ***Tir***"<<endl;
break;
case 5:
cout<<"\t\t\t ***Mordad***"<<endl;
break;
case 6:
cout<<"\t\t\t ***Shahrivar***"<<endl;
break;
case 7:
cout<<"\t\t\t ***Mehr***"<<endl;
break;
case 8:
cout<<"\t\t\t ***Aban***"<<endl;
break;
case 9:
cout<<"\t\t\t ***Azar***"<<endl;
break;
case 10:
cout<<"\t\t\t ***Day***"<<endl;
break;
case 11:
cout<<"\t\t\t ***Bahman***"<<endl;
break;
case 12:
cout<<"\t\t\t ***Esfand***"<<endl;
break;
}//end of switch
if(i==7)
m=30;
if(i==12)
m=29;
cout<<"\n\tSat\tSun\tMon\tTus\tWed\tTur\tFri\n";
for(k=1;k<l;k++){
cout<<"\t";
}//end of for
for(j=1;j<=m;j++){
switch(l){
case 1:
cout<<"\t"<<j;
l++;
break;
case 2:
cout<<"\t"<<j;
l++;
break;
case 3:
cout<<"\t"<<j;
l++;
break;
case 4:
cout<<"\t"<<j;
l++;
break;
case 5:
cout<<"\t"<<j;
l++;
break;
case 6:
cout<<"\t"<<j;
l++;
break;
case 7:
printf("\t%d\n",j);
l=1;
break;
}//end of switch
}//end of for
getch();
}//end of for
}//end of main
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
void h(int n)
{
if(n <= 1)
cout << "Hello\t";
else
for (int i = 0 ; i <n ; i++)
h(i);
}
void main()
{
h(5);
_getch();
}
/* Stars3 Project
By : Morteza Zakeri 902
Date : 90/12/05
::Run this program with Microsoft Visual Stdio 2010 (Visual C++ 10)
*/
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
void main()
{
int i, j, k, n, a;
cout << "Enter a Number: ";
cin >> n;
a = n;
system("cls");
for (i = 1; i <= n; i++){
for (j=a-1 ; j > 0 ; j--)
cout << " ";
for (k=2 ; k < 2*i+1 ; k++)
cout << "*";
cout << "\n";
a--;
}
_getch();
}
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
class Example{
private:
int x;
public:
static int c;
Example(){c++;cout <<c<<" " ;}
~Example(){c--;cout << c<< " ";}
};
int Example::c =0;
void main()
{
Example Obj1[10];
_getch();
}
/* Stars3 Project
By : Morteza Zakeri 902
Date : 90/12/05
::Run this program with Microsoft Visual Stdio 2010 (Visual C++ 10)
*/
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string.h>
using namespace std;
void capitalize();
void repeat();
void threeletter();
void wordy();
char word[50];
void main()
{
//char word[50];
int flag=1,i=0,s;
cout<<"Please enter your string: "<<"\n";
/*while(flag)
{
word[i]=getche();
i++;
if(word[i-1]=='\r')
flag=0;
} */
gets(word);
//gets(word);
cout<<"\n"<<"Please select one this item: ";
cout<<"\n\n1.Capitalize 2.counting the repeat item";
cout<<"\n3.Three letter word 4.word that start ehit 'y'";
cin>>s;
if(s==1) {
capitalize();
}
else if(s==2)
repeat();
else if(s==3)
threeletter();
else if(s==4)
wordy();
cout <<endl;
system("pause");
}
//****************************
void capitalize()
{
for(int j=0;j<strlen(word);j++)
{
if(word[j]>='a' && word[j]<='z')
word[j]= word[j]-32;
}
for(int j=0;j<=strlen(word);j++)
cout<< word[j];
}
//***********************************
// this function is not still word correct!!!
void repeat()
{
int counter = 0;
for(int j=0;j<strlen(word);j++)
{
for(int k=0;k<strlen(word);k++)
if(word[j] == word[k])
counter++;
cout << word[j] << " -> " << counter <<endl;
counter = 0;
}
}
//************************************
void threeletter()
{
int counter = 0;
for(int j=0; j<strlen(word); j++)
{
counter++;
if(word[j]==' ' || j==strlen(word)-1)
{
if(counter==4 || j==strlen(word)-1)
for(int k=j-3;k<=j;k++)
cout<<word[k];
counter=0;
}
}
}
//***********************************
void wordy()
{
for(int j=0; j< strlen(word); j++)
{
if(word[j]=='y' || word[j]=='Y')
{
int i=j;
if(word[i-1]==' '|| i==0) {
while(word[i] != ' ' && i<strlen(word))
{
cout << word[i++];
}
cout << endl;
}
}
}
}
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
void main()
{
int n, sum = 0;
cin >> n;
while(n){
sum += n%10;
n /= 10;
}
cout << sum;
_getch();
}
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int sumDigit(int n)
{
if(n == 0)
return 0;
else
return(n%10 + sumDigit(n/10));
}
void main()
{
int n;
cin >> n;
cout << sumDigit(n);
_getch();
}
#include <conio.h>
#include <stdio.h>
int main ()
{
int i,j,k;
for(i=0; i<=25;i++){
for(k=1;k<15;k++){
printf("%c",176);
for(j=0;j<25000000;j++);
}
for(k=15;k<=30;k++){
printf("%c",177);
for(j=0;j<30000000;j++);
}
for(k=31;k<=50;k++){
printf("%c",219);
for(j=0;j<30000000;j++);
}
for(k=51;k<=66;k++){
printf("%c",177);
for(j=0;j<30000000;j++);
}
for(k=67;k<=80;k++){
printf("%c",176);
for(j=0;j<30000000;j++);
}
}
_getch();
return 0;
}
#include <conio.h>
#include <stdio.h>
int main ()
{
int i,j,k;
for(i=0; i<=2;i++){
for(k=1;k<15;k++){
printf("%c",176);
for(j=0;j<30000000;j++);
}
for(k=15;k<=30;k++){
printf("%c",177);
for(j=0;j<25000000;j++);
}
for(k=31;k<=50;k++){
printf("%c",219);
for(j=0;j<20000000;j++);
}
for(k=51;k<=66;k++){
printf("%c",177);
for(j=0;j<15000000;j++);
}
for(k=67;k<=80;k++){
printf("%c",176);
for(j=0;j<10000000;j++);
}
}
_getch();
return 0;
}
//First program with C++;
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int sum,x,y;
cout << "Welcome to C++! ";
cout << "Enter number 1: ";
cin >> x;
cout << "Enter number 2: ";
cin >> y;
sum = x + y;
cout << "\nSUM = "<<sum<<"\n\nPress any key to exit... ";
getch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment