PROBLEM 4 - WATER TANKS
There are n identical large cylindrical tanks for storing water. The tanks are arranged
in a circle on level ground. Each tank is connected with its two neighbours by means
of pipes situated at its base. There is a valve between each adjacent pair of tanks
(tank n is next to tank 1). All valves are initially closed. All the outlets and the pipes
are at the same level and are always full of water, even if all the tanks are deemed to
be “empty”.
The volume in any tank is measured by the height of the surface of the water above
the level of the top of the outlets. If all valves (or all valves but one) are opened so
that water can flow between the tanks, then the levels will eventually equalise.
Conversely, if all tanks are initially at the same level, no valves need be opened to
equalise the levels. Thus it may be necessary to only open some of the valves to
achieve this result.
For example, consider n=4 tanks each 5 metres high. Assume that the water level in
these tanks is at 4, 4, 3, and 3 meters respectively. Their water level will equalise if
we open the valves between tanks #2 and #3 and between #4 and #1, as suggested
by the following diagram. Thus for this set we need to open only two valves:
Given a set of initial heights, determine the minimum number of valves to open so
that the final water levels in all tanks is equal.
closed open closed open
to #4 to #1
#1 #2 #3 #4
P4 - ACM SPPC 2 of 2 Saturday, 20/09/2003
INPUT FORMAT 1
The input will consist of one or more scenarios, each scenario consisting of two lines.
The first line contains a descriptive title, which is a string of letters or spaces no more
than 200 characters long, containing at least 1 letter.
The second line starts with the number of basins n (3 £ n £ 200), a space, and then n
integers in the range 0 to 99, separated by single spaces, representing the water
levels in the tanks.
The scenarios sequence is terminated by a single ‘#’ character on a line by itself.
SAMPLE INPUT:
High four dude basins
4 4 4 3 3
The Australasian eight
8 2 1 1 2 2 1 1 6
#
OUTPUT FORMAT
Output one line for each input scenario. The line consists of the first letter of each
word in the descriptive title in upper case, followed by a colon (‘:’), a space, and then
the minimum number of valves that need to be open to achieve equal heights in all
tanks.
SAMPLE OUTPUT:
HFDB: 2
TAE: 5


i dont understand how the output is generated..
please help if anyone understand this....

Recommended Answers

All 28 Replies

i dont understand how the output is generated..
please help if anyone understand this....

Which part of the output? I glanced over the assignment. First, there's a math aspect of the problem. You need to model/solve that and that is independent largely of the C++ programming implementation. Second, once you've figured things out math-wise, you need to implement that solution via programming. Third, there is the "output" part which you speak of, but which part is giving you the problem?

in this assignment, the way how the whole process runs i.e., how it can be calculated that which valve should be open and the number is calculated..
how it can be understood that the level of tanks comes equal now..

in this assignment, the way how the whole process runs i.e., how it can be calculated that which valve should be open and the number is calculated..
how it can be understood that the level of tanks comes equal now..

OK, you're stuck on the math portion. You need to draw out some sketches till you get the feel of it. Start out with one tank. That's the trivial problem. If there's only one tank, no valves need to be opened. If there's two tanks, that's pretty trivial too. If the two tanks have the same amount of water, don't open a valve. If they don't, open the valve.

Three tanks stops being completely trivial. You need to draw diagrams. This program involves taking the mean volume of a group of tanks a lot. In your description, it says "as suggested by the following diagram", which we can't see here, so since this all involves diagrams and we'd have to draw and attach them to post here, chances are you'd be better off getting help face to face somewhere. It would take ten times as long to explain it on Daniweb than if you were sitting next to me. Good luck.

commented: Good explanation of the process +36

yeah this is true...
tell me one thing more.
In the first input 4 4 4 3 3, if 2 valves are open then the level will be 3.5 4 3.5 3.5 3.5. but in this case level is not equal....
can u expalin this thing to me..

I understand all the things but this thing is not clear that whether 3.5 4 3.5 3.5 3.5 means equal..

> The second line starts with the number of basins n (3 £ n £ 200), a space, and then n
> integers in the range 0 to 99, separated by single spaces,
> representing the water levels in the tanks.
You read the spec wrong

> In the first input 4 4 4 3 3, if 2 valves are open then the level will be 3.5 4 3.5 3.5 3.5.
You've got 5 tanks, not 4

First number is the number of tanks
4
The remaining n numbers (4 of them) are the tank heights
4 4 3 3

One 4 joins to one 3, and the other 4 joins to the other 3
Result: 2 valves, and all tanks level.

I m talking about the sample input and output mentioned in the last of the problem....

I m talking about the sample input and output mentioned in the last of the problem....

You are reading the spec incorrectly, as Salem says. You have four tanks originally at levels 4, 4, 3, an 3.

Thus, this makes no sense:

3.5 4 3.5 3.5 3.5

That's five numbers, meaning five tanks. In the output, the "2" represents the fact that you open 2 valves and get this for the heights:

3.5 3.5 3.5 3.5

Get rid of the 4. It's a little confusing since there are 4 tanks and two of the tanks start with height 4.

Use the other example. It's easier since there's less confusion. No tank has height 8:

8 2 1 1 2 2 1 1 6

Ignore the 8 regarding the tank heights. 8 is there to help you calculate the mean and to help you read from the file using C++ (8 is there so you know when to stop).

That's eight tanks, with these heights:

2 1 1 2 2 1 1 6

ok but how the calculation is done?
how it can be calculated that which valve will be open to equallize the level of tanks i.e, how can I calculate this...

Read the description again, they're arranged in a ring, each connected with it's neighbours.

> how it can be calculated that which valve will be open to equallize the level of tanks
I've no idea - that's the clever bit for you to work out (well not entirely true, I've an idea or two of where to start, but that's not the point is it).

Designing algorithms from analysing a problem is a key skill which has to be learnt. Nothing is served if we just blurt out the answer for you.

Create more examples, draw them out on paper, and work your way through them until you come up with a strategy for solving the problem.

When you can describe the algorithm on paper, you'll be a lot closer to solving the algorithm using program code.

I am working on designing algorithm for it...
but there are few difficulties in doing this..

I have made the program...:)
I have calculated only the minimum values and the get the solution..:)
I want u people to plz check mine logic...

#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
void main()
{
	clrscr();

	ifstream read("C:\\input.txt",ios::in);
	ofstream write("C:\\prob4.txt",ios::out);

	char * line = new char[50];
	char * input = new char[50];
	char data;
	int i = 0;
	char height[2];
	int count = 0;
	for(int k = 0;k<50;k++)
	{
		line[k] = 0;
		input[k] = 0;
	}
	while( !read.eof() )
	{
		i = 0;
		read>>data;
		if(data == '#')
        	break;
		line[i] = data;
		i++;

		while( 1 )
		{
			if( read.peek() == '\n')
				break;
			if( read.peek() == ' ')
			{
				read>>data;
				line[i] = data;
				i++;
			}
			read>>data;
		}

		write<<line;
		write<<':';

		i = 0;

		read>>height[0];
		height[1] = '\0';
		int h = atoi(height);
	 //   cout<<h;
		while( 1 )
		{

			if( read.peek() == '\n')
				break;
			if( read.peek() == ' ')
			{
				read>>data;
				input[i] = data;
				i++;
			}
		}
		input[i] = '\0';
		input[i+1] = '\0';
		int len = strlen(input);
		int j = 0;

		char min1;
		while(j != 10)
		{
			char min = input[j];
			for(int y = 1;y<len;y++)
			{
				if(min > input[y])
				{
					min = input[y];
					min1 = min;
					count++;
					break;
				}

			}
			j++;
		}
		if(input[len-1] < input[0] ||input[len-1] > input[0])
			count++;
	   // break;

	   write<<count;
	   write<<endl;
	   count = 0;
	}
	getch();
}

What logic is there to check? You make no attempt to figure out how many valves you need to open in this program.

And why are you storing the data as a character array? These are mathematical calculations. Store them as integers. Calculating a minimum or an average where 5 is stored as the ASCII value of 5 is a mistake.

i don't understand what u r saying....

i don't understand what u r saying....

I'm saying there is no function which tries to figure out how many valves need to be opened, no function that calculates the mean height of the water in the tanks, no attempt to implement any type of algorithm. This a math problem, so you're variables should be doubles, floats, or integers, not characters. You don't add, subtract, find the mean, stuff like that, using characters.

char * line = new char[50];
	char * input = new char[50];
	char data;
	char height[2];
char min = input[j];
			for(int y = 1;y<len;y++)
			{
				if(min > input[y])
				{
					min = input[y];
					min1 = min;
					count++;
					break;
				}

			}

You've defined and stored your data as characters, so '0' is stored not as 0, but as 48. '1' is stored as 49, so when you add, you get nonsense like 0 + 1 adding up to 48 + 49, which is 97, which is the ASCII value for I forget what, but it's not a number. When you do math calculations, you do them on integers, doubles, and floats, not characters, so your data needs to not be stored as characters. You need a complete program redesign.

when we read data from file is it necessary to take character array or we can take any other data type?
and I have used the logic in this program of finding minimum value.
where there is a minimum value there is a need of opening a valve.
Thats why I have calculated minimum value.....

Now tell me where do u find mistake in this code.....

when we read data from file is it necessary to take character array or we can take any other data type?
and I have used the logic in this program of finding minimum value.
where there is a minimum value there is a need of opening a valve.
Thats why I have calculated minimum value.....

Now tell me where do u find mistake in this code.....

You may very well need to calculate a minimum value. Depends on the algorithm. My overall point is that when you do math, you do it with floats, integers, and doubles or you are really courting disaster. You can either read the data in as characters, then convert it to integers, doubles, or floats, or you can read it in directly as integers, doubles, or floats. But however you do it, you need to STORE the data as numbers, not characters, and do your mathematical calculations on numbers, not characters.

If it was me, I'd read the numbers in directly as numbers, not characters. there's no need to convert. The NAMES of the tanks will have to be read in as characters or strings.

wat about the logic which I have used about calculating minimum??
Actually I don't know file handling as such I am trying to practice it that's why..
I thought only character array is used for this purpose...

wat about the logic which I have used about calculating minimum??

char min1;
		while(j != 10)
		{
			char min = input[j];
			for(int y = 1;y<len;y++)
			{
				if(min > input[y])
				{
					min = input[y];
					min1 = min;
					count++;
					break;
				}

			}
			j++;
		}

I don't know where you get the 10 in line 2. I don't know what min1 stands for. I'd get the data stored correctly first, THEN worry about minimums, maximums, means, and stuff like that. I can't get past the overall program design to judge a single part of it.

Actually I don't know file handling as such I am trying to practice it that's why..
I thought only character array is used for this purpose...

Nope. You can read in numbers directly as numbers using the >> operator. That's what you should do. The data file is perfect for that since the numbers are on one line, separated by spaces, and the words are on a different line. Use getline to get the name and read it into a string, then use the >> operator to get the numbers. Read the numbers in directly as numbers. Herre's an example.

ifstream ins;
ins.open ("input.txt");
string name;
const int MAX_NUM_TANKS = 20;
int tankHeight[MAX_NUM_TANKS];
int numTanks;
getline (ins, name, '\n');
ins >> numTanks;
for (int i = 0; i < numTanks; i++)
     ins >> tankHeight[i];
while(input[j] != '\0')
		{
			char min = input[j];
			for(int y = 1;y<len;y++)
			{
				if(min > input[y])
				{
					min = input[y];
					count++;
					break;
				}

			}
			j++;
		}

it is the actual code

when i try to read data in an integer array the ASCII of numbers are stored instead of the numbers itself..
For this purpose what should I do?
use integer variable instead of character?
And I have heard that data in a file is in the form of characters..
So how can this data be stored in an integer array as numbers itself....

when i try to read data in an integer array the ASCII of numbers are stored instead of the numbers itself..
For this purpose what should I do?

I gave you an example in my last post that shows how to read from your file into an integer array, and that works with your file format. It'll work fine. Don't try to change your code to work. Start over completely from scratch.

And I have heard that data in a file is in the form of characters..
So how can this data be stored in an integer array as numbers itself....

The >> operator figures all that out for you. It's pretty smart. It can see what your variable type is and it converts to it.

Type "C++ ifstream tutorial" into google and take some tutorials.

http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

Look at the term "arithmetic extractor" in this link. Here's the key sentence from the link.

The first group of member functions are arithmetic extractors. These read characters from the input data, and parse them to interpret them as a value of the specific type of its parameter. The resulting value is stored in the variable passed as parameter.

See the bold part. That's what I mean by it figuring it out for you and figures out how to convert. Yes, the text being read in starts as a character, but don't worry about that. By the time the >> has finished its job, it's converted the characters to the correct integer or float or double or whatever. It's all done for you.

I have tried reading data as u said it is working properly...:)
Now tell me about the calculation....!!
If u say I will mail u the modified program....

#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>

void main()
{
	clrscr();

	ifstream read("C:\\input.txt",ios::in);
	ofstream write("C:\\prob4.txt",ios::out);

	char * line = new char[50];
	int * input = new int[50];
	char data;
	int number;
	int i = 0;
	char height[2];
	int count = 0;
	/////////////INITIALIZATION/////////////////
	for(int k = 0;k<50;k++)
	{
		line[k] = 0;
		input[k] = 0;
	}

	/////////DATA READING FROM FILE////////////////
	while( !read.eof() )
	{
		i = 0;
		read>>data;
		if(data == '#')
			break;
		line[i] = data;
		i++;

		while( 1 )
		{
			if( read.peek() == '\n')
				break;
			if( read.peek() == ' ')
			{
				read>>data;
				line[i] = data;
				i++;
			}
			read>>data;
		}

		write<<line;
		write<<':';

		i = 0;

		read>>height[0];
		height[1] = '\0';
		int h = atoi(height);

		while( 1 )
		{

			if( read.peek() == '\n')
				break;
			if( read.peek() == ' ')
			{
				read>>number;
				input[i] = number;
				cout<<input[i]<<" ";
				i++;
			}
		}

		int j = 0;
		//////////////////CALCULATION//////////////////
		while(input[j] != '\0')
		{
			int min = input[j];
			for(int y = 1;y<i;y++)
			{
				if(min > input[y])
				{
					min = input[y];
					count++;
				}
			}
			j++;
		}
		if(input[i-1] < input[0] ||input[i-1] > input[0])
			count++;

	   write<<count;
	   write<<endl;
	   count = 0;
	}

	getch();
}

I have tried reading data as u said it is working properly...:)
Now tell me about the calculation....!!
If u said I will mail u the modified program....

That's a new thread. I'm not going to have time to help on that and probably by this point no one but me is reading this thread, so I'd mark it solved and start a new, updated thread. Good luck.

ok, I have almost solved it..
I 'll try to complete it.
but what if I need help?

ok, I have almost solved it..
I 'll try to complete it.
but what if I need help?

If you need help, start a new thread, post the updated code, explain the problem, etc.

can u tell me some other websites from where I can clear my concepts of file handling...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.