exercise

I have an exercise again and I got blocked again. The exercise 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.
And I wrote this code that is not working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include<iostream>
 #include<cmath>
using namespace std;
int main()
 {
      double deposit1, deposit2, interest1 = 10, interest2= 5,  simple, compound, years =1;
      cout<< " Enter your deposit for first choice "<< endl;
      cin>> deposit1;
      cout<< " Enter your deposit for the second choice "<< endl;
      cin>>deposit2;
      
    while(simple > compound) 
     {
     simple = deposit1 + deposit1 * interest1/100 * years;
    compound = deposit2 * pow(1.00 + interest2/100, years);
    years++;
    
        }

   cout<<years<<endl;


   system ("pause");
   return 0;
}
You need to initialise your variables. simple = compound = 0;
Still doesn't work
closed account (Lv0f92yv)
Could you explain what about it doesn't work? Is the compiler complaining, are you getting the wrong output, are the calculations off?

This will help us help you.
I modified it a little bit. I added screen output for every time the loop runs so that I can understand what is happening to the numbers. Here is the what I coded. Please check if this works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<cmath>
using namespace std;
int main()
 {
      double deposit1, deposit2, interest1 = 10, interest2= 5,  simple, compound, years =1;
      cout<< " Enter your deposit for first choice "<< endl;
      cin>> deposit1;
      cout<< " Enter your deposit for the second choice "<< endl;
      cin>>deposit2;
      simple=deposit1;
      compound=deposit2;
      
     do
     {
     simple = simple + (deposit1 * interest1/100) ;
     cout << "simple is " << simple << "\n";
     compound = deposit2 * pow(1.00 + interest2/100, years);
     cout << "compound is " << compound << "\n";
     years++;
     }
     while(simple > compound); 

   cout << " It took " <<years<<" years\n";

   system ("pause");
   return 0;
}
Also my school mathematics is quite rusty. So for compound interest I relied on your formula. So that part I don't have much knowledge about.
Last edited on
That is great, thank you. It works fine. It seems that I didn't find that "do". But now it's clear.
Thanks again.
So the whole key was in this;
simple=deposit1;
compound=deposit2; because I modified it like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 #include<cmath>
using namespace std;
int main()
 {
      double deposit1, deposit2, interest1 = 10, interest2= 5,  simple = 0, compound = 0, years=1;
      cout<< " Enter your deposit for first choice "<< endl;
      cin>> deposit1;
      cout<< " Enter your deposit for the second choice "<< endl;
      cin>>deposit2;
     simple=deposit1;
     compound=deposit2;
    while(simple > compound) 
     { 
                  simple = deposit1 + deposit1 * interest1/100 * years;
    compound = deposit2 * pow(1.00 + interest2/100, years);
                
                  years++;
    
        }

   cout<<years<<endl;


   system ("pause");
   return 0;
}
and it works just fine
Last edited on
np. eduard :)

Yeah I changed your while loop into a do while loop- that was one thing.
Another thing, I also changed your simple interest formula. If you compared your code and mine you will see the differences. When I ran the loop with your simple interest formula, the simple deposit amount was not changing at all. This was because each time the loop ran the variable simple got reset back to the value of deposit1
Last edited on
It is only working for some values of deposits. If you choose deposit1 < deposit2, then the exit condition for the while loop is met immediately (simple < compound) and the loop doesn't run. Test your program with many values, including extreme ones. When you can't make it crash, then you can say your program works.
Topic archived. No new replies allowed.