CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2011
    Posts
    4

    C++ substituting variables for values

    Hello everybody. I am a beginner at C++ and have a lab assignment due that i am a little lost on. The lab states that i must create a table using iomanipulators, setw, setprecision, and setiosflags that shows current i, Vr, Vcap, and Vr+Vcap with respect to time. The given formulas are i=E*(pow(e,(-t/(R*C)))/R), Vr=i*R, and Vcap = E*(1-pow(e,(-t/(R*C)))). I have already programmed these and have defined all variables, include t1-t5 (.01s,.05s,.1s,1s,5s) as floats. Values for e, E, R, and C are given. My problem is that i am unsure how to plug different values of t into the equations to get an appropriate output for each section of the table. I know i must cin the time values, but i'm lost as to what to do after that. Could somebody please help?

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: C++ substituting variables for values

    not quite sure I understand where you are stuck. Do you have a method/function that takes time as an argument?

  3. #3
    Join Date
    Sep 2011
    Posts
    4

    Re: C++ substituting variables for values

    Yes i have to make a table that shows 5 different times and then plug those times into the equations for i and Vr and Vcap. I don't know how to plug the values in, besides rewriting the equation with the given times in each of the designated spots on the table

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ substituting variables for values

    Quote Originally Posted by munkalla View Post
    I don't know how to plug the values in,
    I have already programmed these and have defined all variables, include t1-t5 (.01s,.05s,.1s,1s,5s) as floats.
    Instead of just describing, let's see the function you wrote that computes these values.

    And please use code tags when posting code, so that the code becomes formatted when forum members are viewing it.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Sep 2011
    Posts
    4

    Re: C++ substituting variables for values

    That's the problem i have no idea how to create a function that will compute the values... as i said i am a beginner at this. I have the equation and i have the times... the problem is inputting the times into the equations to get the current and voltage drop output

  6. #6
    Join Date
    Aug 2009
    Posts
    440

    Re: C++ substituting variables for values

    Functions take parameters. So, if in your code you have 5 values for time, you can create 5 different variables, one for each time. Then you design your function so that it takes time as a parameter.

    Code:
    int doublevalue( double timevalue );
    
    int main()
    {
       double time1 = 0.5; //time in seconds
       double result;
       result = doublevalue( time1 );
    }
    
    int doublevalue( double timevalue )
    {
        return ( time1 * 2 );
    }
    The above function simple doubles the value you give it. The variable result will hold the doubled value (or the result of the function) after the function is executed.

    So, like I mentioned above, you can simply have 5 time variables, each holding their values and then call your function 5 times with the different values. Or, if you want to be clever, if the time values are multiples of one another, you could use a loop (removes the necessity of needing 5 separate time values).

    Hope this helps.

  7. #7
    Join Date
    Sep 2011
    Posts
    4

    Re: C++ substituting variables for values

    But what if what i have to do with the time is a bit more complicated then simply doubling it... like using one of the formulas given in the initial question

  8. #8
    Join Date
    Aug 2009
    Posts
    440

    Re: C++ substituting variables for values

    Well, you will need to have #include <cmath> in your program to take advantage of the power function. Then, instead of doubling the value you just use one of your above formulas:

    i = E * ( pow( e, ( -t / ( R * C ) ) ) / R )

    Take that for instance. You either create more variables with the appropriate constants, or add more parameters to the function if you need to provide that information in the function You can then just return i in your function.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ substituting variables for values

    Quote Originally Posted by munkalla View Post
    But what if what i have to do with the time is a bit more complicated then simply doubling it... like using one of the formulas given in the initial question
    Why not try to write any formula using C++? The compiler isn't a bomb -- it isn't going to blow up on you if you make a mistake.
    Code:
    #include <cmath>
    
    int SomeFormula( double timevalue, double vZero );
    
    int main()
    {
       double time1 = 0.5; //time in seconds
       double result;
       result = SomeFormula( time1, 0.0 );
    }
    
    int SomeFormula( double timevalue, double VZero )
    {
        return  VZero + 0.5 * (32.0 * pow(timevalue. 2.0));
    }
    Recognize that formula? It is different than yours, and is more "complicated" than doubling.

    The way you learn is to write sample functions and familiarize yourself with the way to call them. If you make a mistake, just keep reading books and trying until you understand and get it correct.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured