Stats

Tuesday, 13 May 2014

                                        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

Thursday, 2 August 2012

C program for electricity bill


#include<iostream>
using namespace std;
#define cost1 0.60
#define cost2 0.80
#define cost3 0.90
#define min 50
#include<conio.h>
void main()
{
char name[10][20];
float amount[10];
int units[10],n;

cout<<"Enter Number of Customers: ";
cin>>n;

cout<<"Enter Names and Number of Units Consumed\n";
for(int i=0;i<n;i++)
{
cout<<"-------------------------\n";
cout<<"Enter Customer Name: ";
cin>>name[i];
cout<<"Enter Number of units= ";
cin>>units[i];
cout<<"-------------------------\n";

if(units[i]<=100)
{
amount[i]=(units[i]*cost1)+min;
}
else if(units[i]>100&&units[i]<=300)
{
amount[i]=(((units[i]-100)*(cost2))+(100*cost1))+min;

}
else if(units[i]>300)
{
amount[i]=(((units[i]-300)*(cost3))+(200*cost2)+(100*cost1))+min;
amount[i]=amount[i]+(amount[i]*(15.0/100.0));
}
}

cout<<"**************BILL**************\n";
cout<<"Name\t\t"<<"Units\t\t"<<"Amount\n";
for(int i=0;i<n;i++)
{
cout<<name[i]<<"\t\t"<<units[i]<<"\t\t"<<"Rs."<<amount[i];
cout<<"\n";
}
getch();
}









C program to implement Newton Raphson method


1)F(x)=x^2-3x+2

#include<stdio.h>
#include<math.h>
#include<conio.h>
#define e 0.001
#define F(x) (2*x)-3
float frac(float a)
{
       float f1;
       f1=a*a-3*a+2;
       return f1;
}
int main()
{
       float x1,x2,f1=0,f2,er,d;
       printf("F(x) = x^2-3x+2\n\n");
       printf("Enter the value of x1: ");
       scanf("%f",&x1);
       printf("\nx1 = %f",x1);
       printf("\n________________________________________________________________________________\n");
        
       printf("     x1      |       x2      |      f1       |       f'1      |  |(x2-x1)/x2|  |  \n");
       printf("--------------------------------------------------------------------------------\n");
       do
       {
              f1=frac(x1);
              d=F(x1);
              x2=x1-(f1/d);
              er=fabs((x2-x1)/x2);
           printf("  %f   |    %f   |     %f  |     %f  |      %f  |   \n",x1,x2,f1,d,er);
              x1=x2;
       }
       while(er>e);
       printf("--------------------------------------------------------------------------------\n\n");
       printf("\n  Root of the equation is: %f",x2);
       getch();
}