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();
}