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 . . .
This comment has been removed by the author.
ReplyDeleteHello, i do not understand this part
ReplyDeleteint min(int d, int f)
{
if (d < f)
return d;
else
return f;
}
this functioning is being used to find the minimum number between d and f.
Deleteif d is less than f then the function returns d as the smaller number or else, it return f as the minimum .
ohk thank you and also the program run an infinite loop when the input is false. Is it possible to avoid the loop and display that that data is invalid if so how?
DeleteHello, what does temp = min(ycapacity - y, x); means? I don't understand this part.
DeleteI had my own blog which has the solution for your answer. Check it out….
Deletehttps://coderhunt.blogspot.com/2019/02/water-jug-problem-using-breath-first.html
https://coderhunt.blogspot.com/2019/02/water-jug-problem-using-depth-first.html
u please explain all steps please
ReplyDeletehi i got the error....
ReplyDeleteSeverity Code Description Project File Line Suppression State
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?