Tuesday, 31 December 2013

C++ program which reads a lowercase letter and print equivalent uppercase letter

#include<iostream.h>
void main()
{

  clrscr();
  char s1,s2;
  cout<<"Enter The lower case charecter\n";
  cin>>s1;
  s2=s1-32;
  cout<<"\n upper case letter of " <<s1<<" is "<<s2;
   getch();
}

Example of Local Variable Using C++

1st example:::::::---------
#include<iostream.h>
  void function1()
 {
  int x;  
  x=10;
  cout<<"\n" <<x;
 }
void function2()
{
int x=34;
cout<<"\n" <<x;
}
void main()
{
       clrscr();
       function1();
       function2();

}

2nd example:::::::---------
#include<iostream.h>
void main()
{
   clrscr();
   int x =10        
   if(x==10)
    {
         int x; /*this x hides outer x*/
         x=77;
        cout<<"Inner x : " <<x;
     }
  cout<<"\n Outer x : "  <<x;
}

C++ Program prints the number 10 ten times Using static variable

#include<iostream.h>
void function()
{
static int k=10;
 cout<<k<<endl ;
 k++;
}
void main()
{
      clrscr();
     int i=10;
     for(i=0;i<10;i++)
     {
       function();
     }
}

C++ program to differentiate Local variable

#include<iostream.h>
void function()
{
  int j=10;
  cout<<j<<"\n" ;
  j++;
}
void main()
{
       clrscr();
     int i=10;
     for(i=0;i<10;i++)
     {
       function();
     }
}

Global Variable Example Using C++

#include<iostream.h>
int count;                               /* count is global*/
void function1();
void main()
{
 clrscr();

  count=100;

   function1();

   getch();

}
void function1()
{
   int temp;
   temp=count;
   cout<<"\n the value of temp and count ::" <<temp<<" " <<count;

}

C++ Program To Print The Perfect Square Number Which is Less Than 1000

#include<iostream.h>
void main()
{
   clrscr();
   int n=1000, s=0;
   cout<<"The Squre values Which is less Than 1000 is::  " ;

   for(int i=1;s<n;i++)
    {
      s=i*i;
      if(s>n)
      break;

     cout<<s<<" ";

    }


       getch();

}

C++ program to finding the maximum element in an array

#include<conio.h>
#include<iostream.h>
#include<stdio.h>
void main()
{
      clrscr();

    int array[]={1,2,3,4,5};

  int var1=array[0];
  for(int i=1;i<=5;i++)
   {  if(var1<array[i])
       var1=array[i];
   }
  cout<<var1;
       getch();

}

Saturday, 28 December 2013

C Progran to Implement 8 Queen's Problem using Backtracking

#include<stdio.h>
#include<conio.h>
#include<math.h>

char a[10][10];
int n=8;

void printmatrix()
{
 int i,j;
 printf("n");
 for(i=0;i < n;i++)
       {
       for(j=0;j < n;j++)
            printf("%ct",a[i][j]);
       printf("nn");
       }
}

//------------------------------------------

int getmarkedcol(int row)
{
 int i,j;
 for(i=0;i < n;i++)
     if(a[row][i]=='Q')
     {
     return(i);
     break;
     }
}

//------------------------------------------

int feasible(int row, int col)
{
 int i,tcol;
 for(i=0;i < n;i++)
     {
     tcol=getmarkedcol(i);
         if(col==tcol || abs(row-i)==abs(col-tcol))
              return 0;
     }
 return 1;
}

//------------------------------------------

void nqueen(int row)
{
 int i,j;
 if(row < n)
 {
  for(i=0;i < n;i++)
     {
     if(feasible(row,i))
         {
         a[row][i]='Q';
         nqueen(row+1);
         a[row][i]='.';
         }
     }
 }
 else
 {
 printf("nThe solution is:- ");
 printmatrix();
 }
}

//---------------------------------

void main()
{
 int i,j;
 clrscr();

 printf("8 Queen's Problem using Backtracking ");


 for(i=0;i < n;i++)
     for(j=0;j < n;j++)
  a[i][j]='.';

 nqueen(0);
 getch();
}

C++ Program having functions setmarks inside which 3 subject marks should be set & Make total a friend function inside which all 3 subject marks total will be done

#include<iostream.h>
  class Marks
{
  int a;
  int b;
  int c;
  public:
  void setmarks()
  {
  cout<<"Enter Marks Of Subject ";
  cin>>a>>b>>c;
  }
friend int total(Marks m);
};
int total(Marks m)
{
  return(m.a+m.b+m.c);
}
Void main()
{
  Marks x;
  x.setmarks();
  cout<<total(x);
}

C++ Program to find area of square and rectangle using function overloading


#include<iostream.h>
int area(int);
int area(int ,int );
void main()
{
  cout<<area(10);
  cout<<area(5,2);
}
int  area (int  l)
{
  return(l*l);
}
int area (int l,int b)
{
  return(l*b);
}

C++ Program For Swap Value Using Call By Reference

#include<iostream.h>
swap(int *,int *);
void main()
{
  int x=40, y=20;
  cout<<“Before calling swap:”<<x<<y;
  swap(&x,&y);
  cout<< “After calling swap :”<<x<<y;
}
swap(int  *a , int  *b)
{
  int  temp;
  temp=*a;
  *a=*b;
  *b=temp;
  cout<< “with swap function”<<a<<b;

}
///////////////////////////////////////////////////////////
Call by Reference
>      In the call by reference method the address of arguments are passed as actual parameters to the called function, the function can change the contents stored at those addresses and these changes will be visible in the calling function.
>      In this when the function is working with its own arguments(formal),it is actually working on the  original data.

C++ Program For Swap Value Using Call By Value

#include<iostream.h>
swap(int,int);
void main()
{
int x=10, y=20;
cout<<“Before calling swap:”<<x<<y;
swap(x,y);
cout<<“After calling swap :”<<x<<y;
}
swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<“with swap function”<<a<<b;
}

/*

Output
Before calling swap : x=10, y=20
With swap function : a=20, b=10
After calling swap : x=10, y=20

*/

Thursday, 26 December 2013

C Progran to Implement N Queen's Problem using Backtracking

#include<stdio.h>
#include<conio.h>
#include<math.h>

char a[10][10];
int n;

void printmatrix()
{
 int i,j;
 printf("n");
 for(i=0;i < n;i++)
       {
       for(j=0;j < n;j++)
            printf("%ct",a[i][j]);
       printf("nn");
       }
}

//------------------------------------------

int getmarkedcol(int row)
{
 int i,j;
 for(i=0;i < n;i++)
     if(a[row][i]=='Q')
     {
     return(i);
     break;
     }
}

//------------------------------------------

int feasible(int row, int col)
{
 int i,tcol;
 for(i=0;i < n;i++)
     {
     tcol=getmarkedcol(i);
         if(col==tcol || abs(row-i)==abs(col-tcol))
              return 0;
     }
 return 1;
}

//------------------------------------------

void nqueen(int row)
{
 int i,j;
 if(row < n)
 {
  for(i=0;i < n;i++)
     {
     if(feasible(row,i))
         {
         a[row][i]='Q';
         nqueen(row+1);
         a[row][i]='.';
         }
     }
 }
 else
 {
 printf("nThe solution is:- ");
 printmatrix();
 }
}

//---------------------------------

void main()
{
 int i,j;
 clrscr();

 printf("n Enter the no. of queens:- ");
 scanf("%d",&n);

 for(i=0;i < n;i++)
     for(j=0;j < n;j++)
  a[i][j]='.';

 nqueen(0);
 getch();
}

Display 2-D Array Using C++

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,a[3][3]={{1,2,3},
                {4,5,6},
                {7,8,9}};
               
for(i=0;i<3;i++)
{
   for(j=0;j<3;j++)
     {
       cout<<a[i][j]<<" ";
     }
cout<<"\n";   
}
getch();
}   

TICK TOCK GAME USING C++

#include<iostream.h>
#include<conio.h>


void final();
void winner();
void condition();
void print();
void next();
void initialise();

int m=0,i,j,f=0,l=0,rw,co,k;
char a[3][3],ch='y';

class player
{
public:
char nm[20];

void getdata()
{
cout<<nm<<" enter position\n";
cout<<"row no - ";
cin>>i;
cout<<"coloumn no - ";
cin>>j;
if(i>2 || j>2)
{
getdata();
}

}
} p1,p2;


class game
{
public:
 void check1()
{
//s1=clock();
final();

if(f==0)
{
  condition();
  if(l>0)
 {
   p1.getdata();
   if(a[i][j] == 'x'|| a[i][j] =='o')
    {
     cout<<"re-enter \n";
     check1();
    }
   else
   {
    a[i][j]='x';
    print();
    check2();
   }
 }
else
 {
  cout<<"game draw";
  next();
 }
}

else
{
m=1;
winner();
}


}



void check2()
{
final();

if(f==0)
{
  condition();
  if(l>0)
{
  p2.getdata();
 if(a[i][j] == 'x'||a[i][j] =='o')
 {
  cout<<"re-enter \n";
  check2();

 }

 else
 {
  a[i][j]='o';
  print();
  check1();
 }
 }
else
{
cout<<"game draw";
next();
}
}
else
{
winner();
}


}
};

void main()
{
clrscr();
cout<<"enter name of 1st player \n";
cin>>p1.nm;
cout<<"enter name of 2nd player \n";
cin>>p2.nm;
game g;
for(i=0;i<20;i++)
{
if(ch=='y'||ch=='Y')
{
initialise();
print();
g.check1();
}
}
getch();
}


void final()
{
 if((a[0][0]=='x' && a[0][1]=='x'&& a[0][2]=='x')||(a[0][0]=='o' && a[0][1]=='o'&& a[0][2]=='o'))
 {
  f=1;
 }
 else if((a[1][0]=='x'&&a[1][1]=='x'&&a[1][2]=='x')||(a[1][0]=='o'&&a[1][1]=='o'&&a[1][2]=='o'))
 {
 f=1;
 }
 else if((a[2][2]=='x'&&a[2][1]=='x'&&a[2][2]=='x')||(a[2][2]=='o'&&a[2][1]=='o'&&a[2][2]=='o'))
 {
 f=1;
 }
 else if((a[0][0]=='x'&&a[1][1]=='x'&&a[2][2]=='x')||(a[0][0]=='o'&&a[1][1]=='o'&&a[2][2]=='o'))
 {
 f=1;
 }
 else if((a[0][0]=='x'&&a[1][0]=='x'&&a[2][0]=='x')||(a[0][0]=='o'&&a[1][0]=='o'&&a[2][0]=='o'))
 {
 f=1;
 }
 else if((a[0][1]=='x'&&a[1][1]=='x'&&a[2][1]=='x')||(a[0][1]=='o'&&a[1][1]=='o'&&a[2][1]=='o'))
 {
 f=1;
 }
 else if((a[0][2]=='x'&&a[1][2]=='x'&&a[2][2]=='x')||(a[0][2]=='o'&&a[1][2]=='o'&&a[2][2]=='o'))
 {
 f=1;
 }
 else if((a[0][2]=='x'&&a[1][1]=='x'&&a[2][0]=='x')||(a[0][2]=='o'&&a[1][1]=='o'&&a[2][0]=='o'))
 {
 f=1;
 }
 else
 {
 f=0;
 }
}




void condition()
{
l=0;
 for(rw=0;rw<3;rw++)
  {
  for(co=0;co<3;co++)
  {
   if(a[rw][co] == 'x' || a[rw][co] =='o')
   {
     k=0;
   }
   else
   {
   l++;
   }
  }
  }
}

void print()
{
cout<<"\n";
cout<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<"\n";
cout<<"_"<<"  "<<"_"<<"  "<<"_"<<"  "<<"_"<<"\n";

cout<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<"\n";
cout<<"_"<<"  "<<"_"<<"  "<<"_"<<"  "<<"_"<<"\n";

cout<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<"\n";
}

void winner()
{
if(m==1)
{
cout<<p2.nm<<" is the winner\n";
next();
}
else
{
cout<<p1.nm<<" is the winner\n";
next();
}
}

void next()
{
cout<<"do you want to play another game (y/n) ??\n";
ch=getche();
}

void initialise()
{
for(rw=0;rw<3;rw++)
{
 for(co=0;co<3;co++)
 {
  a[rw][co]=' ';
 }
}
}

Sudoku solving algorithm C

 #include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
int a[9][9],b[9][9],r[60][2];
int row, col, icount, mode;
char solh;
void main()
{


void readvalues(void), display(void),generate(void);
int solve(),checkmat(), checkrow(), checkcolumn(),check(),isfixed();
int i,j,error;
char c;
clrscr();
printf("\t\t\t   SUDOKU SOLVER by:\n\n\t\t\t  \n\t\t\t   MCA-M.Tech CSE, Lovely Professional University\n\t\t\t   Email:@hotmail.com");
printf("\n\n\n\n\t\tEnter '1' to GENERATE SuDoKu Puzzle\n\n\t\tEnter any other key to SOLVE SuDoKu\n\n\n\n\t\tEnter: ");
scanf("%c",&c);
if(c=='1')
    {
    getchar();
    generate();
    if(solh=='y' || solh=='Y')
    readvalues();
    for(i=0;i<9 br="" i="">    for(j=0;j<9 br="" j="">    a[i][j]=b[i][j];
    clrscr();
    display();
    getchar();
    getchar();
    exit(0);
    }
clrscr();
printf("\n\n\t\t\tHOW TO ENTER ELEMENTS\n");
printf("\n\n1.Enter the elements by specifying the row and column number of\n  corresponding location");
printf("\n\n2.Each row and column start from 1 and end at 9 (not from 0 to 8)");
printf("\n\n3.After finishing entering the elements, enter zeroes (0)\n  for row or column");
printf("\n\n4.If an element entered in a wrong location has to be deleted,\n  enter zero in that location");
printf("\n\n\n\n\t\t\t  Enter any key to continue");
getchar();
read:
icount=0;
row=0; col=0;
for(i=0;i<9 br="" i="">for(j=0;j<9 br="" j="">a[i][j]=0;
for(i=0;i<60 br="" i="">    {
    r[i][0]=20; r[i][1]=20;
    }
    clrscr();
    readvalues();
    getchar();
    clrscr();
    ask:
    display();
    printf("\n\nEnter 'm' to MODIFY\nEnter 'r' to RE-ENTER\nEnter any other key for SUDOKU Solution: ");
    scanf("%c",&c);
    if(c=='r' || c=='R')
        {
        clrscr();
        goto read;
        }
    if(c=='m' || c=='M')
        {
        printf("\n\n");
        readvalues();
        clrscr();
        getchar();
        goto ask;
        }
    next:
    error=solve();
    clrscr();
    display();
    if(error==0)
    printf("\n\n\n\n\rNO SUOLUTION or TOO MANY MODIFICATIONS");
    cprintf("\n\n\n\n\n\rEnter c to continue and any other key to exit: ");
    scanf("%c",&c);
    if(c=='c')
    goto read;
    getchar();
}

/*FUNTION TO READ ELEMENTS*/

void readvalues()
{
int i=icount,j,x,y,chek;
void display(void);
int check(int,int);
clrscr();
do
{
display();
printf("\n");
row:
printf("\n\nEnter row: ");
scanf("%d",&r[i][0]);
if(r[i][0]==0)
    {
    r[i][0]=100; r[i][1]=100;
    break;
    }
if(r[i][0]>10 || r[i][0]<0 br="">    {
    printf("\nInvalid size\nEnter again");
    r[i][0]=20; r[i][1]=20;
    getchar();
    goto row;
    }
column:
printf("Enter column: ");
scanf("%d",&r[i][1]);
if(r[i][1]==0)
    {
    r[i][0]=100; r[i][1]=100;
    break;
    }
if(r[i][1]<0 i="" r="">10)
    {
    printf("\nInvalid size\nEnter again\n\n");
    r[i][0]=20; r[i][1]=20;
    getchar();
    goto column;
    }
r[i][0]=r[i][0]-1; r[i][1]=r[i][1]-1;
x=r[i][0]; y=r[i][1];
if(solh=='y' || solh=='Y')
for(j=0;j    {
    if(x==r[j][0] && y==r[j][1])
        {
        printf("This location is fixed and cannot be changed");
        goto row;
        }
    else
        r[i][0]=20; r[i][1]=20;
    }
scan:
printf("Enter the corresponding number: ");
scanf("%d",&a[x][y]);
if(a[x][y]==0)
    {
    for(j=0;j        {
        if(x==r[j][0] && y==r[j][1])
            {
            r[j][0]=20; r[j][1]=20;
            goto end;
            }
        if(solh=='y' || solh=='Y')
            {
            r[i][0]=20; r[i][1]=20;
            goto end;
            }
        }
    }
if(a[x][y]>9 || a[x][y]<=0)
    {
     printf("\nNumber out of range\nEnter again\n\n");
     a[x][y]=0;
     goto scan;
     }
else
{
chek=check(x,y);
if(chek==0)
    {
    printf("\nInconsistent number\nEnter again");
    a[x][y]=0; r[i][0]=20; r[i][1]=20;
    goto row;
    }
}
if(solh!='y' && solh!='Y')
    {
    icount++; i++;
    }
end:
clrscr();
}while(r[i-1][0]!=-1 || r[i-1][1]!=-1);
}

isfixed(int count, int m, int n)
{
int i;
for(i=0;iif(m==r[i][0] && n==r[i][1])
return(1);
return(0);
}

/*FUNCTION TO DISPLAY SUDOKU*/

void display()
{
int i,j,k,fixed;
gotoxy(6,0);
for(i=0;i<10 br="" i="">    {
    if(i%3==0)
        {
        textcolor(WHITE);
        if(i!=0)
        printf("\n");
        printf("\t\t");
        for(k=0;k<45 br="" k="">        cprintf("*");
        if(i==9)
        goto end;
        }
    if(i%3!=0)
        {
        textcolor(WHITE);
        printf("\n\t\t");
        printf("|\t\t\b|\t\t\b\b|\t\t\t\b\b\b\b|");
        printf("\n\t\t");
        }
    else
    printf("\n\t\t");
    for(j=0;j<9 br="" j="">        {
        if(j%3==0)
            {
            if(a[i][j]==0)
                {
                textcolor(WHITE);
                cprintf("|");
                for(k=0;k<4 br="" k="">                printf(" ");
                }
            else
                {
                textcolor(WHITE);
                cprintf("| ",a[i][j]);
                fixed=isfixed(icount,i,j);
                if(fixed==1)
                textcolor(LIGHTRED);
                else
                textcolor(LIGHTGREEN);
                cprintf("%d  ",a[i][j]);
                }
            }
        else
            {
            if(a[i][j]==0)
            for(k=0;k<5 br="" k="">            printf(" ");
            else
                {
                fixed=isfixed(icount,i,j);
                if(fixed==1)
                textcolor(LIGHTRED);
                else
                textcolor(LIGHTGREEN);
                cprintf("  %d  ",a[i][j]);
                }
            }
        }textcolor(WHITE);
        cprintf("\b|");
    }end:
}


int checkmat(int m, int n)
{
int x,y;
for(x=(m/3)*3;x<((m/3)*3+3);x++)
for(y=(n/3)*3;y<((n/3)*3+3);y++)
    {
    if(m!=x && n!=y)
    if(a[m][n]==a[x][y])
    return(0);
    }
return(1);
}


int checkrow(int m, int n)
{
int x;
for(x=0;x<9 br="" x="">    {
    if(x!=n)
    if(a[m][n]==a[m][x])
    return(0);
    }
return(1);
}


int checkcolumn(int m, int n)
{
int x;
for(x=0;x<9 br="" x="">    {
    if(x!=m)
    if(a[m][n]==a[x][n])
    return(0);
    }
return(1);
}

/*FUNCTION TO CHECK CONSISTENCY OF AN ELEMENT*/

int check(int m,int n)
{
int c;
c=checkmat(m,n);
if(c==1)
c=c*checkrow(m,n);
else
return(c);
if(c==1)
c=c*checkcolumn(m,n);
else
return(c);
return(c);
}

/*FUNCTION TO SOLVE SUDOKU*/

solve()
{
int i,m=0,n=0,number,c,fixed;

mainallot:

for(m=row;m<9 br="" m="">    {
    n=col;
    while(n<9 br="">        {
        number=1;
        fixed=isfixed(icount,m,n);
        if(fixed==0)
            {
            allot:
            a[m][n]=number;
            c=check(m,n);
            if(c==0)
                {
                number++;
                if(number>9)
                    {
                    a[m][n]=0;
                    if(n==0)
                        {
                        row=m-1; col=8;
                        if(row<0 br="">                        return(0);
                        goto backadjust;
                        }
                    else
                        {
                        row=m; col=n-1;
                        if(row<0 br="" col="">                        return(0);
                        goto backadjust;
                        }
                    }
                goto allot;
                }
            }
        n++;
        if(n>8)
        col=0;
        }
    }
return(1);

backadjust:

fixed=isfixed(icount,row,col);
if(fixed==0)
    {
    number=a[row][col];
    allot1:
    number++;
    a[row][col]=number;
    if(number>9)
        {
        a[row][col]=0;
        if(col==0)
            {
            row--; col=8;
            if(row<0 br="">            return(0);
            goto backadjust;
            }
        else
            {
            col--;
            if(col<0 br="">            return(0);
            goto backadjust;
            }
        }
    c=check(row,col);
    if(c==0)
    goto allot1;
    else
        {
        if(col==8)
            {
            row++;
            if(row>8)
            return(0);
            col=0;
            goto mainallot;
            }
        else
            {
            col++;
            if(col>8)
            return(0);
            goto mainallot;
            }
        }
    }
else
    {
    if(col==0)
        {
        row--; col=8;
        if(row<0 br="">        return(0);
        goto backadjust;
        }
    else
        {
        col--;
        if(col<0 br="">        return(0);
        goto backadjust;
        }
    }
}

/*FUNCTION TO GENERATE SODOKU*/

void generate()
{
int c,i,m,n,count=1,fixed,error;
char choice;
randomize();
generate:
for(m=0;m<9 br="" m="">for(n=0;n<9 br="" n="">a[m][n]=0;
for(i=0;i<60 br="" i="">    {
    r[i][0]=20; r[i][1]=20;
    }
clrscr();
printf("\n\n\n\n\t\tEnter your choice of difficulty");
printf("\n\n\n\t\t1.VERY EASY\n\n\t\t2.EASY\n\n\t\t3.MEDIUM\n\n\t\t4.HARD\n\n\n\t\tENTER: ");
scanf("%c",&choice);
switch(choice)
    {
    case '1': mode=34+random(4);
          break;
    case '2': mode=29+random(4);
          break;
    case '3': mode=27+random(2);
          break;
    case '4': mode=25+random(2);
          break;
    default : getchar();
              goto generate;
    }
a[0][0]=1+random(8); r[0][0]=0; r[0][1]=0;
for(count=1;count<10 br="" count="">    {
    choose:
    m=random(9); n=random(9);
    fixed=isfixed(icount,m,n);
    if(fixed==1)
    goto choose;
    r[count][0]=m; r[count][1]=n;
    allot:
    a[m][n]=1+random(9);
    if(a[m][n]==0)
    goto allot;
    c=check(m,n);
    if(c==0)
    goto allot;
    }
icount=count;
error=solve();
for(m=0;m<9 br="" m="">for(n=0;n<9 br="" n="">b[m][n]=a[m][n];
row=0; col=0;
for(i=0;i<60 br="" i="">    {
    r[i][0]=20; r[i][1]=20;
    }
if(error==0)
goto generate;
for(count=0;count    {
    choose2:
    m=random(9); n=random(5);
    fixed=isfixed(count,m,n);
    if(fixed==1)
    goto choose2;
    r[count][0]=m; r[count][1]=n;
    r[count+mode/2][0]=8-m;
    r[count+mode/2][1]=8-n;
    }
for(m=0;m<9 br="" m="">for(n=0;n<9 br="" n="">    {
    fixed=isfixed(mode,m,n);
    if(fixed!=1)
    a[m][n]=0;
    }
icount=mode;
clrscr();
display();
getchar();
printf("\n\n\nEnter 'y' to solve by yourself\nEnter 's' for solution\nEnter 'm' to generate more sudoku puzzles");
printf("\nAnd any other key to exit: ");
scanf("%c", &solh);
if(solh=='y' || solh=='Y' || solh=='s' || solh=='S')
return;
getchar();
if(solh=='m' || solh=='M')
goto generate;
exit(0);
}

Sudoku in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
int a[9][9],b[9][9],r[60][2];
int row, col, icount, mode;
char solh;
void main()
{


void readvalues(void), display(void),generate(void);
int solve(),checkmat(), checkrow(), checkcolumn(),check(),isfixed();
int i,j,error;
char c;
clrscr();
printf("\t\t\t   SUDOKU SOLVER by:\n\n\t\t\t  \n\t\t\t   MCA-M.Tech CSE, Lovely Professional University\n\t\t\t   Email:@hotmail.com");
printf("\n\n\n\n\t\tEnter '1' to GENERATE SuDoKu Puzzle\n\n\t\tEnter any other key to SOLVE SuDoKu\n\n\n\n\t\tEnter: ");
scanf("%c",&c);
if(c=='1')
    {
    getchar();
    generate();
    if(solh=='y' || solh=='Y')
    readvalues();
    for(i=0;i<9 br="" i="">    for(j=0;j<9 br="" j="">    a[i][j]=b[i][j];
    clrscr();
    display();
    getchar();
    getchar();
    exit(0);
    }
clrscr();
printf("\n\n\t\t\tHOW TO ENTER ELEMENTS\n");
printf("\n\n1.Enter the elements by specifying the row and column number of\n  corresponding location");
printf("\n\n2.Each row and column start from 1 and end at 9 (not from 0 to 8)");
printf("\n\n3.After finishing entering the elements, enter zeroes (0)\n  for row or column");
printf("\n\n4.If an element entered in a wrong location has to be deleted,\n  enter zero in that location");
printf("\n\n\n\n\t\t\t  Enter any key to continue");
getchar();
read:
icount=0;
row=0; col=0;
for(i=0;i<9 br="" i="">for(j=0;j<9 br="" j="">a[i][j]=0;
for(i=0;i<60 br="" i="">    {
    r[i][0]=20; r[i][1]=20;
    }
    clrscr();
    readvalues();
    getchar();
    clrscr();
    ask:
    display();
    printf("\n\nEnter 'm' to MODIFY\nEnter 'r' to RE-ENTER\nEnter any other key for SUDOKU Solution: ");
    scanf("%c",&c);
    if(c=='r' || c=='R')
        {
        clrscr();
        goto read;
        }
    if(c=='m' || c=='M')
        {
        printf("\n\n");
        readvalues();
        clrscr();
        getchar();
        goto ask;
        }
    next:
    error=solve();
    clrscr();
    display();
    if(error==0)
    printf("\n\n\n\n\rNO SUOLUTION or TOO MANY MODIFICATIONS");
    cprintf("\n\n\n\n\n\rEnter c to continue and any other key to exit: ");
    scanf("%c",&c);
    if(c=='c')
    goto read;
    getchar();
}

/*FUNTION TO READ ELEMENTS*/

void readvalues()
{
int i=icount,j,x,y,chek;
void display(void);
int check(int,int);
clrscr();
do
{
display();
printf("\n");
row:
printf("\n\nEnter row: ");
scanf("%d",&r[i][0]);
if(r[i][0]==0)
    {
    r[i][0]=100; r[i][1]=100;
    break;
    }
if(r[i][0]>10 || r[i][0]<0 br="">    {
    printf("\nInvalid size\nEnter again");
    r[i][0]=20; r[i][1]=20;
    getchar();
    goto row;
    }
column:
printf("Enter column: ");
scanf("%d",&r[i][1]);
if(r[i][1]==0)
    {
    r[i][0]=100; r[i][1]=100;
    break;
    }
if(r[i][1]<0 i="" r="">10)
    {
    printf("\nInvalid size\nEnter again\n\n");
    r[i][0]=20; r[i][1]=20;
    getchar();
    goto column;
    }
r[i][0]=r[i][0]-1; r[i][1]=r[i][1]-1;
x=r[i][0]; y=r[i][1];
if(solh=='y' || solh=='Y')
for(j=0;j    {
    if(x==r[j][0] && y==r[j][1])
        {
        printf("This location is fixed and cannot be changed");
        goto row;
        }
    else
        r[i][0]=20; r[i][1]=20;
    }
scan:
printf("Enter the corresponding number: ");
scanf("%d",&a[x][y]);
if(a[x][y]==0)
    {
    for(j=0;j        {
        if(x==r[j][0] && y==r[j][1])
            {
            r[j][0]=20; r[j][1]=20;
            goto end;
            }
        if(solh=='y' || solh=='Y')
            {
            r[i][0]=20; r[i][1]=20;
            goto end;
            }
        }
    }
if(a[x][y]>9 || a[x][y]<=0)
    {
     printf("\nNumber out of range\nEnter again\n\n");
     a[x][y]=0;
     goto scan;
     }
else
{
chek=check(x,y);
if(chek==0)
    {
    printf("\nInconsistent number\nEnter again");
    a[x][y]=0; r[i][0]=20; r[i][1]=20;
    goto row;
    }
}
if(solh!='y' && solh!='Y')
    {
    icount++; i++;
    }
end:
clrscr();
}while(r[i-1][0]!=-1 || r[i-1][1]!=-1);
}

isfixed(int count, int m, int n)
{
int i;
for(i=0;iif(m==r[i][0] && n==r[i][1])
return(1);
return(0);
}

/*FUNCTION TO DISPLAY SUDOKU*/

void display()
{
int i,j,k,fixed;
gotoxy(6,0);
for(i=0;i<10 br="" i="">    {
    if(i%3==0)
        {
        textcolor(WHITE);
        if(i!=0)
        printf("\n");
        printf("\t\t");
        for(k=0;k<45 br="" k="">        cprintf("*");
        if(i==9)
        goto end;
        }
    if(i%3!=0)
        {
        textcolor(WHITE);
        printf("\n\t\t");
        printf("|\t\t\b|\t\t\b\b|\t\t\t\b\b\b\b|");
        printf("\n\t\t");
        }
    else
    printf("\n\t\t");
    for(j=0;j<9 br="" j="">        {
        if(j%3==0)
            {
            if(a[i][j]==0)
                {
                textcolor(WHITE);
                cprintf("|");
                for(k=0;k<4 br="" k="">                printf(" ");
                }
            else
                {
                textcolor(WHITE);
                cprintf("| ",a[i][j]);
                fixed=isfixed(icount,i,j);
                if(fixed==1)
                textcolor(LIGHTRED);
                else
                textcolor(LIGHTGREEN);
                cprintf("%d  ",a[i][j]);
                }
            }
        else
            {
            if(a[i][j]==0)
            for(k=0;k<5 br="" k="">            printf(" ");
            else
                {
                fixed=isfixed(icount,i,j);
                if(fixed==1)
                textcolor(LIGHTRED);
                else
                textcolor(LIGHTGREEN);
                cprintf("  %d  ",a[i][j]);
                }
            }
        }textcolor(WHITE);
        cprintf("\b|");
    }end:
}


int checkmat(int m, int n)
{
int x,y;
for(x=(m/3)*3;x<((m/3)*3+3);x++)
for(y=(n/3)*3;y<((n/3)*3+3);y++)
    {
    if(m!=x && n!=y)
    if(a[m][n]==a[x][y])
    return(0);
    }
return(1);
}


int checkrow(int m, int n)
{
int x;
for(x=0;x<9 br="" x="">    {
    if(x!=n)
    if(a[m][n]==a[m][x])
    return(0);
    }
return(1);
}


int checkcolumn(int m, int n)
{
int x;
for(x=0;x<9 br="" x="">    {
    if(x!=m)
    if(a[m][n]==a[x][n])
    return(0);
    }
return(1);
}

/*FUNCTION TO CHECK CONSISTENCY OF AN ELEMENT*/

int check(int m,int n)
{
int c;
c=checkmat(m,n);
if(c==1)
c=c*checkrow(m,n);
else
return(c);
if(c==1)
c=c*checkcolumn(m,n);
else
return(c);
return(c);
}

/*FUNCTION TO SOLVE SUDOKU*/

solve()
{
int i,m=0,n=0,number,c,fixed;

mainallot:

for(m=row;m<9 br="" m="">    {
    n=col;
    while(n<9 br="">        {
        number=1;
        fixed=isfixed(icount,m,n);
        if(fixed==0)
            {
            allot:
            a[m][n]=number;
            c=check(m,n);
            if(c==0)
                {
                number++;
                if(number>9)
                    {
                    a[m][n]=0;
                    if(n==0)
                        {
                        row=m-1; col=8;
                        if(row<0 br="">                        return(0);
                        goto backadjust;
                        }
                    else
                        {
                        row=m; col=n-1;
                        if(row<0 br="" col="">                        return(0);
                        goto backadjust;
                        }
                    }
                goto allot;
                }
            }
        n++;
        if(n>8)
        col=0;
        }
    }
return(1);

backadjust:

fixed=isfixed(icount,row,col);
if(fixed==0)
    {
    number=a[row][col];
    allot1:
    number++;
    a[row][col]=number;
    if(number>9)
        {
        a[row][col]=0;
        if(col==0)
            {
            row--; col=8;
            if(row<0 br="">            return(0);
            goto backadjust;
            }
        else
            {
            col--;
            if(col<0 br="">            return(0);
            goto backadjust;
            }
        }
    c=check(row,col);
    if(c==0)
    goto allot1;
    else
        {
        if(col==8)
            {
            row++;
            if(row>8)
            return(0);
            col=0;
            goto mainallot;
            }
        else
            {
            col++;
            if(col>8)
            return(0);
            goto mainallot;
            }
        }
    }
else
    {
    if(col==0)
        {
        row--; col=8;
        if(row<0 br="">        return(0);
        goto backadjust;
        }
    else
        {
        col--;
        if(col<0 br="">        return(0);
        goto backadjust;
        }
    }
}

/*FUNCTION TO GENERATE SODOKU*/

void generate()
{
int c,i,m,n,count=1,fixed,error;
char choice;
randomize();
generate:
for(m=0;m<9 br="" m="">for(n=0;n<9 br="" n="">a[m][n]=0;
for(i=0;i<60 br="" i="">    {
    r[i][0]=20; r[i][1]=20;
    }
clrscr();
printf("\n\n\n\n\t\tEnter your choice of difficulty");
printf("\n\n\n\t\t1.VERY EASY\n\n\t\t2.EASY\n\n\t\t3.MEDIUM\n\n\t\t4.HARD\n\n\n\t\tENTER: ");
scanf("%c",&choice);
switch(choice)
    {
    case '1': mode=34+random(4);
          break;
    case '2': mode=29+random(4);
          break;
    case '3': mode=27+random(2);
          break;
    case '4': mode=25+random(2);
          break;
    default : getchar();
              goto generate;
    }
a[0][0]=1+random(8); r[0][0]=0; r[0][1]=0;
for(count=1;count<10 br="" count="">    {
    choose:
    m=random(9); n=random(9);
    fixed=isfixed(icount,m,n);
    if(fixed==1)
    goto choose;
    r[count][0]=m; r[count][1]=n;
    allot:
    a[m][n]=1+random(9);
    if(a[m][n]==0)
    goto allot;
    c=check(m,n);
    if(c==0)
    goto allot;
    }
icount=count;
error=solve();
for(m=0;m<9 br="" m="">for(n=0;n<9 br="" n="">b[m][n]=a[m][n];
row=0; col=0;
for(i=0;i<60 br="" i="">    {
    r[i][0]=20; r[i][1]=20;
    }
if(error==0)
goto generate;
for(count=0;count    {
    choose2:
    m=random(9); n=random(5);
    fixed=isfixed(count,m,n);
    if(fixed==1)
    goto choose2;
    r[count][0]=m; r[count][1]=n;
    r[count+mode/2][0]=8-m;
    r[count+mode/2][1]=8-n;
    }
for(m=0;m<9 br="" m="">for(n=0;n<9 br="" n="">    {
    fixed=isfixed(mode,m,n);
    if(fixed!=1)
    a[m][n]=0;
    }
icount=mode;
clrscr();
display();
getchar();
printf("\n\n\nEnter 'y' to solve by yourself\nEnter 's' for solution\nEnter 'm' to generate more sudoku puzzles");
printf("\nAnd any other key to exit: ");
scanf("%c", &solh);
if(solh=='y' || solh=='Y' || solh=='s' || solh=='S')
return;
getchar();
if(solh=='m' || solh=='M')
goto generate;
exit(0);
}

C++ program using multiple inheritance

#include<iostream.h>

#include<conio.h>

Class Stuinfo

{

Private:

Char name [25];

Int roll;

Public:

Void getdata ()

{

Cout<<”\n Enter name”;

Cin>>name;

Cout<<”\n Enter roll”;

Cin>> roll;

}

Void display ()

{

Cout<<”\n name=”<<name;

Cout<<”\n Roll number”<<roll;

}

};

Class Stuacademicinfo

{

Private:

Char course [25];

Char semester [15];

Public:

Void getdata ()

{

Cout<<”\n Enter course name”;

Cin>>course;

Cout<<”\n Enter semester”;

Cin>>semester;

}

Void display ()

{

Cout<<”\n Course “<<course;

Cout<<”\n semester”<<semester;

};

Class stuaddressinfo: public Stuinfo, public Stuacademicinfo

{

Private:

Char address [25];

Public:

Void getdata ()

{

Stuinfo:: getdata ();

Stuacademicinfo:: getdata ();

Cout<<”\n Enter the address”;

Cin>>address;

}

Void display ()

{

Stuinfo:: display ();

Stuacademic:: display ();

Cout <<”\n Address”<<address;

};

Void main ()

{

Stuaddressinfo obj;

Obj .getdata ();

Obj.dispay ();

getch ();

}

C++ programming to virtual function

#include <iostream>
using namespace std;
class B
{
    public:
     virtual void display()      /* Virtual function */
         { cout<<"Content of base class.\n"; }
};

class D1 : public B
{
    public:
       void display()
         { cout<<"Content of first derived class.\n"; }
};

class D2 : public B
{
    public:
       void display()
         { cout<<"Content of second derived class.\n"; }
};

int main()
{
    B *b;
    D1 d1;
    D2 d2;

/* b->display();  // You cannot use this code here because the function of base class is virtual. */

    b = &d1;
    b->display();   /* calls display() of class derived D1 */
    b = &d2;          
    b->display();   /* calls display() of class derived D2 */
    return 0;
}
*****************************************
Output
Content of first derived class.
Content of second derived class.
////////////////////////////////////////////////////////////////////////////
Virtual Function in C++ Programming
If there are member function with same name in derived classes, virtual functions gives programmer capability to call member function of different class by a same function call depending upon different context. This feature in C++ programming is known as polymorphism which is one of the important feature of OOP.

C++ program to demonstrate the working of friend function

#include <iostream>
using namespace std;
class Distance
{
    private:
        int meter;
    public:
        Distance(): meter(0){ }
        friend int func(Distance);  //friend function
};
int func(Distance d)            //function definition
{
    d.meter=5;         //accessing private data from non-member function
    return d.meter;
}
int main()
{
    Distance D;
    cout<<"Distace: "<<func(D);
    return 0;
}
*************************************
Output
Distance: 5
//////////////////////////////////////////////////////////////////////
C++ Programming friend Function and friend Classes

One of the important concept of OOP is data hiding, i.e., a nonmember function cannot access an object's private or protected data. But, sometimes this restriction may force programmer to write long and complex codes. So, there is mechanism built in C++ programming to access private or protected data from non-member function which is  friend function and friend class.

friend Function in C++

If a function is defined as a friend function then, the private and protected data of class can be accessed from that function. The complier knows a given function is a friend function by its keyword friend. The declaration of friend function should be made inside the body of class (can be anywhere inside class either in private or public section) starting with keyword friend.
class class_name
{
    ......  ....   ........
    friend return_type function_name(argument/s);
    ......  ....   ........
}

Strassen's Method For Matrix Multiplication Using C


#include<stdio.h>
int main(){
  int a[2][2],b[2][2],c[2][2],i,j;
  int m1,m2,m3,m4,m5,m6,m7;

  printf("Enter the 4 elements of first matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("Enter the 4 elements of second matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&b[i][j]);

  printf("\nThe first matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }

  printf("\nThe second matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",b[i][j]);
  }

  m1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
  m2= (a[1][0]+a[1][1])*b[0][0];
  m3= a[0][0]*(b[0][1]-b[1][1]);
  m4= a[1][1]*(b[1][0]-b[0][0]);
  m5= (a[0][0]+a[0][1])*b[1][1];
  m6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
  m7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

  c[0][0]=m1+m4-m5+m7;
  c[0][1]=m3+m5;
  c[1][0]=m2+m4;
  c[1][1]=m1-m2+m3+m6;

   printf("\nAfter multiplication using \n");
   for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",c[i][j]);
   }

   return 0;
}

C Program to Display same Source Code as Output

#include<stdio.h>

int main(){
    FILE *fp;
    char c;

    fp = fopen(__FILE__,"r");

    do{
         c= getc(fp);
         putchar(c);
    }
    while(c!=EOF);

    fclose(fp);

    return 0;
}
Output :

#include<stdio.h>

int main(){
    FILE *fp;
    char c;

    fp = fopen(__FILE__,"r");

    do{
         c= getc(fp);
         putchar(c);
    }
    while(c!=EOF);

    fclose(fp);

    return 0;
}

C Program to Multiply two Matrices using Recursion

#include<stdio.h>
#define MAX 10

void matMult(int [MAX][MAX],int [MAX][MAX]);
int r1,r2,c1,c2;
int c[MAX][MAX];

int main(){

    int a[MAX][MAX],b[MAX][MAX],i,j,k;

    printf("Enter the row and column of first matrix: ");
    scanf("%d%d",&r1,&c1);
    printf("Enter the row and column of second matrix: ");
    scanf("%d%d",&r2,&c2);

    if(c1!=o){
         printf("Matrix mutiplication is not possible");
    }
  else{

      printf("Enter the First matrix: ");
      for(i=0;i<r1;i++)
      for(j=0;j<c1;j++)
           scanf("%d",&a[i][j]);

      printf("Enter the Second matrix: ");
      for(i=0;i<r2;i++)
      for(j=0;j<c2;j++)
           scanf("%d",&b[i][j]);

      printf("nThe First matrix is: n");
      for(i=0;i<r1;i++){
      printf("n");
      for(j=0;j<c1;j++){
           printf("%dt",a[i][j]);
      }
      }

      printf("nThe Second matrix is: n");
      for(i=0;i<r2;i++){
      printf("n");
      for(j=0;j<c2;j++){
           printf("%dt",b[i][j]);
      }
      }

      matMult(a,b);

  }

  printf("nThe multiplication of two matrixes is: n");
  for(i=0;i<r1;i++){
      printf("n");
      for(j=0;j<c2;j++){
           printf("%dt",c[i][j]);
      }
  }
  return 0;
}

void matMult(int a[MAX][MAX],int b[MAX][MAX]){

    static int sum,i=0,j=0,k=0;

    if(i<r1) //row of first matrix
    {
      if(j<c2) //column of second matrix
      {
         if(k<c1)
         {
             sum=sum+a[i][k]*b[k][j];
             k++;
             matMult(a,b);
         }
         c[i][j]=sum;
             sum=0;
             k=0;
             j++;
             matMult(a,b);
      }
        j=0;
        i++;
        matMult(a,b);
    }
}
output :

Enter the row and column of first matrix: 2 2
Enter the row and column of second matrix: 2 2
Enter the First matrix: 1 2 3 4
Enter the Second matrix: 1 2 1 2

The First matrix is:

1       2
3       4
The Second matrix is:

1       2
1       2
The multiplication of two matrixes is:

3       6
7       14

C Program to implement Binary Search Tree Traversal

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>

typedef struct BST
{
    int data;
    struct BST *lchild,*rchild;
}node;

void insert(node *,node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *,int,node **);

void main()
{
 int choice;
 char ans='N';
 int key;
 node *new_node,*root,*tmp,*parent;
 node *get_node();
 root=NULL;
 clrscr();

 printf("nProgram For Binary Search Tree ");
 do
 {
   printf("n1.Create");
   printf("n2.Search");
   printf("n3.Recursive Traversals");
   printf("n4.Exit");
   printf("nEnter your choice :");
   scanf("%d",&choice);

   switch(choice)
   {
    case 1:
           do
             {
             new_node=get_node();

             printf("nEnter The Element ");
             scanf("%d",&new_node->data);

             if(root==NULL)   /* Tree is not Created */
                 root=new_node;
             else
                 insert(root,new_node);

             printf("nWant To enter More Elements?(y/n)");
             ans=getch();

             }while(ans=='y');

             break;

     case 2:
             printf("nEnter Element to be searched :");
             scanf("%d",&key);

             tmp = search(root,key,&parent);

             printf("nParent of node %d is %d",
                              tmp->data,parent->data);
             break;

    case 3:

            if(root==NULL)
                printf("Tree Is Not Created");
            else
               {
               printf("nThe Inorder display : ");
               inorder(root);
               printf("nThe Preorder display : ");
               preorder(root);
               printf("nThe Postorder display : ");
               postorder(root);
               }

            break;
    }
 }while(choice!=4);
}
/*
  Get new Node
*/
node *get_node()
 {
 node *temp;
 temp=(node *)malloc(sizeof(node));
 temp->lchild=NULL;
 temp->rchild=NULL;
 return temp;
 }
/*
  This function is for creating a binary search tree
*/
void insert(node *root,node *new_node)
{
  if(new_node->data < root->data)
     {
     if(root->lchild==NULL)
         root->lchild = new_node;
     else
         insert(root->lchild,new_node);
     }

  if(new_node->data > root->data)
     {
     if(root->rchild==NULL)
         root->rchild=new_node;
     else
         insert(root->rchild,new_node);
     }
}
/*
This function is for searching the node from
      binary Search Tree
*/
node *search(node *root,int key,node **parent)
{
 node *temp;
 temp=root;
    while(temp!=NULL)
    {
      if(temp->data==key)
         {
         printf("n The %d Element is Present",temp->data);
         return temp;
         }
      *parent=temp;

      if(temp->data>key)
         temp=temp->lchild;
      else
         temp=temp->rchild;
    }
 return NULL;
}
/*
This function displays the tree in inorder fashion
*/
void inorder(node *temp)
{
   if(temp!=NULL)
    {
    inorder(temp->lchild);
    printf("%d",temp->data);
    inorder(temp->rchild);
    }
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp)
{
 if(temp!=NULL)
    {
    printf("%d",temp->data);
    preorder(temp->lchild);
    preorder(temp->rchild);
    }
}

/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp)
{
 if(temp!=NULL)
    {
    postorder(temp->lchild);
    postorder(temp->rchild);
    printf("%d",temp->data);
    }
}
[468x15]
Output :
Program For Binary Search Tree
1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :1

Enter The Element 5

Do u Want To enter More Elements?(y/n)
Enter The Element 12

Do u Want To enter More Elements?(y/n)
Enter The Element 6

Do u Want To enter More Elements?(y/n)
Enter The Element 3

Do u Want To enter More Elements?(y/n)
1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :3

The Inorder   display  :   3  5  6  12
The Preorder  display  :   5  3  12  6
The Postorder display  :   3  6  12  5

1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :2

Enter Element to be searched :3

The 20 Element is Present
Parent of node 3 is 5