# Using functions (subroutines)

• 03-14-2003
DoItAllMom115
Using functions (subroutines)
I need to write a program to calculate time it takes (in increments of 10 seconds) to drain a tank of radius 2ft and height3ft
I have to use a function to get full credit
I am having major trouble, can anyone help?

Code:

``` #include <iostream> #include <cstdlib> #include <cmath> using namespace std; //Project 3 - Draining a Tank //Name function for calculating volume lost float calcVolLost(int height); int main (void) {  const double pi=3.14159;  float volume, VolumeLost, VelocityJet, DiaJet;  int time=0, height=3, RadiusTank=2; //set flags for formatting  cout.setf(ios::left);  cout.setf(ios::adjustfield);  cout.setf(ios::showpoint);  cout.setf(ios::fixed);  cout.precision(3);  cout <<"Time"<<"Height of Fluid"<<"Velocity of Jet"<<"Diameter of Jet"<<endl; //Subtract volume lost and calculate new height  while (height <=0){  float calcVolLost(int height);  volume=volume-VolumeLost;  height=height-(volume/(pi*RadiusTank*RadiusTank));  cout<<time<<height<<VelocityJet<<DiaJet<<endl; } return EXIT_SUCCESS; } //function for calculating volume lost  float calcVolLost(int height) { i nt time;  const double pi=3.14159;  float c=0.95, float AreaJet, float DiaJet,float VelocityJet, float  VolumeLost, float RadiusJet=0.1667; AreaJet=pi*RadiusJet*RadiusJet; VelocityJet=sqrt(2*32.2*height); VolumeLost=10*VelocityJet*c*AreaJet; time=time+10; return VolumeLost; }```
:confused: See next reply which gives the problem...
• 03-14-2003
DoItAllMom115
Here is the assignment
Project 3: Draining a Tank
EECS 1530/EECS 1050
Due: Tuesday March 18, 2003
Description
Your program will be used to determine a solution to the flow rate out of a tank of liquid. We will do an approximation rather than solve the formula in a continuous form. A local engineering fraternity has a tank of fuel oil that they wish to drain. They know the tank is cylindrical with a diameter of 4 feet and the 2 inch diameter spout to drain it is at the bottom. The initial level of the fuel oil is 3 feet above the bottom of the tank. Now, the engineering students don’t want to sit around while it drains, they wish to determine approximately how long the tank will take to drain. This will be governed by a pair of equations. The first equation determines the diameter of the “jet” of fuel oil that will come out of the spout. This is:

Where C is a constant, in this case 0.95, ASpout is the area of the spout, and VJet is the velocity of the jet of fluid exiting the tank. To determine the VJet value there is a separate equation:

So, as the fuel oil drains from the tank the height will drop, the velocity of the outgoing jet of fluid will drop, and we will need to take this into account. Rather than do a continuous solution of this problem the engineering students have decided to assume there is little difference over the period of 10 seconds and to recompute the height and the flow rate after each 10 seconds. Your program should print a table with columns for time, height of fluid in the tank, velocity of the jet, and Q. The program should exit when the height reaches 0 or goes negative.

Example Calculation:
The original height is 3’ and the diameter is 4’ so the original volume of fuel oil in the tank is: pi * radius *radius * height or pi * 2ft * 2ft * 3ft or about 37.7 cubic feet. The area of the spout is .021817 square feet.

The velocity of the jet initially is the square root of 2 * g * height or 2 * 32.2 ft/sec/sec * 3 ft or a velocity of 13.9 ft/sec.

The amount of fluid that will leave the tank in 10 seconds under these conditions would be:
10 sec* 13.9 ft/sec * 0.95 * 0.021817 sq. ft. = 2.8809 cubic feet.

So for the next time through you would compute the new height:

37.7 cu. ft. – 2.8809 cu. ft. = pi * 2 ft. * 2 ft. * newHeight

2.7708 feet = newHeight

and go forward.

Other Notes:
Volumes, velocities, and heights should be given to 3 decimal places. Time should be an integer.
All values should line up under headings – The output should be neat and appear as a table of values.
To receive full credit you must use at least one function to implement your program.
• 03-14-2003
RoD
Its hard to help when you have not presented a specific problem you are having. Exactly what with the function are you having trouble? Also, please use code tags when posting, information on these can be found in the moderator posted thread at the top of this, and some others, forum.

-RoD
• 03-14-2003
Stoned_Coder
scrap all that you have done and start again and this time think about it step by step.
Get your types right. Use doubles wherever appropriate.
Decide what the constants are. There are more constants here than just PI.
Think about the steps you take to solve mathematically. Each major step you take should be a separate function. Then main() just becomes a simple job of gluing the other function calls together in the right order.
Also remember in c++ references are extremely useful.
• 03-16-2003
DoItAllMom115
New code
OK, Here is my rewritten code:
Code:

```#include <iostream> #include <cstdlib> #include <cmath> #include <iomanip> using namespace std; //Project 3 - Draining a Tank //Amy E Peabody - March 10, 2003 //Subroutines (functions) for calculating Velocity Jet and VolumeLost float CVelocityJet(int height); float CVolumeLost(float VelocityJet); //Main Program int main (void) {         const double pi=3.14159, RadiusTank=2;         float volume;         float VelocityJet;         float VolumeLost=0;         int height=3, time=0;         //set flags for formatting                 cout.setf(ios::left);                 cout.setf(ios::adjustfield);                 cout.setf(ios::showpoint);                 cout.setf(ios::fixed);                 cout.precision(3);                 volume=pi*RadiusTank*RadiusTank*height;                 cout<<setw(6)<<"Time"<<setw(20)<<"Height of Fluid"<<setw(20)<<"Velocity of Jet"<<setw(15)<<"Volume Lost"<<endl;                 while (height<=0) {                         float CVelocityJet(int height);                         float CVolumeLost(float VelocityJet);                         volume=volume-VolumeLost;                         height=volume/(pi*RadiusTank*RadiusTank);                         time=time+10;                         cout<<time<<height<<VelocityJet<<VolumeLost<<endl;                 }                 return EXIT_SUCCESS; } //function for calculating VelocityJet float CVelocityJet(int height) {         float VelocityJet;         VelocityJet=sqrt(2*32.2*height);         return VelocityJet; } //function for calculating VolumeLost float CVolumeLost (float VelocityJet) {         const double c=0.95, AreaJet=0.021817;         float VolumeLost;         VolumeLost=10*VelocityJet*c*AreaJet;         return VolumeLost; }```

I think this is much better except I cannot get it to print out the different (looped) values of time, height, etc. (line after time=time+10)

Can anyone help?
• 03-16-2003
Stoned_Coder
still not got your types right. Do you really think height should be an int?
while (height<=0)
do you know what that says. It says do this while height is negative.Is that what you want?

float CVelocityJet(int height);
float CVolumeLost(float VelocityJet);

these are func prototypes are are redundant as they are already prototyped at top of file.

etc.
• 03-16-2003
DoItAllMom115
I'm still trying...
I have changed it to
Code:

`                while (height>=0) {`
but why won't it print out the values? is it not calculating them?

so I don't have to declare the function types a second time?
• 03-16-2003
Stoned_Coder
nope but it would be good to call the functions when needed. I think thats what you were trying to do. but you failed. you reprototyped them instead!
• 03-16-2003
DoItAllMom115
still trying...
ok, I removed the declaration in front of the function call.
I changed height to a float instead of an integer, but why won't it print out the calculated values?

Code:

```                while (height>=0) {                         CVelocityJet (height);                         CVolumeLost (VelocityJet);                         volume=volume-VolumeLost;                         height=height-volume/(pi*RadiusTank*RadiusTank);                         cout<<setw(6)<<time<<setw(20)<<height<<setw(20)<<VelocityJet<<setw(15)<<VolumeLost<<endl;                         time=time+10;                 }                 return EXIT_SUCCESS;```
• 03-16-2003
DoItAllMom115
returning values from functions
for some reason it is not returning the calculated values from my functions to the main program

Anyone know why?
• 03-16-2003
XSquared
You forgot to assign the value returned from the function to a variable.

Code:

```VelocityJet = CVelocityJet( height); VolumeLost = CVolumeLost( VelocityJet );```
• 03-16-2003
DoItAllMom115
THANK YOU SO MUCH!!