Solved

# mutliple functions??

Posted on 2004-10-26
197 Views
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

LVL 5

Accepted Solution

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

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

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

also add years in the param list.
0

LVL 5

Expert Comment

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

Thanx.. how do I truncate a number?

eg. 16.66666666666666 to 16.67
0

LVL 9

Assisted Solution

#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

cout.precision(4);
cout << number << endl;

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

## Featured Post

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.