Reputation:
I'm a beginner trying to learn C++ using "C++ Primer Plus Sixth Edition".
I'm on Chapter 5, going over loops. Anyways I was doing this programming exercise from the book, the problem is:
Daphne invests $100 at 10% simple interest.That is, every year, the investment earns 10% of the original investment, or $10 each and every year: interest = 0.10 × original balance At the same time, Cleo invests $100 at 5% compound interest.That is, interest is 5% of the current balance, including previous additions of interest:
interest = 0.05 × current balance
Cleo earns 5% of $100 the first year, giving her $105.The next year she earns 5% of $105, or $5.25, and so on.Write a program that finds how many years it takes for the value of Cleo’s investment to exceed the value of Daphne’s investment and then displays the value of both investments at that time.
Here is the code I have written for this exercise, I'm not getting good results though. EDITED code:
#include <iostream>
#include <array>
double Daphne(int, double, double);
double Chleo(double, double);
int main() {
using namespace std;
int p = 100; //Principle
double i1 = 0.1; // 10% interest rate
double i2 = 0.05; // 5% interest rate
double dInv = 0; //Daphnes investment
double cInv = 0; // Chleos investment
int t=1; //Starting at year 1
double s1 = 0; //Sum 1 for Daphne
double s2 = 0; // Sum 2 for Chleo
s1 = p + 10; //Initial interest (base case after year 1) for Daphne
s2 = p + (i2*p); //Initial interest (base case after year 1) for Chleo
do{
dInv = Daphne(p, i1, s1);
cInv = Chleo(i2, s2);
t++;
} while (cInv < dInv);
cout << "The time taken for Chleos investment to exceed Daphnes was: "
<< t << endl;
cout << "Daphnes investment at " << t << " years is: " << dInv << endl;
cout << "Chleos invesment at " << t << " years is: " << cInv << endl;
system("pause");
return 0;
}
double Daphne(int p, double i, double s1) {
s1 = s1 + (p*i);
return s1;
}
double Chleo(double i, double s2){
s2 = s2 + (s2*i);
return s2;
}
EDIT: Getting no output from console. Suspecting that it never breaks from the loop.
Final EDIT:
Figured it out using for loops and working in main for now, while loops and function calls were confusing me. Thanks for all the comments, here is my final code for anyone that is interested:
#include <iostream>
#include <array>
int main() {
using namespace std;
int p = 100;
double dInv = p; //Daphnes investment
double cInv = p; //Cleos investment
int t; //Time
double i1 = 0.1; //10% interest
double i2 = 0.05; //5% interest
for (t = 0; cInv <= dInv; t++) {
dInv = (p*i1) + dInv; //Simple interest for Daphne
cInv = (cInv *i2) + cInv; //Compund interest for Cleo
}
cout << "Time taken for Cleos investment to exceed Daphnes: " << t << endl;
cout << "Daphnes investment: " << dInv << endl;
cout << "Cleos investment: " << cInv << endl;
system("pause");
return 0;
}
Thanks for reading
Upvotes: 2
Views: 1520
Reputation: 1
#include <iostream>
int main()
{
int counter = 0;
for (int balance1 = 100, balance2 = 100; balance2 <= balance1; balance1++ , balance2++)
{
balance1 += 10;
balance2 *= 1.05;
counter++;
}
std::cout << counter << " years" << std::endl;
return 0;
}
Upvotes: 0
Reputation: 8805
dInv
is 0. cInv
is also 0. Is 0 < 0
, this is false, so the while loop exits. You can fix your current problem with:
while (cInv <= dInv)
Upvotes: 2