#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#include<conio.h>
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 second vessel”;
cin>>ycapacity;
ans = steps(n);
cout<< “Steps Required:” <<ans;
getch();
}
Leave a Reply