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

C++ halp

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 5
Thread images: 1

File: 1451282081009-1.jpg (69KB, 604x604px) Image search: [Google]
1451282081009-1.jpg
69KB, 604x604px
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 posts: 5
Thread images: 1


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.