[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
C++ halp

You are currently reading a thread in /wsr/ - Worksafe Requests

Thread replies: 5
Thread images: 1
File: 1451282081009-1.jpg (69 KB, 604x604) Image search: [Google]
1451282081009-1.jpg
69 KB, 604x604
What am I doing wrong?

***********************************************************
#include <iostream>

using namespace std;

int main()
{
float years = 1;
float goal = 50000;
float deposit = 3000;
float interest = ((years * deposit) * (4.2 / 100));
float balance = ((years * deposit) + interest);
while (goal > balance)
{
years++;
}
cout << "Your goal is reached after " << years << " years." << endl;
cout << "Your balance will be " << balance << " dollars.";
}

***************************************************************

I should end up with 13 years and 50513.15 dollars.
>>
>>41530
>***********************************************************
>#include <iostream>
>using namespace std;
>int main()
>{
>float years = 0;
>float goal = 50000;
>float deposit = 3000;
>float interest, balance;
>while (goal > balance)
>{
>years++;
>interest = ((years * deposit) * (4.2 / 100));
>balance = ((years * deposit) + interest);
>}
>cout << "Your goal is reached after " << years << " years." << endl;
>cout << "Your balance will be " << balance << " dollars.";
>}
>***************************************************************
>I should end up with 13 years and 50513.15 dollars.

Statements like "interest = ((years * deposit) * (4.2 / 100));" don't create a function that tell the compiler how to calculate the Left-Hand Side every time it's needed, they tell the compiler to evaluate the RHS once, and store that value in the LHS when that line is executed.

So if you want to do the calculation every time in the loop, you need the statement to be inside the loop body.

Also you're calculating compound interest wrong, but you asked about your code, not your algorithm.
>>
>>41539
Thank you so much!!
Though, what's wrong with the algorithm?
>>
>>41530
This would be how you calculate the interest correctly.
>===================

#include <iostream>

using namespace std;

int main()
{
int years = 0;
float balance = 0;

const float goal = 50000; // the program's not supposed to change these values, so mark them const
const float deposit = 3000; // this means that even a dumb compiler will inline them, saving memory
const float interest_rate = 1 + (4.2/100); // and will warn if we change them when we're not supposed to

while (balance <= goal) // constants on LHS is safer, sure, but RHS is congruent to English's "While the <whatever> is <whatever>" syntax
{
years += 1; //++ for when you're using the value in an expression, += for when you're not
balance += deposit;
balance *= interest_rate;
//cout << "It's year " << years << ", and you have a balance of" << balance << " dollars."<< endl;
}
cout << "Your goal is reached after " << years << " years." << endl;
cout << "Your balance will be " << balance << " dollars.";
}
>>
>>41542
interest = ((years * deposit) * (4.2 / 100));
balance = ((years * deposit) + interest);

so balance = ((years * deposit) + (years * deposit) * (4.2 / 100));
so balance = ((years * deposit) * (1+ (4.2 / 100)));
so balance = ((years * deposit) * (1.042));

This isn't correct, because it's only applying the interest for each yearly deposit once, on the year it's deposited. With compound interest, each year's deposit gets interest added every year, not just once. So on year five:

year 5 is compounded 1 time
year 4 is compounded 2 times
year 3 is compounded 3 times
year 2 is compounded 4 times
year 1 is compounded 5 times

And so on.
Thread replies: 5
Thread images: 1

[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
If a post contains illegal content, please click on its [Report] button and follow the instructions.
This is a 4chan archive - all of the content originated from them. If you need information for a Poster - you need to contact them.
This website shows only archived content and is not affiliated with 4chan in any way.
If you like this website please support us by donating with Bitcoin at 1XVgDnu36zCj97gLdeSwHMdiJaBkqhtMK