Hey fox i had a hw problem and im stuck we're supposed to do a payment project and i sort of did the first part but then stuck on the second. Here's what i've done so far...

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//Program name: Amortize
int main(void)
{
   double Amount, Rate, Payment, Z;
   int Years, NoPayment;
   cout<<"Enter the borrowed amount --->";
   cin>>Amount;
   cout<<endl<<"Enter the annual interest rate (%) --->";
   cin>>Rate;
   cout<<endl<<"Enter the pay-back period (in years) --->";
   cin>>Years;
   /******These statements compute the monthly payment*****/
   Rate = Rate / 1200;
   NoPayment = Years*12;
   Z = exp(-NoPayment * log(1+Rate));
   Z = (1 - Z) / Rate;
   Payment = Amount / Z;
   /*******************************************************/
   cout<<endl;
   cout<<setprecision(2)<<fixed<<showpoint;
   cout<<"The monthly payment is $"<<Payment<<endl;
   cin.get(); cin.get();
   return 0;
} // Amortize

Now the professor is asking to put the function definition for find payment... im kinda lost... it's due tmw and im stuck... i've marked where i think the code should be put i might be wrong. Would be glad if someone would help me out... Thanks a bunch.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

/***** Function prototype goes here ******/

//Program name: Amortize
int main(void)
{
   double Amount, Rate, Payment, Z;
   int Years, NoPayment;
   cout<<"Enter the borrowed amount --->";
   cin>>Amount;
   cout<<endl<<"Enter the annual interest rate (%) --->";
   cin>>Rate;
   cout<<endl<<"Enter the pay-back period (in years) --->";
   cin>>Years;
   /******These statements compute the monthly payment*****/
   Payment = FindPayment (Amount, Rate, Year);
   /*******************************************************/
   cout<<endl;
   cout<<setprecision(2)<<fixed<<showpoint;
   cout<<"The monthly payment is $"<<Payment<<endl;
   cin.get(); cin.get();
   return 0;
} // Amortize


/*****Insert function definition for FindPayment( ) here*****/

Recommended Answers

All 5 Replies

1)Define a function with similar structure as your "main()" method, but the return value is "double" instead of "int" and the arguments in the parenthesis must match the incoming type of Amount, Rate, and Year.
2)Copy the whole part where you make the mark and put them inside the newly create function.
3)At the end of the function do not forget to "return" the value you just computed.

// for example
// in main
int main() {
  ...
  double val;
  int a=2;
  double b=30.5;
  ...
  val = callMyFunc(a, b);  // call the function
}

// the function
double callMyFunc(int v1, double v2) {
  double x = v1 * v2;  // try to be explicit here
  return x;
}

Also, don't forget the prototype at the top. Taywin's example would look like:

/***** Function prototype goes here ******/
double callMyFunc(int v1, double v2);

Note that the argument names aren't strictly needed in the prototype, so double callMyFunc(int, double); is OK, I just find it much more self-documenting if sensible names are included at that point, so the reader can tell immediately what the -purpose- of each argument is, not just the type.

ok i don't quite follow... where do i plug in what you've given me... can you show it to me with the entire coding? please im not so good in coding.

This is for your programming course. The way to get good at coding is to do it. Relax! It's not hard. You've been given the name of the function, and how it will be invoked. The next step is to write the function-prototype which will go up at the top of your program: it takes three arguments, in the correct order (as indicated by the call to it) -- what are the types of each? And it has to return something. What type should it return? Can you do that much?

Then you put the function definition at the bottom: using Taywin's sample as a guide for how to write a function, in general, now have your function do what it needs to do -- the "filling" has also been given to you, between the lines in the first chunk of code marked "These statements compute the monthly payment" ... can you see how the only difference between the first program and the second is that those lines have been replaced with a function call? They still have to go somewhere -- in the function-body. And then don't forget to make sure your function returns the computed value (which should be of the expected type). Assuming you've done the prototype, can you now do this part as well?

First off, I want to say I understand why someone may be reluctant to just post the answer. After all people just post questions without any effort. On the other hand, sometimes the best way to learn it is to see it done first hand.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std ; 

// FUnction Prototype // 
double calculatePayment(int no_payment, int years) ; 


/* Note: that in function prototype or definition it doesn't matter if the variable
names are the same as they are in main as they are creating separate copies */
/*Second Note: Most people put their function definition (The stuff that does the stuff) below main or their main class, however you can put it wherever you want as long as it's below the #includes and using namespace std statement. If you put it above main then you don't (usually) have to have a prototype, but it's a good habit. 
*/

//FUnction definition
double calculatePayment(int no_payment, int years) { 
    double amount, rate, payment, z ;   

     Rate = Rate / 1200 ; 
     no_payment = years * 12 ; 
// that thing you did with z i don't want to type
// the other thing ...
payment = amount / z ;

// return the amount *give it back to main *

return payment ;

} 

And in main . . .

double payment ; 
payment = calculatePayment(noPayment, years) ; 

Hope this helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.