Tuesday, 31 December 2013

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