13 Replies - 693 Views - Last Post: 03 May 2013 - 01:06 PM Rate Topic: -----

#1 yank13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 13

functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 11:06 AM

caculate monthly payments for a loan using functions.

I'm sorry the code is not in order, I'm still in the troubleshooting stage. the program crashes. this is the assignment.

Write a c++ program to help a prospective borrower calculate the monthly payment for a loan. The program also prints the amortization(payoff)table to show the balance of the loan after monthly payment.

prompt user for input;

1)Amount of the loan:
2)Interet rate per year:
3)number of years:



formula;

NM=(NY *12)
IM =(IY/12)
P=(1+IM)NM
Q =(P/(P-1))
MP=(PR*IM*Q)

where
NY= Scheduled number of years to amortize the loan
NM=Scheduled number of months for the loan
IY=Interest rate per year(as a percentage)
IM=Interest rate/month (decimal)
PR=Principal (the amount of the loan)
P= The value of (1+IM)NM
Q= The value of P/(P-1)
MP= Monthly payment

main must call three functions: calculateMonthlyPayment, printInormation, printAmortizationTable.



i will make a attachments how output should look like. i already successfully run one without using functions, just dont get it with using functions, anyone help me to work it out? i can run it but those data just spread out
and it keeps running forever.



and sorry that i dont know how to attach my codes.

#include <cstdlib>
 #include <iostream>
 #include <math.h>
 #include <iomanip>
 
using namespace std;
 

double calcMonthlyPayment (double prin, double IY, double NY);
 void printInformation (double prin, double IM, double NM, double MP);
 void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB);
 
int main()
 { 
double prin, IY, NY, MP;
 
cout<< " Loan Amount (Principal)? ";
 cin>> prin;
 cout<< endl;
 cout<< " Interest rate per year (percent)? ";
 cin>> IY;
 cout<< endl;
 cout<< " Number of years? ";
 cin>> NY;
 cout<< endl;
 cout<< endl;
 
MP = calcMonthlyPayment(prin, IY, NY);
 

}
 
double calcMonthlyPayment (double prin, double IY, double NY)
 { 
double P, Q, IM, NM, MP;
 
IM = (IY/12)/100;
 NM = (NY*12);
 P = pow((1 + IM), NM);
 Q = (P / (P - 1));
 MP = (prin * IM * Q);
 
printInformation (prin, IM, NM, MP);
 
return MP;
 }
 
void printInformation (double prin, double IM, double NM, double MP)
 { 
double PP, IP, OB, NB;
 cout<< " Loan Amout: " << prin << endl;
 cout<< " Interest rate per month (IM): " << IM << endl;
 cout<< " Number of months (NM): " << NM << endl;
 cout<< " Monthly Payment is: " << MP << endl;
 cout<< endl;
 cout<< " Month Old Monthly Interest Principal New " << endl;
 cout<< " Balance Payment Paid Paid Balance " << endl;
 
printAmortizationTable (PP, MP, NM, IM, IP, OB, NB);
 }
 
void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB)
 { 
int i;
 double prin;
 OB = prin;
 for(i=1; i <= NM; i++)
 {
 IP = OB * IM;
 PP = MP - IP;
 NB = OB - PP;
 cout<< setw(2) << i << setw(14) << OB << setw(9) << MP << setw(13) << IP << setw(14) << PP << setw(15) << NB << endl;
 OB = NB;
 }
 

system("PAUSE");
 }



thank you!

Attached image(s)

  • Attached Image

This post has been edited by jimblumberg: 03 May 2013 - 12:24 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: functions, sry i made mistake, dont know how to edit so just post new

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,311
  • Joined: 20-March 10

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 11:35 AM

Here is your code in code tags,

[ code] [ /code] (without spaces)

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;


double calcMonthlyPayment (double prin, double IY, double NY);
void printInformation (double prin, double IM, double NM, double MP);
void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB);

int main()
{
double prin, IY, NY, MP;

cout<< " Loan Amount (Principal)? ";
cin>> prin;
cout<< endl;
cout<< " Interest rate per year (percent)? ";
cin>> IY;
cout<< endl;
cout<< " Number of years? ";
cin>> NY;
cout<< endl;
cout<< endl;

MP = calcMonthlyPayment(prin, IY, NY);


}

double calcMonthlyPayment (double prin, double IY, double NY)
{
double P, Q, IM, NM, MP;

IM = (IY/12)/100;
NM = (NY*12);
P = pow((1 + IM), NM);
Q = (P / (P - 1));
MP = (prin * IM * Q);

printInformation (prin, IM, NM, MP);

return MP;
}

void printInformation (double prin, double IM, double NM, double MP)
{
double PP, IP, OB, NB;
cout<< " Loan Amout: " << prin << endl;
cout<< " Interest rate per month (IM): " << IM << endl;
cout<< " Number of months (NM): " << NM << endl;
cout<< " Monthly Payment is: " << MP << endl;
cout<< endl;
cout<< " Month Old Monthly Interest Principal New " << endl;
cout<< " Balance Payment Paid Paid Balance " << endl;

printAmortizationTable (PP, MP, NM, IM, IP, OB, NB);
}

void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB)
{
int i;
double prin;
OB = prin;
for(i=1; i <= NM; i++)
{
IP = OB * IM;
PP = MP - IP;
NB = OB - PP;
cout<< setw(2) << i << setw(14) << OB << setw(9) << MP << setw(13) << IP << setw(14) << PP << setw(15) << NB << endl;
OB = NB;
}


system("PAUSE");
}



First of all int main should return an integer usually zero for success.

second always initialise variables

like so

double PP=0.0, IP=0.0, OB=0.0, NB=0.0;



and finally can you clarify what it is you are asking...?

Thanks

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 yank13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 13

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:27 PM

y it just run like this forever?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4076
  • View blog
  • Posts: 12,588
  • Joined: 25-December 09

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:29 PM

What values did you input into your program?

Try: 1000
12
1


Jim

This post has been edited by jimblumberg: 03 May 2013 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,311
  • Joined: 20-March 10

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:32 PM

View Postyank13, on 03 May 2013 - 07:27 PM, said:

y it just run like this forever?



Because you are not initialising everyone of your variables.

uninitialised variables contain junk values so it can run a long time.

If you would just initialise every single double you have to 0.0 it would not do that.


Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#6 yank13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 13

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:32 PM

input: prin, IY, NY. There are formula for others
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,311
  • Joined: 20-March 10

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:35 PM

no Jim means

what values did you try when the program asked you for

amount of loan

interest

and

repayment period
Was This Post Helpful? 0
  • +
  • -

#8 yank13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 13

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:36 PM

but how i gonna initialise those PP if i need to get that from a formula PP = MP - IP; and MP = (prin * IM * Q);, IP = OB * IM;

ohh, sorry, my bad, i input 5000, 11, 1. example from homework sheet
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4076
  • View blog
  • Posts: 12,588
  • Joined: 25-December 09

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:41 PM

This is the output I get from your program:

 Loan Amount (Principal)? 5000

 Interest rate per year (percent)? 11

 Number of years? 1


 Loan Amout:                   5000
 Interest rate per month (IM): 0.00916667
 Number of months (NM):        12
 Monthly Payment is:           441.908

 Month     Old     Monthly     Interest     Principal         New 
         Balance   Payment       Paid         Paid          Balance 
 1  -1.37481e-41  441.908 -1.26024e-43       441.908       -441.908
 2      -441.908  441.908     -4.05083       445.959       -887.867
 3      -887.867  441.908     -8.13878       450.047       -1337.91
 4      -1337.91  441.908     -12.2642       454.173       -1792.09
 5      -1792.09  441.908     -16.4275       458.336       -2250.42
 6      -2250.42  441.908     -20.6289       462.537       -2712.96
 7      -2712.96  441.908     -24.8688       466.777       -3179.74
 8      -3179.74  441.908     -29.1476       471.056       -3650.79
 9      -3650.79  441.908     -33.4656       475.374       -4126.17
10      -4126.17  441.908     -37.8232       479.731        -4605.9
11       -4605.9  441.908     -42.2207       484.129       -5090.03
12      -5090.03  441.908     -46.6586       488.567       -5578.59
sh: 1: PAUSE: not found



These are the warnings I got when I compiled your code:

Quote

main.cpp||In function ‘int main()’:|
main.cpp|15|warning: variable ‘MP’ set but not used [-Wunused-but-set-variable]|
main.cpp||In function ‘void printAmortizationTable(double, double, double, double, double, double, double)’:|
main.cpp|66|warning: ‘prin’ is used uninitialized in this function [-Wuninitialized]|
main.cpp||In function ‘void printInformation(double, double, double, double)’:|
main.cpp|59|warning: ‘PP’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|59|warning: ‘IP’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|59|warning: ‘OB’ is used uninitialized in this function [-Wuninitialized]|
main.cpp|59|warning: ‘NB’ is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 errors, 6 warnings ===|



Jim
Was This Post Helpful? 0
  • +
  • -

#10 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,311
  • Joined: 20-March 10

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:44 PM

for the input 5000, 11, and 1 I get with your program


Attached Image

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#11 yank13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 13

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:49 PM

um, why you can stop there? i cant even pause, it would just keep running, because i didnt initialise? but i need to give the value to them by the formula
Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 778
  • View blog
  • Posts: 2,311
  • Joined: 20-March 10

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 12:55 PM

You need to basically

initialise here

void printInformation (double prin, double IM, double NM, double MP)
{
double PP=0.0, IP=0.0, OB=0.0, NB=0.0;



and here

void printAmortizationTable (double PP, double MP, double NM, double IM, double IP, double OB, double NB)
{
int i;
double prin=0.0;


what compiler/IDE are you using because your warning levels are set too low

Your IDE should have warned you about these things.

Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4076
  • View blog
  • Posts: 12,588
  • Joined: 25-December 09

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 01:06 PM

Actually there doesn't even seem to be a reason to pass most of those variables into your function, they don't have a value in the calling function and since they are passed by value there is no need to pass them.


I would define printAmortizationTable more like:

void printAmortizationTable (double MP, double NM, double IM, double prin)
{
     int i;
     double OB, IP, PP, NB;


Of course you'll need to change the function call and prototype to match.


You should study the function tutorials contained in my signature.


Jim
Was This Post Helpful? 1
  • +
  • -

#14 yank13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-May 13

Re: functions, sry i made mistake, dont know how to edit so just post new

Posted 03 May 2013 - 01:06 PM

Using Dev-C++ 4.9.9.2
I am newbie I dont knwo much about that :( sorry,um, and i initialised all the places u said, but i got the error

C:\Users\KAIYAN~1\AppData\Local\Temp\ccWUjaaa.o(.text+0x28c) In function `Z18calcMonthlyPaymentddd':
[Linker error] undefined reference to `printInformation(double, double, double, double)'
C:\Users\KAIYAN~1\AppData\Local\Temp\ccWUjaaa.o(.text+0x28c) ld returned 1 exit status
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1