1

EDITED FOR CLARITY'S SAKE. I APOLOGIZE FOR THE CONFUSION :3

Okay, so I'm following an online CS class, and we're supposed to write a program, in C, that will tell you how much money you'd have if you had a penny at the beginning of the month and doubled it every day.

Each day you would get double what you had yesterday PLUS everything from the previous days.

Example: You start with .01 and what to calculate a running total by day 3. So the first day is .01, second day is .02, third day is .04. On day 3 you would have 0.01+0.02+0.04 (.09).

The program intends to calculate this process over the duration of any given month (28 - 31 days).

I'm having a really hard time trying to implement this. I've got it doubling it, but I'm not sure how to the previously-calculated days together.

Here's my code:

#include <stdio.h>
#include <math.h>

int main(void) {

    /*days represents total days in months*/
    /*pens represents the number of pennies on the first day*/

    long long days;
    long long pens;

    do {
      printf("Enter the number of days in the month: ");
      scanf("%llu", &days);
    } while(days < 28 || days > 31);    

    printf("Enter the initial number of pennies: ");
    scanf("%llu", &pens);

    for (int i=0; i<= days-1; i++) {
      pens += pow(2,i);
      printf("You'll have $%llu\n", pens);
    }
}

edit2: Okay, so I think I fixed it thanks to all your awesome advice. I changed the last part to:

for (int i=0; i<= days-1; i++)
        {
            pens = pens + (pens * 2);

        }
        total = pens / 100;
        printf("You'll have $%.2f\n", total);

    } 

Though there is still a slight issue with the output (which, I'm thinking, is due to the data type I'm using?) It prints out:

You'll have $0.00 You'll have $0.00 You'll have $0.00 You'll have $0.00 You'll have $2.00 You'll have $7.00 You'll have $21.00 You'll have $65.00 You'll have $196.00 You'll have $590.00 You'll have $1771.00 You'll have $5314.00 You'll have $15943.00 You'll have $47829.00 You'll have $143489.00 You'll have $430467.00 You'll have $1291401.00 You'll have $3874204.00

etc.

Pretty good, but I'm betting it's not that accurate since the first few iterations are 0.00.

10
  • I don't understand the setup. How much would you have on day 3?
    – Kerrek SB
    Commented Sep 10, 2012 at 6:01
  • @KerrekSB you would have 0.01 + 0.02 + 0.04, assuming you start at day 1 instead of day 0
    – tekknolagi
    Commented Sep 10, 2012 at 6:02
  • @tekknolagi: So the question should be, "I receive a doubling amount of money every day"?
    – Kerrek SB
    Commented Sep 10, 2012 at 6:04
  • @KerrekSB sounds right. Edit it if you'd like, though I see no need to. He's new, but he's doing an alright job.
    – tekknolagi
    Commented Sep 10, 2012 at 6:04
  • @KerrekSB though the title does in fact make 0 sense
    – tekknolagi
    Commented Sep 10, 2012 at 6:05

4 Answers 4

5

Well, let's translate what you have to do into pseudocode:

var daily_amount = 0.01
var total = daily_amount

iteration_over_days:
    daily_amount *= 2
    total += daily_amount

From there, all you need to do is translate to C.

Enjoy!

Instead of using the long datatype, you can also start at 1 and then at the end divide by 100: $0.01 -> $1

8
  • This is basically what the OP's code does, and is not correct.
    – Jim Balter
    Commented Sep 10, 2012 at 6:14
  • Is this not what he is trying to accomplish?
    – tekknolagi
    Commented Sep 10, 2012 at 6:16
  • @JimBalter forgot to tag you.
    – tekknolagi
    Commented Sep 10, 2012 at 6:18
  • I don't know ... his or her problem statement says something different from his or her example. It would help if the OP had said what was wrong with his/her output.
    – Jim Balter
    Commented Sep 10, 2012 at 6:20
  • I think he mistyped his English explanation, as his code pretty much says what mine did, and this is a typical problem given out for homework.
    – tekknolagi
    Commented Sep 10, 2012 at 6:24
0

I think the following line needs change:

pens+=pow(2,i);

It should be:

pens = pens + (pens * 2)

The logic is that if you start with 1 penny on day 1, then it will be something like this

Day 1 : 1 + (1 * 2) = 3
Day 2 : 3 + (3 * 2) = 9
Day 3 : 9 + (9 * 2) = 27
and so on..

I hope this is the logic which you are expecting. By doing pow(2,i), you are just adding square of the day count to your total money each day which does't seem to be the expected logic based on your question.

1
  • 'square of the day count ' -- no, that's pow(i, 2). The problem with pow(2, i) is that it isn't multiplied by the initial number of pennies, so it's wrong if that isn't 1.
    – Jim Balter
    Commented Sep 10, 2012 at 6:41
0

This is the code you are looking at:

total = 0;
for (int i = 1; i <= days; i++) {
     total += pens;
     pens = pens*2;
}
printf("You'll have $%d\n", total);
10
  • pens += pens*2 is the same as pens *= 3. I wonder if the OP is stating the problem correctly.
    – Jim Balter
    Commented Sep 10, 2012 at 6:12
  • I was confused in the beginning, I think above is the code he is asking for - double amount of each day and add up. I have edited it. Commented Sep 10, 2012 at 6:15
  • Your edit produces the same result as the OP's code. The OP's problem statement says one thing, but the example says another. It's very unclear.
    – Jim Balter
    Commented Sep 10, 2012 at 6:18
  • Sorry if I'm unclear, it's pretty late where I'm at, so I'm probably not at my best. To sum it up succinctly: every day, the initial penny (or amount of pennies) doubles. So first day is .01, next is .02, next is .04, .08, 16, etc. Then I want to add all the days up (.01 + .02 + .04) once we reach the end of the month. This is like my 2nd day at attempting to learn to program, so I'm probably overthinking it.
    – Aweary
    Commented Sep 10, 2012 at 6:19
  • @Kierkegaurd That's what your code does ... I've run it and it gives the output you say you want. But that description doesn't match what your question says is the problem statement ... which is adding twice the total to the total.
    – Jim Balter
    Commented Sep 10, 2012 at 6:21
0

Don't compute the power each time! Just keep track of a running count and double it every day.

Here's the core of the algorithm you need for total_days number of days:

typedef unsigned long long int my_uint;

my_uint initial_payment = 1;
my_uint total_pennies = 0;

for (my_uint day_payment = initial_payment, day = 0; days != total_days; ++day)
{
    total_pennies += day_payment;
    day_payment *= 2;
}

// now have total_pennies

Of course you could notice that 1 + q + q2 + q3 + ... + qN − 1 is the same as (qN − 1) / (q − 1) and thus compute the answer in one single step (e.g. for q = 2 in your case and N number of days).

Not the answer you're looking for? Browse other questions tagged or ask your own question.