| closed account (jL8qM4Gy) | |
| Hello all! I am new to C++ and I was given an assignment to calculate the future value of an ordinary annuity in C++. I am very confused on how to do this. I have done some research on annuities but its very confusing. Can anyone give me some pointers? | |
|
|
|
| Darkmaster (494) | |
| and what excactly is your problem? | |
|
|
|
| closed account (jL8qM4Gy) | |
|
This is what I have so far : #include <iostream> #include <string> using namespace std; int main () { float n, r, i; // Future value of an ordinary annuity float OrdinaryAnnuity = ((1 + i)*n -1 /i); cout << "Please enter the payment amount $"; cin >> r; cout << "Please enter the interest rate: "; cin >> i; cout << "Please enter the number of payments: "; cin >> n; cout << "The future value of the annuity is" <<OrdinaryAnnuity<<endl; } Idk what I am doing I am looking at my text book but I don't really understand. | |
|
Last edited on
|
|
| Darkmaster (494) | |
|
you try to calculate OrdinaryAnnuity, before you set i and n with the users input. that won't work | |
|
|
|
| Catfish2 (666) | |
You do it wrong, by calculating OrdinaryAnnuity before you read the data.The value of uninitialized variables ( n, r, i) is garbage, or zero, depending on the phase of the Moon. So you get a garbage response.Solution: move the line float OrdinaryAnnuity = ((1 + i)*n -1 /i); after the last cin.Or you could be more straightforward and just smack the calculation into the final cout. cout << "The future value of the annuity is" << ((1 + i)*n -1 /i) <<endl;Moral: use variables after you read them. Also: you don't use your variable r in calculating the formula. | |
|
Last edited on
|
|
| LGonzales (48) | |||
|
kam, you are just one step away, listen to Darkmasters comment, calculating your formula before you asked for inputs is not going to work.
simply move down your equation to after you enter the inputs. Oops! Sorry while we all might be posting at the same time, Catfish2 already commented on the calculation happening before the input stage. I didnt mean to butt in. | |||
|
Last edited on
|
|||
| closed account (jL8qM4Gy) | |
|
Thank you all. for some reason it still isn't calculating right. I know the calculation for it mathematically is the payment [((1 + interest)*number of periods - 1) / interest].. I can't figure out how to write it in C++ i guess. | |
|
|
|
| LGonzales (48) | |
|
You know what kam, after looking at your problem a little more, i noticed that you didnt use the variable r in the equation. i think the equation is OrdinaryAnnuity = r *[((1 + i)^n - 1) / i] you should be able to figure out from your book what else you might need to do the equation. | |
|
Last edited on
|
|
| Catfish2 (666) | |||
@ kam5985: you misplace a pair of parentheses!
| |||
|
|
|||
| closed account (jL8qM4Gy) | |
|
I know the calculation for it mathematically is the payment [((1 + interest)*number of periods - 1) / interest] r=payment i=interest n=number of periods .. I can't figure out how to write it in C++ i guess. | |
|
|
|
| closed account (jL8qM4Gy) | |
|
actually its not multiplied by number of periods its raised to the number of periods --how do I do this? i know it should be "pow" n... but it won't let me...ahhh | |
|
Last edited on
|
|
| LGonzales (48) | |
you need to #include <math.h> to your program, then you can do things like pow((1 + i),n )so your equation should be like this: OrdinaryAnnuity = r*( pow((1 + i),n) -1 /i); | |
|
Last edited on
|
|
| Catfish2 (666) | |
And if your compiler can't find math.h, use cmath.#include <cmath>
| |
|
|
|
| closed account (jL8qM4Gy) | |
#include <iostream>#include <string> #include <cmath> using namespace std; int main () { double r, i, n, p; double OrdinaryAnnuity; // Future value of an ordinary annuity cout << "Please enter the payment amount per period $"; cin >> r; cout << "Please enter number of periods per year: "; cin >> p; cout << "Please enter the number of years: "; cin >> n; cout << "Please enter the state annual interest rate (in decimal form): "; cin >> i; OrdinaryAnnuity = r*((pow (1+(i / p), (n * p))-1 ) /(i / p)); cout << "The future value of the annuity is " <<OrdinaryAnnuity<<endl; }[ This is what I came up with now. The problem is it's still not calculating it right. My question is how should I input the rate to show as a percentage? say the interest rate is 7%...I would have to enter .07 in order for the program to calculate right. I would like to make it so all you have to do is enter in the 7. | |
|
Last edited on
|
|
| LGonzales (48) | |
|
on line right after cin >> i; you can put i = i/100;At this point you should have all the tools you need to complete your assignment, now its all a matter of getting your formula correct, this should be a reachable goal for you if you just go by trial and error. work out on paper what you have for the formula, and play around with the variables until you know what the output should be like. thats most of the job of programming right there, code->compile->test->retry. you've got the formula and i'm glad you figured out how to incorporate the POW function. you are almost done | |
|
Last edited on
|
|
| Incis B (92) | |
|
kam, Q)-say the interest rate is 7%...I would have to enter .07 in order for the program to calculate right. I would like to make it so all you have to do is enter in the 7. A)- Enter 7 and divide by 100. for further help , try this link: http://www.jiskha.com/search/index.cgi?query=%245%2C000+compounded+semiannually+at+6%+for+5+years btw, it'll be a lot easier if you make a plan first. plan what will go into MAIN() and plan what functions you'll need plan what inputs and outputs you'll need plan how you want to handle exceptions and errors. and plan how you want the user to respond - is this a single shot, once through program or multiple time use? and lastly, how you plan to test. oh, and plan your variables in advance so they don't look like i,j,n,m, x,y, but rather like INT i_Rate, i_Num_years Float f_Calc_Val DOUBLE d_Annual_Inst Once you have a good design, then you can add your formulas THEN you can begin to wrap that with C++ code. | |
|
|
|
| closed account (jL8qM4Gy) | ||
This is my newest code. I think I am getting really close. The only problem I am getting is it's not producing the exact amount still. | ||
|
|
||
| Catfish2 (666) | ||
Then it can only be that you made mistakes in the formula again, because your code doesn't have language mistakes that I can see. Or if the problem has to do with accuracy, use double instead of float. Be advised, because of the way both double and float store real numbers, you can never get 100% accuracy. http://en.wikipedia.org/wiki/Floating-point#Accuracy_problems | ||
|
|
||
| LGonzales (48) | |||
|
kam, you changed the formula and added another parameter to the program (years and per), i just noticed it, and this is why you're getting confused. I looked up the formula for FutureValue Ordinary Annuity and there are really 3 parameters needed. C = Cash flow per period i = interest rate n = number of payments FVOrdinaryAnnuity = C * [ ( pow(1+i,n) -1) / i ] remember this? this is what you were putting up for us to look at, and its pretty close to what the other textbooks are saying, just refined it a little so it works in C/C++ language. when you introduced the 'years' variable and the 'per' variable you really didnt change anything. relative to your original equation, years*per = number of payments. and with that proof, here is my version of your program which only uses 'number of payments' the equation was from: http://www.investopedia.com/articles/03/101503.asp#axzz2CrmpdaTV i'm hoping that this at least solves your original problem:
I threw in Present Value ordinary Annuity just in case you wanted to see anther calculation translated into your program. give this a try and see if this gets you closer to the right anwser. I'll let you figure out how to do the equation with years*per variables. | |||
|
Last edited on
|
|||
| nathan10 (122) | |||
|
This is a great example of the pitfalls of trying to write code on the fly. OP, when you don't fully understand the subject for which you are programming, in this case finance, then first write pseudocode to organize your thoughts about the problem. I KNOW that if you had pseudocoded your problem you would not have tried to compute the future value before the user inputted the needed parameters, as Darkmaster pointed out. I also believe that if you had also created test data by hand and tested your program with it, you would have discovered your misplaced parenthesis error right away. If you are still having difficulty after the posts to help you, then go back to square one and pseudocode your problem. Then you can get your program correct. I am teaching myself C++ and am far from an expert. My very first intro to computer science course was as a freshman in 1980 and we used BASIC. The professor taught us pseudocode. As I try to write programs for more complex problems, I really see how ESSENTIAL pseudocoding is. I am surprised it is not emphasized more. I decided to give an example.
If you want bonus points, add filters to make sure the interest rate is not negative, the compounding period is at least daily, the number of periods is not negative, the annuity amount is not negative. N.B. There is a difference between an ordinary annuity, (payment made at end of the period like a salary), and an annuity due, (payment made end beginning of the period like rent). | |||
|
Last edited on
|
|||