EvilZone

Programming and Scripting => C - C++ => : gh0st April 14, 2011, 12:00:13 AM

: [C++] do/while help!(solved)
: gh0st April 14, 2011, 12:00:13 AM
hello guys well this time Im doing this problem:

3. 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.


so I did:

:
#include <iostream>

const float s = 10/100;
const float c = 5/100;

int main()
{
    using namespace std;
    int Cleo = 100;
    int daphne = 100;
    do{
        Cleo * c;
        daphne * s;
       
        }while(Cleo!=daphne);
        cout << Cleo;
        cout << daphne;
        cout << endl;
   
    system("pause");
    return 0;
}

now the problem is that Idk if the output was ok and I dont know if thats correct plz somebody explain me what should be the correct way to solve this !.

awards : +1 of karma
: Re: [C++] do/while help!
: bluechill April 14, 2011, 07:07:30 PM
P(1 + (r/n))^nxt

thats the compound interest formula right there,.

P = initial investment
r = rate (ie .1)
n = times a year
t = years
: Re: [C++] do/while help!
: gh0st April 15, 2011, 01:22:57 AM
bluechill, I dont think that thats the problem the problem is to make the program to show off the output when Cleo and Daphne have the same amount of money I think that I have to use "for" but in the correct way and Ive run a program that doesnt seem to be the answer.

:
#include <iostream>

using namespace std;

int main()
{
    float c = 0.10;
    float d = 0.5;
    int cleo = 100;
    int daphne = 100;
   
    for (int i=cleo;cleo * c>daphne *d; i--)
    cout << i;
    cout << endl;
    system("pause");
    return 0;
}
: Re: [C++] do/while help!
: dubblea13 April 15, 2011, 02:40:48 AM
i think this will work. I haven't tried to compile though.

:
using namespace std;

int main()
{
int cleo = 100;
int daphne = 100;
int count = 1;

while(cleo < daphne)
{
cleo += 10;
daphne *= 1.05;
count++;
}
cout << "Balance after " << count << " years: ";
cout << "\nCleo: " << cleo << endl;
cout << "Daphne: " << daphne\n\n";
system("pause");
return 0;
}
: Re: [C++] do/while help!
: FuyuKitsune April 15, 2011, 03:24:15 AM
Does the problem specify "for" or "do...while"? I did this one with a for

:
#include <iostream>

const float d_rate = 0.10;
const float c_rate = 0.05;

int main()
{
    using namespace std;
    int daphne = 100;
    int daphne2 = daphne;
    int cleo = 100;
    int x;

    for(x=1; cleo<=daphne2; x++){
        daphne2 = daphne2 + daphne * d_rate;
        cleo = cleo + cleo * c_rate;
    }

    cout << "Years taken= " << x << endl;
    cout << "Daphne's money: $" << daphne2 << endl;
    cout << "Cleo's money: $" << cleo << endl;



    system("pause");
    return 0;
}


Daphne's interest is from the original value so I made a 2nd variable to hold the current amount. Cleo's interest is based on the current value so you only need a single variable for her.

You could also use += in the for loop

:
    for(x=1; cleo<=daphne2; x++){
        daphne2 += daphne * d_rate;
        cleo += cleo * c_rate;
    }
: Re: [C++] do/while help!
: gh0st April 15, 2011, 04:12:08 AM
@FuyuKitsune: yeah thats the answer thank you +1 of karma lol you surprised me I didnt know that you knew C++