[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

mutliple functions??

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
kgpretty
Asked:
kgpretty
  • 3
  • 2
  • 2
2 Solutions
 
dennis_georgeCommented:
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
 
kgprettyAuthor Commented:
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
 
jhshuklaCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jhshuklaCommented:
also add years in the param list.
0
 
dennis_georgeCommented:
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
 
kgprettyAuthor Commented:
Thanx.. how do I truncate a number?

eg. 16.66666666666666 to 16.67
0
 
jhshuklaCommented:
#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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now