Stats

Tuesday, 13 May 2014

                 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 . . .




8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello, i do not understand this part
    int min(int d, int f)
    {
    if (d < f)
    return d;
    else
    return f;
    }

    ReplyDelete
    Replies
    1. this functioning is being used to find the minimum number between d and f.
      if d is less than f then the function returns d as the smaller number or else, it return f as the minimum .

      Delete
    2. 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?

      Delete
    3. Hello, what does temp = min(ycapacity - y, x); means? I don't understand this part.

      Delete
    4. I had my own blog which has the solution for your answer. Check it out….

      https://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

      Delete
  3. u please explain all steps please

    ReplyDelete
  4. hi i got the error....
    Severity 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?

    ReplyDelete