Solved

mutliple functions??

Posted on 2004-10-26
197 Views
Last Modified: 2012-05-05
Should I place multiple program defined functions in my C++ code?

I have to find the maturity value for four different rates [4%, 5%, 6%, 7%] using this expression:

maturity4 = principal * pow((1 + rate/100) ,years);

The question states that the formula should be calculated in a separate function, but the output should be in main ().
0
Question by:kgpretty
    7 Comments
     
    LVL 5

    Accepted Solution

    by:
    I think its a homework question... so I will not help you with code..... but we can guide you how to do it.....

    I think you have been asked to create four functions, each with different names.... e.g. calculate_Maturity4, calculate_Maturity5, calculate_Maturity6, ....

    and each function will have separate rates... calculateMaturity4 will be return a value calculated with rate = 4%
    e.g. it will return principal * pow((1 + 4/100), years)....

    Hope you got it
    Dennis
    0
     

    Author Comment

    by:kgpretty
    Ok.. I guess that answered my question - multiple functions.

    When I'm displaying the answer.. and I have mat_val1 as the function name, I put this in right.. between the string and end line:

    cout << "Maturity value with 4%: " << mat_val1 << endl;
    0
     
    LVL 9

    Expert Comment

    by:jhshukla
    don't know if you want to do this or not:
    float calculateMaturity(float principal, float rate){
      ...
    }

    then call the function with different principals & rates as arguments.
    0
     
    LVL 9

    Expert Comment

    by:jhshukla
    also add years in the param list.
    0
     
    LVL 5

    Expert Comment

    by:dennis_george
    If you are using global principal and year variable then its ok ... but with brackets

    float mat_val1() ; // function declaration

    cout << "Maturity value with 4%: " << mat_val1() << endl;

    else as jhshukla said you can pass it to the function

    cout << "Maturity value with 4%: " << mat_val1(principal, year) << endl;  // as rate is fixed for each function

    Dennis
    0
     

    Author Comment

    by:kgpretty
    Thanx.. how do I truncate a number?

    eg. 16.66666666666666 to 16.67
    0
     
    LVL 9

    Assisted Solution

    by:jhshukla
    #include <iostream>
    using namespace std;

    int main () {
      double f = 3.14159;
      cout.precision(5);
      cout << f << endl;
      cout.precision(10);
      cout << f << endl;
      return 0;
    }
    The execution of this example shall display:
      3.1416
      3.14159

    in your case,
    cout.precision(4);
    cout << number << endl;

    http://www.cplusplus.com/ref/iostream/ios_base/index.html
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now