Stats

Tuesday, 13 May 2014

                           TOWER OF HANOI C++ CODE

#include <iostream>
#include <conio.h>
using namespace std;

void tower(int a, char from, char aux, char to)
{
if (a == 1)
{
cout << "Move disc 1 from " << from << " to " << to << "\n";
return;
}
else
{
tower(a - 1, from, to, aux);
cout << "Move disc " << a << " from " << from << " to " << to << "\n";
tower(a - 1, aux, from, to);
}
}

void main()
{
int n;
cout << "Tower of Hanoi\n";
cout << "Enter number of discs : ";
cin >> n;
cout << "\n";
tower(n, 'A', 'B', 'C');
getch();
}

OUTPUT
Tower of Hanoi
Enter number of discs : 3

Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C

Move disc 1 from A to C

                 WATER JUG PROBLEM C++ CODE-II

#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int xcapacity;
int ycapacity;
void display(int a, int b,int s);
int min(int d, int f)
{
if (d < f)
return d;
else
return f;
}
int steps(int n)
{
int x = 0, y = 0, step = 0;
int temp;
cout << setw(60) << " Vessel A        Vessel B       Steps" << endl;
while (x != n )
{
if (x == 0)
{
x = xcapacity;
step += 1;
cout << "Fill X     "; display(x, y,step);
}
else if (y == ycapacity)
{
y = 0;
step++; 
cout << "Empty Y    "; display(x, y,step);
}
else
{
temp = min(ycapacity - y, x);
y = y + temp;
x = x - temp;
step++;
cout << "Pour X in Y"; display(x, y, step);

}
}
return step;
}
void display(int a, int b,int s)
{
cout << setw(16) << a << setw(15) << b << setw(15)<<s<<endl;
}
void main()
{
int n, ans;
cout << "Enter the liters(GOAL) of water required to be filled in Vessel 1:";
cin >> n;
cout << "Enter the capacity of the vessel: ";
cin >> xcapacity;
cout << "Enter the capacity of the second vesssel  ";
cin >> ycapacity;
ans = steps(n);
cout << "Steps Required:" << ans;
        system("pause");
}

OUTPUT

Enter the liters(GOAL) of water required to be filled in Vessel 1:2
Enter the capacity of the vessel: 4
Enter the capacity of the second vesssel  3
                        Vessel A        Vessel B       Steps
Fill X                    4              0              1
Pour X in Y               1              3              2
Empty Y                   1              0              3
Pour X in Y               0              1              4
Fill X                    4              1              5
Pour X in Y               2              3              6
Steps Required:6Press any key to continue . . .




                                        WATER JUG PROBLEM C++ CODE

#include<iostream>
using namespace std;
#include<conio.h>
int in1[20], in2[20];
void main()
{
float J1, J2, j1 = 0, j2 = 0, j1_max, j2_max, J1_goal;
int i, j, choice, pos = 0;
in1[0] = 4;
in2[0] = 3;
pos++;
in1[pos] = 0;
in2[pos] = 0;
pos++;
cout << "Enter max volume of JUG1: ";
cin >> j1_max;
cout << "Enter max volume of JUG2: ";
cin >> j2_max;
cout << "Enter Goal state for JUG1: ";
cin >> J1_goal;
cout << "\nInitial State: 0   0";
cout << "\nFinal State: " << J1_goal << "\t n";;
cout << "\nJUG1   JUG2 ";
choice = 1;
cout << "\n  " << j1 << " " << j2;
while (1)
{
J1 = j1;
J2 = j2;
switch (choice)
{
case 1:
{
if (j1 < j1_max)
{ j1 = j1_max; }
choice++;
}
break;
case 2:
{
if (j2 < j2_max)
{ j2 = j2_max; }
choice++;
}
break;
case 3:
{
if (j1>0)
{j1 = 0; }
choice++;
}
break;
case 4:
{
if (j2 > 0)
{ j2 = 0;}
choice++;
}
break;
case 5:
{
if ((j1 + j2 >= j1_max) && (j2 > 0))
{
j2 = j2 - (j1_max - j1);
j1 = j1_max;
}
choice++;
}
break;
case 6:
{
if ((j1 + j2 >= j2_max) && (j1 > 0))
{
j1 = j1 - (j2_max - j2);
j2 = j2_max;
}
choice++;
}
break;
case 7:
{
if ((j1 + j2 <= j1_max) && (j2 > 0))
{
j1 = j1 + j2;
j2 = 0;
}
choice++;
}
break;
case 8:
{
if ((j1 + j2 <= j2_max) && (j1 > 0))
{
j2 = j1 + j2;
j1 = 0;
}
choice++;
}
case 9:
{
if ((j2 == J1_goal) && (j1 == 0))
{
j2 = j1;
j1 = J1_goal;
}
choice++;
}
default:
{
choice = 1;
}
}
for (i = 0; i < pos; i++)
{
if ((in1[i] == j1) && (in2[i] == j2))
{
j1 = J1;
j2 = J2;
break;;
}
}
if (i == pos)
{
in1[pos] = j1;
in2[pos] = j2;
pos++;
cout << "\n  " << j1 << " " << j2;
}
if (j1 == J1_goal)
{
cout << "\nDONE";
exit(1);
}
}
getch();
}

OUTPUT

Enter max volume of JUG1: 4
Enter max volume of JUG2: 3
Enter Goal state for JUG1: 2

Initial State: 0   0
Final State: 2   n
JUG1   JUG2
  0      0
  4      0
  1      3
  0      3
  3      0
  3      3
  4      2
  0      2
  2      0
DONEPress any key to continue . . .

DEPTH FIRST SEARCH (DFS) & BREADTH FIRST SEARCH (BFS) C++ CODE

#include<iostream> 
#include<conio.h>
using namespace std;

int cost[10][10], k, qu[10], front, rare, visit[10], visited[10], i, j, v, stk[10], top,m,n;
void bfs()
{
cout <<"\nEnter no of vertices: ";
cin >> n;
cout <<"Enter no of edges: ";
cin >> m;
cout <<"\nEnter Edges \n";
for (k = 1; k <= m; k++)
{
cin >> i >> j; 
cost[i][j] = 1; //ASSUME COST BETWEN EDGES i & j IS 1
}
cout <<"Enter Initial Vertex: ";
cin >> v;
cout <<"Visited Vertices\n";
cout << v;
visited[v] = 1;
k = 1;
while (k < n)
{
for (j = 1; j <= n; j++)
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1; qu[rare++] = j;
}
v = qu[front++];
cout << v << " ";
k++;
visit[v] = 0;
visited[v] = 1;
}
}
void dfs()
{
cout << "Enter no of vertices: ";
cin >> n;
cout << "Enter no of edges: ";
cin >> m;
cout << "\nEnter Edges \n";
for (k = 1; k <= m; k++)
{
cin >> i >> j;
cost[i][j] = 1;
}
cout <<"Enter Initial Vertex: ";
cin >> v;
cout <<"Visited Vertices\n";
cout << v << " ";
visited[v] = 1;
k = 1;
while (k < n)
{
for (j = n; j >= 1; j--)
if (cost[v][j] != 0 && visited[j] != 1 && visit[j] != 1)
{
visit[j] = 1;
stk[top] = j;
top++;
}
v = stk[--top];
cout << v << " ";
k++; visit[v] = 0;
visited[v] = 1;
}
}

void main()
{
int choice;
cout <<"1.DFS\n2.BFS";
cout <<"\nSelect Method: ";
cin >> choice;
if (choice == 1)
dfs();
else
bfs();
getch();
}

OUTPUT

1.DFS
2.BFS
Select Method: 1
Enter no of vertices: 6
Enter no of edges: 6

Enter Edges
1
2
1
3
1
4
2
5
2
6
3
6
Enter Initial Vertex: 1
Visited Vertices
1 2 5 6 3 4
1.DFS
2.BFS
Select Method: 2

Enter no of vertices: 6
Enter no of edges: 6

Enter Edges
1
2
1
3
1
4
2
5
2
6
3
6
Enter Initial Vertex: 1
Visited Vertices
12 3 4 5 6

Sunday, 23 December 2012

INHERITANCE PROGRAM EXAMPLE


#include<iostream>
using namespace std;
#include<conio.h>
#include<string.h>
class student
{
protected:
int roll_no;
char name[20];
public:
void details()
{
cout<<"enter roll number and name of the student:\n";
cin>>roll_no>>name;
}
};
class subject: public student
{
protected:
int thm,prm;
public:
void tp()
{
cout<<"\ntheory marks: ";
cin>>thm;
cout<<"\npractical marks: ";
cin>>prm;
}
};
class sports
{
protected:
int spm;
public:
void sp()
{
cout<<"\nsports marks: ";
cin>>spm;
}
};
class result:public subject,public sports
{
protected:
int total;
public:
int t()
{
total=thm+prm+spm;
return total;
}
void res()
{
cout<<"\n\nName"<<"      "<<"Roll No."<<"   "<<"Theory"<<"   "
<<"Practical"<<"   "<<"Total"<<endl;
cout<<name<<"    "<<roll_no<<"         "<<thm<<"         "
<<prm<<"         "<<total;
}
};
void main()
{
result r;
r.details();
r.tp();
r.sp();
r.t();
r.res();
getch();
}

Output

24 Namrata

theory marks: 89

practical marks: 45

sports marks: 23


Name      Roll No.   Theory   Practical   Total
Namrata    24         89         45         157




Friday, 7 September 2012

Gauss Elimination Method


# include<stdio.h>
# include<conio.h>
void main()
{
 int i,j,k,n;
 float a[10][10],x[10];
 float s,p; 
 printf("Enter the number of equations : ");
 scanf("%d",&n) ;
 printf("\nEnter the co-efficients of the equations :\n\n");
 for(i=0;i<n;i++)
 {
  for(j=0;j<n;j++)
  {
   printf("a[%d][%d]= ",i+1,j+1);
   scanf("%f",&a[i][j]);
  }
  printf("d[%d]= ",i+1);
  scanf("%f",&a[i][n]);
 }
 for(k=0;k<n-1;k++)
 {
  for(i=k+1;i<n;i++)
  {
   p = a[i][k] / a[k][k] ;
   for(j=k;j<n+1;j++)
    a[i][j]=a[i][j]-p*a[k][j];
  }
 }
 x[n-1]=a[n-1][n]/a[n-1][n-1];
 for(i=n-2;i>=0;i--)
 {
  s=0;
  for(j=i+1;j<n;j++)
  {
   s +=(a[i][j]*x[j]);
   x[i]=(a[i][n]-s)/a[i][i];
  }
 }
 printf("\nThe result is:\n");
 for(i=0;i<n;i++)
  printf("\nx[%d]=%f",i+1,x[i]);
 getch();

}

gauss seidel method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.01

void main() {
  int i, j, k, n;
  float a[10][10], x[10];
  float sum, temp, error, big;
  printf("Enter the number of equations: ");
  scanf("%d", & n);
  printf("Enter the co-efficients of the equations: \n");
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n + 1; j++) {
      printf("a[%d][%d]= ", i, j);
      scanf("%f", & a[i][j]);
    }
  }
  for (i = 1; i <= n; i++) {
    x[i] = 0;
  }
  do {
    big = 0;
    for (i = 1; i <= n; i++) {
      sum = 0;
      for (j = 1; j <= n; j++) {
        if (j != i) {
          sum = sum + a[i][j] * x[j];
        }
      }
      temp = (a[i][n + 1] - sum) / a[i][i];
      error = fabs(x[i] - temp);
      if (error > big) {
        big = error;
      }
      x[i] = temp;
      printf("\nx[%d] =%f", i, x[i]);
    }
    printf("\n");
  }
  while (big >= e);
  printf("\n\nconverges to solution");
  for (i = 1; i <= n; i++) {
    printf("\nx[%d]=%f", i, x[i]);
  }
  getch();
}

Enter the number of equations: 3
Enter the co-efficients of the equations:
a[1][1]= 2
a[1][2]= 1
a[1][3]= 1
a[1][4]= 5
a[2][1]= 3
a[2][2]= 5
a[2][3]= 2
a[2][4]= 15
a[3][1]= 2
a[3][2]= 1
a[3][3]= 4
a[3][4]= 8

x[1] =2.500000
x[2] =1.500000
x[3] =0.375000

x[1] =1.562500
x[2] =1.912500
x[3] =0.740625

x[1] =1.173437
x[2] =1.999688
x[3] =0.913359

x[1] =1.043477
x[2] =2.008570
x[3] =0.976119

x[1] =1.007655
x[2] =2.004959
x[3] =0.994933

x[1] =1.000054
x[2] =2.001995
x[3] =0.999474


converges to solution
x[1]=1.000054
x[2]=2.001995
x[3]=0.999474