day+previous day
Hi,
In my program I would like to accumulate the total rice earnings. I cant seem to understand the logic. Can someone point me in the right direction?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int day;
double grains, accum;
cout << "Day Grains Accum" << endl;
for (day = 0; day <= 17; day++)
{
grains = pow(2.,day), accum = day+grains; //accum needs to be grains given per day + grains given the previous day
cout << (day + 1) << setw(9) << grains << setw(14) << accum << endl;
}
system("PAUSE");
}
|
Last edited on
fishalot79
This bit:
grains = pow(2.,day), accum = day+grains;
should be 2 statements:
1 2
|
grains = pow(2.0,day);
accum = day+grains; //not doing what you think - an unneeded variable?
|
If you named your variables Day, Total, DailyAmt - would that make it clearer?
Another clue - make use of the += operator.
Good Luck
Last edited on
1 2 3 4
|
grains = pow(2.0,day);
accum += grains;
|
Thank you very much!
Last edited on
Topic archived. No new replies allowed.