I'm currently trying to learn c++ by going through a book and ran into this problem, which I'll summarize:
One person invests $100 at 10% simple interest (interest = .10 x original balance)and another person invest $100 at 5% compound interest ( interest = .05 x current balance)
Write a program that finds how many years it takes for the compound interest value to exceed the value of the simple interest investment and display the value of both.
#include <iostream>
#include <string>
usingnamespace std;
int simpleInterest(int, int);
int compoundInterest(int, int);
int main()
{
int originalInvest = 100;
int years;
int finalBalanceSimple;
int finalBalanceCompound;
for (years = 1; simpleInterest(originalInvest, years) > compoundInterest(originalInvest, years); ++years)
{
finalBalanceSimple = simpleInterest(originalInvest, years);
finalBalanceCompound = compoundInterest(originalInvest, years);
}
cout << "It takes " << years << " years for the compound interest investment to exceed the value of the simple interest investment.\n";
cout << "After " << years << " years, the balance of the simple interest investment is: $" << finalBalanceSimple;
cout << " and the balance of the compound interest investment is: $" << finalBalanceCompound << endl;
return 0;
}
int simpleInterest(int invest, int period)
{
int interest = (0.10 * invest) * period;
int balance = invest + interest;
return balance;
}
int compoundInterest(int invest, int period)
{
int interest;
int balance = invest;
for (int i = 1; i <= period; ++i)
{
interest = balance * .05;
balance = balance + interest;
}
return balance;
}
I think it is calculating the number of years (30) correctly, but the balances seem to be displaying for only 29 years instead of 30 (insert pulling out my hair emoticon here). I'm sure it's just something simple that I need to add but I can't figure out how to get those correct values.
Also, I'm sure this code is a mess and hard to read. I keep reading about clean and good code, but I'm having problems figuring out what that means and how to code that way. Any help in cleaning this up and making it easier to read would be very appreciated. I want to learn how to code well, not just hack out something that works. Please criticize this and point me in the right direction so I can write better code in the future.
The problem is the termination condition in your for loop.
It's preventing you from computing the balances for the final year.
A dowhile is simpler:
1 2 3 4 5 6 7 8 9
int years = 0;
int BalanceSimple;
int BalanceCompound;
do
{ years++;
BalanceSimple = simpleInterest(originalInvest, years);
BalanceCompound = compoundInterest(originalInvest, years);
} while (BalanceSimple < BalanceCompound);
Other than this do you think the code is written well? Is there anything else you would change in it to make it easier to read/understand? Have I violated many formatting conventions? Would you have done anything different with the functions? I'm very new and would appreciate any criticism or suggestions.
With the exception of the for loop, your code is fine.
Although there is no rule against having complex calculations in the termination condition of a for loop, it's not best practice to repeat calculations done in the loop.
And as you found out, it caused you to exit the loop prematurely. For loops are most commonly used for iterating through a fixed number of cycles.
dowhile is usually used when you want loop until a specified conditiion is met. i.e. the number of iterations is not known in advance.
As a general rule, I like to keep calculations out of the conditions in a loop, either doing the calculation outside the loop when possible, or when not possible assign the calculation to a variable then test the variable in the loop condition. This makes it easier to debug because you can see what's in the variable when stepping through with a debugger.
#include <iostream>
#include <string>
usingnamespace std;
int simpleInterest(int, int);
int compoundInterest(int, int);
int main()
{
int originalInvest = 100;
int years = 0;
int BalanceSimple;
int BalanceCompound;
do
{ years++;
BalanceSimple = simpleInterest(originalInvest, years);
BalanceCompound = compoundInterest(originalInvest, years);
} while (BalanceCompound < BalanceSimple);
cout << "It takes " << years << " years for the compound interest investment to exceed the value of the simple interest investment.\n";
cout << "After " << years << " years, the balance of the simple interest investment is: $" << BalanceSimple;
cout << " and the balance of the compound interest investment is: $" << BalanceCompound << endl;
return 0;
}
int simpleInterest(int invest, int period)
{
int interest = (0.10 * invest) * period;
int balance = invest + interest;
return balance;
}
int compoundInterest(int invest, int period)
{
int interest;
int balance = invest;
for (int i = 1; i <= period; ++i)
{
interest = balance * .05;
balance = balance + interest;
}
return balance;
}
and here is the correct output:
It takes 30 years for the compound interest investment to exceed the value of the simple interest investment.
After 30 years, the balance of the simple interest investment is: $400 and the balance of the compound interest investment is: $402
Process returned 0 (0x0) execution time : 0.015 s
Press any key to continue.