I am writing a program where I need to start with .01 and double it every day thereafter for 64 days. I have all the output showing up like it is supposed to except I can't get the money side to work right. Could someone help, please?
Doubling equation for C++ program.
Collapse
X
-
I need to make the program look like:
Day Amount Owed
1 .01
2 .02
3 .04
4 .08
all the way to day 64.
I did a for loop and my program looks like it is supposed to. My problem is that I can't figure out how to start out at .01 and get it to double each day after. Maybe someone could help me with an equation or maybe I need to use a different kind of loop?Comment
-
Need help with C++ program.
The program I am working on says a king gives a beggar one cent and double the amount for 64 days. I have to write a program that will display how much the king must pay on each day. My problem is I can't figure out how to get the amount owed to start at .01 and then double every day. Here is what I got so far. I have to turn this in in 27 minutes so any help would be useful.
[code=cpp]
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int MAXNUMS = 64;
int num;
double amount;
amount = .01;
cout << "Day Amount Owed\n"
<< "--- -----------\n";
for (num = 1; num<=MAXNUMS; num++)
cout << setiosflags(ios ::fixed)
<< setiosflags(ios ::showpoint)
<< setw(2)<< num << " "
<< setw(8)<< setprecision(2) << amount*2 << " " <<endl;
return 0;
}[/code]Comment
-
Your code simply does the math and prints out the result of amount*2 and does not store the result nor modify the amount. What you need to do is store the result of the math and then print it out. Such as:
So the result of the math portion (amount * 2.0) is assigned into the variable amount. A shorthand of doing manipulations like this is:Code:amount = amount * 2.0;
Same result as the *= just means multiply the left hand side by the right hand side and store into the left hand side. Similarly for +=, -= and /=. If your math is more complex use the first one as its easier to read.Code:amount *= 2.0;
Then your variable value changes each iteration of the loop so on the second pass you get the result of (0.1* 2.0 * 2.0) like you want.Comment
-
You cannot do it on the same line as your output. Thus you would have 2 lines in your loop, which means you need to use the { and } around it. This signals to the compiler that all the code within the { and the } is part of the loop (same is true of if statements).
If you still have trouble post your code using the assignment I showed and the compiler error.Comment
-
Technically not all of the time, but avoiding this usually results in hard to understand code. So I normally try to break things up into separate lines. So this would be a case for something like:
You could even put more code in the { and } if you needed it. In C/C++ we surround blocks of code with the brackets as opposed to something like Python where it uses the indention to show the blocks. You have already used this when defining your function, which itself is just a block of code.Code:for (num = 1; num<=MAXNUMS; num++) { amount *= 2.0; cout << .... (the rest of your output here); }Comment
-
You are very helpful. This is what I got but when I run it it doesn't start at .01 it starts at .02. I need it to start at .01.
[code=cpp]
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int MAXNUMS = 64;
int num;
double amount;
amount = .01;
cout << "Day Amount Owed\n"
<< "--- -----------\n";
for (num = 1; num<=MAXNUMS; num++)
{
amount *=2.0;
cout << setiosflags(ios ::fixed)
<< setiosflags(ios ::showpoint)
<< setw(2)<< num << " "
<< setw(8)<< setprecision(2) << amount << " " <<endl;
}
return 0;
}[/code]Comment
-
Swap the position of the assignment and output operator. You are doubling the value then printing it out. You want to do the opposite.Comment
-
Dud you swap the order inside the loop where you do amount *= 2.0? Post the code again. Also use the code tags (the # symbol in the button bar) its a bit easier to read.Comment
Comment