Student Programmer, help with math step

Pages: 12
I am writing a program for class were the user earns 1 for the first day of work and 2 pennies on the second and 4 pennies on the 3 day doubling every day after that. My problem is, is that I can't figure out how to make the program double i tried using the pow function but that did work. Help please.
How about a simple multiply the previous value by 2?
This is what i thought of but it doesn't do what I want.

pennies = (days * dailyPay); // dailyPay=1
for(day = 1; day <= days; day++)

cout << setw(3) << day << setw(10) << pennies*2 << endl;


Here is my full code:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int days, pennies, dailyPay=1, day;

cout << "How many days has the employee worked this month?: ";
cin >> days;

cout << "Days Worked Pay" << endl;
cout << "-----------------------" << endl;

pennies = (days * dailyPay);
for(day = 1; day <= days; day++)

cout << setw(3) << day << setw(10) << pennies*2 << endl;


system("pause");
return 0;
}
Well, unless I misunderstood the question, dailyPay=1 seems sensible, then each day, the value of dailyPay needs to be multiplied by 2, for example dailyPay *= 2;
still isn't right
it gives the total for all days worked instead of showing the pennies for that day.
Well, I tend to give hints rather than complete answers, because I feel the best way to learn is to try to figure things out for yourself. Can you show the updated version of the code please.


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
int days, dailyPay=1, day;

cout << "How many days has the employee worked this month?: ";
cin >> days;

cout << "Days Worked Pay" << endl;
cout << "-----------------------" << endl;


for(day = 1; day <= days; day++)
{int pennies = 1;
cout << setw(3) << day << setw(10) << pennies << endl;
}

system("pause");
return 0;
}
I can get it to count the days my problem is getting it to double the pennies each I am not quit sure how to go about that it should go 1, 2, 4, 8, 16, 32 ....
You should read Chervil's first post again. You will realized you haven't incorporated his suggestion into your code yet. ;)
I am not understanding where it should be placed. Everywhere I have tried to add it or replace something with it I get errors. And no matter where i try it i get this error 1 IntelliSense: operand of '*' must be a pointer.
here's my new code so far. Its getting better but its still not right it counts my days correctly but my income looks like this
1, 2, 12, 573, 1.65888e+006 instead of 1, 2, 4, 8, 16, 32......


int main()
{
double days, dailyPay=1, day, pennies = 1;
cout << "How many days has the employee worked this month?: ";
cin >> days;

cout << "Days Worked Pay" << endl;
cout << "-----------------------" << endl;


for(day = 1; day <= days; day++)
{
pennies = day * dailyPay * pow (pennies, 2); // I changed it here.


cout << setw(3) << day << setw(10) << pennies << endl;
}

system("pause");
return 0;
}
Maybe you're overthinking things a little.
Consider this (rather simple) program:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using std::cout;

int main()
{
    int num = 0;
    for (int day = 0; day < 10; ++day)
    {
        cout << num << ' ';
        ++num;
    }
}
0 1 2 3 4 5 6 7 8 9

It prints 0 on the first day and then for each day after that for 10 days, it prints one more than the previous day's value.

Now, you want it to double every day instead of just going up by 1.
Do you see what your code should look like?

hopefully I'm not giving away too much...
1.)Declare and define a constant variable named "days", or have the user input a number.

2.)Define two variables which hold the current amount of pennies, and the number of pennies you earn each day. Some possible names are "pennies" and "profit".

3.)Initialize profit to 1.

4.)Write a for loop, which does the following while a counter is less than "days".

5.)Add the profit to the current amount of money.

6.)Multiply the profit by two.

7.)Print out the current amount of money.

8.)Tada
it has to display only that days pay like on day 1 it will say 1 on day 2 the pay is 2 on day three the pay 4 and the pay doubles each day. But you have to display each days pay. it counts my days. My problem is in squaring the pennies each day but if you square 1 the answer is 1 I can't get it to double it for each day. Nothing I am trying is working.
#removed | edited | reposted below
Last edited on
I've a feeling you're missing something very straightforward here. The idea of squaring a number is pretty much off-topic and not needed here. Instead of squaring 1
1 * 1 = 1
just multiply 1 by 2
1 * 2 = 2
on the next day, multiply the previous value by 2
2 * 2 = 4
and the day after that, multiply the previous value by 2
4 * 2 = 8
and the day after that, ...
@ksm347
I've had a long day, and maybe I'm not fully awake here. However, the series of values you supplied for pow(day,2)
1 4 8 16 32 64


is not the same series that I get when I try it?
pow(day,2)
1 4 9 16 25 36
Edited

@cheril....thanks for pointing out....i think i went the other way round....oops

@kmartar....
But you have to display each days pay


try using the pow(2,day) instead of pow(day,2)



Days          Penny                 pow(2,day)
          (what it should be)     (what it gives)
1             1                          2
2             2                          4
3             4                          8
4             8                          16
5            16                          32
6            32                          64


now you'll see what's wrong....pow(2,day) doubles what you are trying to get.....so you know what needs to be done...

if you are trying to get all the days throw it in a loop.....other wise it'll give you result for one day easily with max 6 lines of coding...
When I use the pow function it always starts with 2 I need the pay to start with 1. then 2,4,8.....
I have tried to plug it in different ways and have even changed it around changed numbers tried to set pennies equal to one( when I do all days keep one. i still have no clue as to how to make this work.
Last edited on
Forget the power function completely. It does not need to be used in this program.

Now, think of it in the real world.

1. First day, you start with one.
2. Second day, the one you have doubles into 2 (you multiplied your current pay by two
3. Third day, the two doubles (you multiplied your current pay by two)
4. Fourth day, the four doubles (you multiplied your current pay by two)

Sense a pattern?

*cough* You multiply your current pay by two each time *cough*

Now, fit this in a for loop and output it each time and you are done.
Pages: 12