Thread: C code question

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    10

    C code question

    Hello,

    I'm trying to write a program in C that will prompt the user for the number of days in a month, and then ask how many pennies they will receive on the first day. I then have to tell them how much money they will have received by the end of the month if every day the amount of pennies they first received doubles (so if they got 1 penny on the first day, then they'll get 2 on the second, 4 on the third, etc.)

    I'm having trouble figuring out how to find the total amount of pennies they received in the month. I can write a program for what they will receive on the last day of the month, just not the total amount. Can someone direct me in the right direction?

    Thanks!

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Imagine I gave you any arbitrary list of values, say [1, 10, 6, 7, 9] --- how would you find the sum then?
    If you can calculate the amount received on each day it should be trivial; don't try to make this more complicated than it is.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    This sounds suspiciously like a homework assignment. I mean, does anyone code something like this in their free time? =S

    Also, post your effort before asking for help. Even if it's just setting up a loop, or some pseudo-code, it's better than nothing.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    10
    Quote Originally Posted by memcpy View Post
    This sounds suspiciously like a homework assignment. I mean, does anyone code something like this in their free time? =S

    Also, post your effort before asking for help. Even if it's just setting up a loop, or some pseudo-code, it's better than nothing.
    I'm taking one of the free itunesU courses of programming (just starting out) and I'm working through one of the problem sets, but I'm stuck on this one and can't find a solution! So it is a 'homework' question, but I'm not actually in school, just personal interest.

    I know the psudocode is something like this:

    get number of days
    get number of pennies
    for (each day)
    double pennies
    update total
    output total dollars and cents

    I've got the first two steps, but I can't figure out how to keep on adding to a total without using an array or list or something like that. Any suggestions? Thanks!

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Fair enough, try using this framework and tell me how it goes:

    Code:
    uint64_t total = 0;
    uint32_t temp = 0; 
    uint8_t day = 30;
    
    while (day--) {
        /* your code to get the power goes here, store it in temp */
        total += temp; 
    }

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    10
    That works! Thanks a lot for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about c++ code and what to use
    By hunterx1x in forum C++ Programming
    Replies: 10
    Last Post: 02-19-2010, 03:04 PM
  2. Code Question
    By lyoncourt in forum C Programming
    Replies: 3
    Last Post: 09-16-2007, 04:33 PM
  3. My First If Code, Question? :P
    By dimirpaw in forum C++ Programming
    Replies: 3
    Last Post: 11-29-2005, 08:49 PM
  4. Code Question
    By Drew in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2003, 11:59 AM
  5. question about my code
    By killerasp in forum C++ Programming
    Replies: 7
    Last Post: 02-18-2002, 08:05 PM