Using functions (subroutines)

This is a discussion on Using functions (subroutines) within the C++ Programming forums, part of the General Programming Boards category; I need to write a program to calculate time it takes (in increments of 10 seconds) to drain a tank ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Question 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;
    }
    See next reply which gives the problem...
    Last edited by DoItAllMom115; 03-14-2003 at 06:55 PM.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Post 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.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Lightbulb 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?

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    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?

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    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;

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Unhappy returning values from functions

    for some reason it is not returning the calculated values from my functions to the main program

    Anyone know why?

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You forgot to assign the value returned from the function to a variable.

    Code:
    VelocityJet = CVelocityJet( height);
    VolumeLost = CVolumeLost( VelocityJet );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    27

    Thumbs up THANK YOU SO MUCH!!

    I really appreciate your help.
    I think I might have it now!! finally...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21