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;
}
|