This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++] do/while help!(solved)  (Read 2725 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++] do/while help!(solved)
« on: 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:

Code: [Select]
#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
« Last Edit: April 15, 2011, 04:13:17 AM by gh0st »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [C++] do/while help!
« Reply #1 on: 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
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C++] do/while help!
« Reply #2 on: 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.

Code: [Select]
#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;
}

Offline dubblea13

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: [C++] do/while help!
« Reply #3 on: April 15, 2011, 02:40:48 AM »
i think this will work. I haven't tried to compile though.

Code: [Select]
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;
}
« Last Edit: April 15, 2011, 02:41:26 AM by dubblea13 »

Offline FuyuKitsune

  • Knight
  • **
  • Posts: 292
  • Cookies: 21
    • View Profile
Re: [C++] do/while help!
« Reply #4 on: April 15, 2011, 03:24:15 AM »
Does the problem specify "for" or "do...while"? I did this one with a for

Code: [Select]
#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

Code: [Select]
    for(x=1; cleo<=daphne2; x++){
        daphne2 += daphne * d_rate;
        cleo += cleo * c_rate;
    }
« Last Edit: April 15, 2011, 03:25:17 AM by FuyuKitsune »

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: [C++] do/while help!
« Reply #5 on: 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++
« Last Edit: April 15, 2011, 05:42:22 AM by gh0st »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.