In: Computer Science
Write a C++ program to calculate the time to drain a cylindrical water tank for an initial water height ranging from 1 to 10 feet (specifically for 1, 2, 4, 6, 8, and 10 ft). The tank has a radius (rt) of 2 feet and the drain radius (rd) is 0.3 inch. The gravitational constant (g) is 32.2 feet/sec2.
The formula for time to drain the tank is
time=(rtrd)2h/vavg
where, average velocity, vavg=0.5(2gh).5
In your program, assign the values to the variables rt, rd, and g. Prompt user to enter the value of h. The program should calculate the value of vavg, and then display time in hours.
Using MS Visual Studio, create a project folder called tank and a source code file tank.cpp.
Use the function pow() in the formulas and NOT sqrt().
Set up the output statements (cout) such that the one line display looks like the following:
Initial water height (ft.) = 1; Time to drain (hrs.) = 0.44
STEP 1: Analyze the Problem –
There is only one required output (time in hour of data type double) and one input (initial water height in ft of data type int). All intermediate variables will be assigned a data type of double.
STEP 2: Develop a Solution –
Pseudo code is shown below.
Convert all input data to the same unit (ft)
Display a prompt to enter initial height of water
Read a value for the height
Calculate average velocity, using the given formulas
Display the calculated time
Hand calculation for h = 1 ft, rt = 2 ft, rd = 0.3 inch, and g = 32.2 ft/sec2:
time = ?
STEP 3: Code the Solution -
You can start the program as shown below:
// Filename:
// C++ program to calculate time to drain water tank
// Written by: Your Name ON: Date
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int h;
double rt = 2.0, rd = 0.3, g = 32.2;
double vavg, time;
// Input initial water height (h)
//---
// Set output formats
cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);// Calculate average velocity in ft/hr
//---
// Calculate time in hrs to drain
// NOTE: The function pow(double a, int n) returns an
//---
// Display results in the specified format
//---
return 0;
}
STEP 4: Test and Correct the Program -
If the program shows the correct output, copy and paste the output into MS Word. Also copy and paste the source code to the word processor as well. To copy the display window contents, click on the small black box in the upper left corner, select Edit -> Select All, then click again on the box and select Edit -> Copy.
Test the code by changing initial heights to 2, 4, 6, 8, and 10 feet.
Below is the code in C++:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int h;
double rt = 2.0, rd = 0.3, g = 32.2;
double vavg, time;
// Input initial water height (h)
cout << "Enter the initial water height (h) : ";
cin >> h;
// Set output formats
cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(2);
// Calculate average velocity in ft/hr
// multiplied by 3600 to convert speed from ft/sec to ft/hr
vavg = 0.5 * pow(2 * g * h, 0.5) * 3600.0;
// Calculate time in hrs to drain
time = pow((rt * 12 / rd), 2) * h / vavg;
// Display results in the specified format
cout << "Initial water height (ft.) = " << h << "; Time to drain (hrs.) = " << time << "\n";
return 0;
}
Screenshot of the code for proper indentation:
OUTPUT:
Enter the initial water height (h) : 1
Initial water height (ft.) = 1; Time to drain (hrs.) = 0.44
Screenshot of the output:
**NOTE: If you understood the solution, please upvote the solution as it means a lot. If you have any doubt feel free to ask in the comment section below. Thanks!!