Thread: Function assignment problems

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    Function assignment problems

    I have an assignment where I'm supposed to take an old code and rewrite it using functions. I'm still a little shady on where I'm supposed to include certain parts of information. Could someone take a look at them and see where I misplaced the information? I'm brand new to this so please don't assume this is sloppy work because I'm lazy. This is my original code:

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    
    int main()
    {
       // Declare and initialize variables and constants
    
    
       //(r = radius, g = acceleration due to gravity,
       // ds = solid density, df = fluid density,
       // fv = fluid viscosity, v = velocity, t = time,
       // d = distance to bottom of tank.)
    
    
       double r, g, d, ds, df, fv, v, t, df1, ds1, fv1;
    
    
       //**Print values**//
    
    
       //Constants
    
    
       cout << "Distance to bottom of tank =  20.0 cm" << endl;
       cout << "Radius of the particle =       0.5 cm" << endl;
       cout << "Local value of g (gravity) = 980.00 cm/s^2" << endl;
    
    
       //User inputs
    
    
       cout << endl;
       cout << "Please enter a value for the fluid density (g/cm^3): ";
       cin >> df;
    
    
       cout << "The value you entered is " << df << endl;
       df1=df;
    
    
       cout << endl;
       cout << "Please enter a value for the solid density (g/cm^3): ";
       cin >> ds;
       cout << "The value you entered is " << ds << endl;
       ds1=ds;
    
    
       cout << endl;
       cout << "Please enter a value for the fluid viscosity (g/cm-s): ";
       cin >> fv;
       cout << "The value you entered is " << fv << endl;
       fv1=fv;
    
    
       r=0.5, g=981, d=20;
       v = ((2.0/9)*((ds1-df1)/fv1)*g*r*r)/100;
       t = (d/v);
    
    
       //Calculations
    
    
       cout << endl;
       cout << fixed << setprecision(2);
       cout << "Fluid density: " << setw (10) << df << " g/cm^3" << endl;
       cout << "Solid density: " << setw (10) << ds << " g/cm^3" << endl;
       cout << "Fluid viscosity: " << setw (8) << fv << " g/cm-s" << endl;
       cout << "Velocity: " << setw(15) << v << " cm/s" << endl;
       cout << "Falling time: " << setw (11) << t << " s" << endl;
    
    
       return 0;
    }
    and this is the one I'm trying to create using functions:

    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    
    //Constants
    
    
    void printInformation ()
    {
       cout << "Distance to bottom of tank =  20.0 cm" << endl;
       cout << "Radius of the particle =       0.5 cm" << endl;
       cout << "Local value of g (gravity) = 980.00 cm/s^2" << endl;
    }
    
    
    // Declare and initialize variables and constants
    
    
       //(r = radius, g = acceleration due to gravity,
       // ds = solid density, df = fluid density,
       // fv = fluid viscosity, v = velocity, t = time,
       // d = distance to bottom of tank.)
    
    
        void calculateVelocityAndTime(double r, double g, double d, double df,
            double ds, double fv, double& v, double& t)
     {  v = ((2.0/9)*((ds-df)/fv)*g*r*r)/100;
        t = (d/v);
       cout << endl;
       cout << fixed << setprecision(2);
       cout << "Fluid density: " << setw (10) << df << " g/cm^3" << endl;
       cout << "Solid density: " << setw (10) << ds << " g/cm^3" << endl;
       cout << "Fluid viscosity: " << setw (8) << fv << " g/cm-s" << endl;
       cout << "Velocity: " << setw(15) << v << " cm/s" << endl;
       cout << "Falling time: " << setw (11) << t << " s" << endl;
     }
    
    
        void displayResults(double dp, double mu, double r,
            double H, double Ho, double velocity, double time);
    
    
    int main()
    {
        printInformation();
        cin << df << endl;
        cin << ds << endl;
        cin << fv << endl;
        displayesults();
        cout << v << " " << t << endl;
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    I rewrote it and tried cleaning it up. Can someone explain why I keep getting an error message saying why the variables in the display results in main were not declared?

    Code:
    
    
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    
    void printInformation ();
    
    
    void askQuestions(double df, double ds, double fv);
    
    
    void displayResults(double df, double ds, double fv, double v,
     double t);
    
    
    int main()
    {
        float df, ds, fv;
    
    
        printInformation();
        askQuestions (df, ds, fv);
        displayResults(df, ds, fv, v, t);
    
    
       return 0;
    }
    
    
    //Constants display
    
    
    void printInformation ()
    {
       cout << "Distance to bottom of tank =  20.0 cm" << endl;
       cout << "Radius of the particle =       0.5 cm" << endl;
       cout << "Local value of g (gravity) = 980.00 cm/s^2" << endl;
       cout << endl;
    }
    
    
    void askQuestions (float fd, float ds, float fv)
    {
       cout << "Please enter a value for fluid density " << endl;
       cin >> df ;
       cout << "Please enter a value for solid density " << endl;
       cin >> ds;
       cout << "Please enter a value for fluid viscosity " << endl;
       cin >> fv ;
    
    
       {void displayResults(double df, double ds, double fv, double v,
     double t)
    {
       cout << endl;
       cout << fixed << setprecision(2);
       cout << "Fluid density: " << setw (10) << df << " g/cm^3" << endl;
       cout << "Solid density: " << setw (10) << ds << " g/cm^3" << endl;
       cout << "Fluid viscosity: " << setw (8) << fv << " g/cm-s" << endl;
       cout << "Velocity: " << setw(15) << v << " cm/s" << endl;
       cout << "Falling time: " << setw (11) << t << " s" << endl;
    }
    
    
       }
    
    
    }
    double addNumbers (double df, double ds, double fv, double g, double r, double d, double v, double t)
    {
       r=0.5, g=981, d=20;
       v = ((2.0/9)*((ds-df)/fv)*g*r*r)/100;
       t = (d/v);
    }
    Last edited by Allen Sarkisov; 03-02-2014 at 12:11 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Here is your main.
    Where is v or t declared in main?

    Tim S.

    Code:
    int main()
    {
        float df, ds, fv;
     
     
        printInformation();
        askQuestions (df, ds, fv);
        displayResults(df, ds, fv, v, t);
     
     
       return 0;
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest using real constants for the program constants.
    Edit2: Added link http://www.cplusplus.com/doc/tutorial/constants/
    Edit3: See the "Typed constant expressions" section.

    Edit1: Read this link http://www.cprogramming.com/tutorial/lesson4.html
    You will likely need to break addNumbers into two functions each returning just a single value.

    Tim S.
    Last edited by stahta01; 03-02-2014 at 08:53 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> {void displayResults(double df, double ds, double fv, double v,
    >> double t)
    Presumably this is not your real code, or you have a syntax error.
    Anyway, your AskQuestions function must take references, or you will not see the information reflected back in main (read up on references).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multi function assignment
    By CASHOUT in forum C Programming
    Replies: 12
    Last Post: 02-03-2013, 02:15 PM
  2. Replies: 14
    Last Post: 10-17-2011, 10:17 PM
  3. Cprogramming Assignment, having problems.
    By Zaz in forum C Programming
    Replies: 2
    Last Post: 10-30-2009, 01:24 PM
  4. Big problems with assignment, please help!!
    By JFonseka in forum C Programming
    Replies: 12
    Last Post: 03-25-2007, 12:16 AM
  5. Problems with array to string assignment.
    By Tronic in forum C++ Programming
    Replies: 11
    Last Post: 03-15-2004, 09:06 PM

Tags for this Thread