urgently need help trying to get this equation to work in c++?

closed account (3A5XSL3A)
I can't seem to get the right answer.
I'm supposed to be calculating the volume of water in a water tower.
The "truncated cone" formula is 1/3 * π * ( r1^2 + r1*r2 + r2^2) * h
r1= 15
r2= 25

height is a constant 3.1415926

the angle of the conical part is 45 degrees. I have no idea what that means and how/if i'm supposed to incorporate that into the equation. my rubric says "The angle of the conical parts is 45°, so the change in radius of the conical parts is proportional to the height in that part."

this is what the "water tower" looks like: http://postimg.org/image/yzfqehfcn/

I know i'm supposed to use 1.0/3.0 for the equation, and I know to get the full volume up to the truncated cone part, I have to add the volume amount up to it. I have written out the formula in various ways and I can't seem to get the correct answer. If someone could help me, I would really appreciate it or at least point me in the right direction.
I have tried all of these, none of them work. The +14137.2 at the end of a couple of them is the volume of the first part of the water tower.

cone1 = (1.0/3.0)*(PI*((r1*r1)+r1*r2+(r2*r2))*height);
cone1 = (1.0/3.0) * PI * (225+15*25+625) * height;
cone1 = (1.0/3.0)*(PI*(pow(2,r1)+r1*pow(2,r1)+r2)*height)+14137.2;
cone1 = (1.0/3.0)*3.1415926*(156000)*height+14137.2;
To find the volume of a frustum of a cone:

V = ((π * h) / 3) * (R² + Rr + r²)


Where π is pi
h is the height
R is the radius of the lower base
r is the radius of the upper case

Give that a try.

In addition, make sure that the calculation inaccuracies aren't due to a floating point number being cast to an integer.
Last edited on
closed account (3A5XSL3A)
thank you, this gave me an answer closer to the correct answer but it still did not work.
I did : cone1 = ((PI * height)/3) * (r1*r1)+(r2*r1)+(r2*r2)+14137.2;

the +14137.2 is the volume of the bottom part of the water tower that must be added to the cone's volume in order to get the right answer.
The correct answer when 25.5 is the height: 19624.6
My answer when 25.5 is the height: 21145.5

I have no idea where i'm going wrong. could it be something about the 45 degree angle?
Last edited on
closed account (3A5XSL3A)
My rubric says this: "The angle of the conical parts is
45°, so the change in radius of the conical parts is proportional to the height in that part" I have no idea what this means.
HERE is my code:

#include <iostream>
#include <cmath>
using namespace std;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main() 
{ 
const float PI = 3.1415926, r1 = 15, r2 = 25; 
float height, cylinder1, cylinder2, cone1, cone2; 

cout << "Enter height of water in tower: "; 
cin >> height; 
cylinder1 = PI*(r1*r1)*height; 
cone1 = ((PI * height)/3.0) * (r1*r1)+(r2*r1)+(r2*r2); 
cylinder2 = PI*(r2*r2)*height; 
cone2 = ((PI * height)/3.0) * (r1*r1)+(r2*r1)+(r2*r2); 

if (height>0 && height<=20) 
cout << "With a height of " << height << " feet, the volume is " << cylinder1 << " cubic feet."; 

else if (height>20 && height<=30) 
cout << "With a height of " << height << " feet, the volume is " << cone1 << " cubic feet."; 

else if (height>30 && height<=40) 
cout << "With a height of " << height << " feet, the volume is " << cylinder2 << " cubic feet." << endl; 

else if (height>40 && height<=50) 
cout << "With a height of " << height << " feet, the volume is " << cone2 << " cubic feet."; 

return 0; 
} 


I am having trouble with the equations. I know to get the full volume of the water tower as the height gets higher, I must add the volume of water in the previous sections of the tower, however my result is wrong so I have no done this step.

On my rubric it says:
The correct answer when 25.5 is the height: 19624.6
HOWEVER My answer when 25.5 is the height: 7008.3
And when I add the volume of the first 20 feet of the cylinder, it is still the wrong answer. I have no idea what i'm doing wrong, I guess it must be the 45 degree angle, but I have no idea what that means.
Help would be so appreciated.

cone1 and cone2 as well as cylinder1 and cylinder2 are assigned the same equation because I have not yet added the volume of the previous sections to the end of equations since my results are wrong.
I'm confused. Your rubric says "When the height is 25.5", however, your diagram has the height labeled as 50 (10 + 10 + 10 + 20).
?

*EDIT* the height of what exactly? The whole tower? One of the segments? Perhaps, the actual water level (does the water level need to occupy all of the water tower, or can the water tower be half full)?
Last edited on
closed account (3A5XSL3A)
the "when height is 25.5" means when I run the program and input the height as 25.5, then the program will calculate the volume of water up to the height 25.5, meaning it will calculate the first 20 feet of the tower, plus another 5.5 feet of the next part of the tower (which is now a frustum cone)

I actually re-wrote the equations a bit to where I think they are correct in the way I formatted them, however the equation themselves are not correct, because I dont get the correct result when the program run.

here is my new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	//Declaring variables
	const double PI = 3.1415926, r1 = 15, r2 = 25;
	double height, cylinder1, cylinder2, cone1, cone2;
	//Asking user to input height
	cout << "Enter height of water in tower: ";
	cin >> height;
	
	//Equations to compute the volume of the water, given the height. However, after the first equation,
	//the rest do not correspond with the correct volume.
//In these equations (after the first one) the extra parts are to compute and add the volume of the previous section to the volume that is being computed given the height.
	cylinder1 = PI*(r1*r1)*height;
	cone1 = ((PI * height)/3.0) * (r1*r1)+(r2*r1)+(r2*r2) + (PI*(r1*r1)*20);
	cylinder2 = (PI*(r2*r2)*height) + (PI*(r1*r1)*20) + ((PI * 10)/3.0) * (r1*r1)+(r2*r1)+(r2*r2);
	cone2 = ((PI * height)/3.0) * (r1*r1)+(r2*r1)+(r2*r2) + (PI*(r1*r1)*20) + ((PI * 10)/3.0) * (r1*r1)+(r2*r1)+(r2*r2);

	//if/else if statements to calculate and output volume given the height
	if (height>0 && height<=20)
		cout << "With a height of " << height << " feet, the volume is " << cylinder1 << " cubic feet.";

	else if (height>20 && height<=30)
		cout << "With a height of " << height << " feet, the volume is " << cone1 << " cubic feet.";

	else if (height>30 && height<=40)
		cout << "With a height of " << height << " feet, the volume is " << cylinder2 << " cubic feet." << endl;

	else if (height>40 && height<=50)
		cout << "With a height of " << height << " feet, the volume is " << cone2 << " cubic feet.";

	return 0;
}

Last edited on
There's a fundamental problem with the way you're doing this, let me explain.

Your equations to calculate the volumes of each respective shape (cylinder or frustum of cone) make use of the height variable. The problem with that is that, the height variable that's being used isn't actually the height of that particular segment of the tower, but the height of the water level.

I'll try to post a simple example here, as well as a more complex example of performing these calculations using classes soon.
xismn, actually, he's almost there, he uses height for the topmost segment of the tower, which is admittedly wrong, but quite close.

1
2
3
4
cylinder1 = PI*(r1*r1)*height;
cone1 = ((PI * height)/3.0) * (r1*r1)+(r2*r1)+(r2*r2) + (PI*(r1*r1)*20);
cylinder2 = (PI*(r2*r2)*height) + (PI*(r1*r1)*20) + ((PI * 10)/3.0) * (r1*r1)+(r2*r1)+(r2*r2);
cone2 = ((PI * height)/3.0) * (r1*r1)+(r2*r1)+(r2*r2) + (PI*(r1*r1)*20) + ((PI * 10)/3.0) * (r1*r1)+(r2*r1)+(r2*r2);


OK, this isn't working, no surprises there, as well as using the height variable wrong, you need to say BODMAS in your head 10 times then look at your cone equations.

try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
        //Declaring variables
        const double PI = 3.1415926, r1 = 15, r2 = 25;
        double height, cylinder1, cylinder1_height, cylinder2, cylinder2_height, cone1, cone1_height, cone2, cone2_height;
        //Asking user to input height
        cout << "Enter height of water in tower: ";
        cin >> height;

        //Equations to compute the volume of the water, given the height. However, after the first equation,
        //the rest do not correspond with the correct volume.
        //In these equations (after the first one) the extra parts are to compute and add the volume of the previous section to the volume that is being computed given the height.

        /* Calculate the volume of cynlinder 1*/
        cylinder1_height = (height > 20.0) ? 20.0 : height;
        cylinder1 = PI*(r1*r1)*cylinder1_height;

        /* Calculate the volume of cylinder 1 + cone 1 */
        cone1_height = (height > 30.0) ? 10.0 : height - 20.0;
        cone1 = (PI * cone1_height /3.0) * ((r1*r1)+(r2*r1)+(r2*r2)) + cylinder1;

        /* Calculate the volume of cylinder 1 + cone 1 + cylinder 2 */
        cylinder2_height = (height > 40.0) ? 10.0 : height - 30.0;
        cylinder2 = (PI*(r2*r2)*cylinder2_height) + cone1;

        /* Calculate the volume of cone 2 + cylinder 1 + cone 1 + cylinder 2 */
        cone2_height = (height > 50.0) ? 10.0 : height - 50.0;
        cone2 = (PI * cone2_height /3.0) * ((r1*r1)+(r2*r1)+(r2*r2)) + cylinder2;

        //if/else if statements to calculate and output volume given the height
        if (height>0 && height<=20)
                cout << "With a height of " << height << " feet, the volume is " << cylinder1 << " cubic feet.\n";

        else if (height>20 && height<=30)
                cout << "With a height of " << height << " feet, the volume is " << cone1 << " cubic feet.\n";

        else if (height>30 && height<=40)
                cout << "With a height of " << height << " feet, the volume is " << cylinder2 << " cubic feet.\n";

        else if (height>40 && height<=50)
                cout << "With a height of " << height << " feet, the volume is " << cone2 << " cubic feet.\n";

        return 0;
}


This gets us closer,
With a height of 10 feet, the volume is 7068.58 cubic feet.
With a height of 25.5 feet, the volume is 21192.7 cubic feet.


Again, part 1 is fine, the remaining parts are not working... so what's wrong?

The problem is in your equations.

A cone with an angle of 45 degrees has the same height as it has radius, to calculate the volume of a partial cone with an angle of 45 degrees you would do this:

PI/3 * (h*h*h-r*r*r)

So, your equations fixed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
        //Declaring variables
        const float PI = 3.1415926, r1 = 15, r2 = 25;
        float h, height, cylinder1, cylinder2, cone1, cone2;
        //Asking user to input height
        cout << "Enter height of water in tower: ";
        cin >> height;

        //Equations to compute the volume of the water, given the height. However, after the first equation,
        //the rest do not correspond with the correct volume.
        //In these equations (after the first one) the extra parts are to compute and add the volume of the previous section to the volume that is being computed given the height.

        /* Calculate the volume of cynlinder 1*/
        h = (height > 20.0) ? 20.0 : height;
        cylinder1 = PI*(r1*r1)*h;

        /* Calculate the volume of cylinder 1 + cone 1 */
        h = (height > 30.0) ? 25.0 : height - 5.0;
        cone1 = (PI / 3.0) * ((h*h*h)-(r1*r1*r1)) + cylinder1;

        /* Calculate the volume of cylinder 1 + cone 1 + cylinder 2 */
        h = (height > 40.0) ? 10.0 :  height - 30.0;
        cylinder2 = (PI*(r2*r2)*h) + cone1;

        /* Calculate the volume of cone 2 + cylinder 1 + cone 1 + cylinder 2 */
        h = (height > 50.0) ? 25.0 : height - 25.0;
        cone2 = (PI / 3.0) * ((h*h*h)-(r1*r1*r1)) + cylinder2;

        //if/else if statements to calculate and output volume given the height
        if (height>0 && height<=20)
                cout << "With a height of " << height << " feet, the volume is " << cylinder1 << " cubic feet.\n";

        else if (height>20 && height<=30)
                cout << "With a height of " << height << " feet, the volume is " << cone1 << " cubic feet.\n";

        else if (height>30 && height<=40)
                cout << "With a height of " << height << " feet, the volume is " << cylinder2 << " cubic feet.\n";

        else if (height>40 && height<=50)
                cout << "With a height of " << height << " feet, the volume is " << cone2 << " cubic feet.\n";

        return 0;
}


With a height of 10 feet, the volume is 7068.58 cubic feet.
With a height of 20 feet, the volume is 14137.2 cubic feet.
With a height of 25.5 feet, the volume is 19624.6 cubic feet.
Last edited on
ValliusDax saves the day! :)
@xsimn, And he takes a bow. ;~)
Topic archived. No new replies allowed.