Similar Posts

Leave a Reply

Your email address will not be published.

5 Comments

  1. Hello,

    First off, basically thanks for the answer to the ‘pest’ lol…

    I am starting to work on CS50 Cash example and basically worked thru your tutorial.
    I am struggling to understand a few concepts of code however.. I am hoping you have the patience to answer several questions. My apologies for being so noob.

    To start, can you confirm that the first lines of code at the top are functions or are they variables??
    My guess is that they are functions?

    int get_cents(void);
    int calculate_quarters(int cents);
    int calculate_dimes(int cents);
    int calculate_nickels(int cents);
    int calculate_pennies(int cents);

    Next, I am struggling with the idea of the variable of ‘cents’ and I am hoping you can confirm a few things:
    1. Is the cents variable where are not supposed to edit the same variable that is passed down into the area of code where we are supposed edit? OR are they separate?
    2. In section where we are supposed to edit between quarters, dimes, etc….I struggle to understand where the remainder is stored. Do we assume that after the math is performed in each coin section (cents = cents – (coin worth);) the remainder is still stored and carries thru to the next coin section?

    I feel like as I analyze the code I understand it but I can never seem to do it on my own.

    Thanks for your help! I hope you don’t mind if I bug you in the future!

    1. Hi Wave,

      Thank you for your question.

      To begin with, the first lines of the codes are neither functions nor variables. They are simply function declarations to let the compiler know that there will be such function later. That way no error is caused when we use/call those functions in the main() function.

      For your next question, you need to understand passing by value:
      When local variables are passed to custom functions, their value is not changed. The called function called receives a copy of the variable to do math with, the original value of the local variable ,in main for example, remains the same.

      That is:

      When cents is passed to calculate_quarters(), for example, calculate_quarters() receives only a copy of it. Lets imagine the cents is 56. So calculate_quarters() gets 56 as a copy, and it performs the math, i.e. subtracts as many quarters as it can, let’s imagine the remaining cents is 6. Recall that main() still has cents as 56. Then, calculate_quarters() returns the quarters with return quarters;. The main still has cents as 56.

      That data now, is used in main to update the cents with “cents = cents – quarters * 25;”. Now the new cents is 6, and that remaining cents (6) is used to calculate dimes, nickels, and pennies respectively.

      I hope this answered your question. If you still don’t understand, please follow up: respond to this reply.

  2. Well, i’m a newbie and was doing “Cash” with a method of “try and see”. I coded way less calculation, but somehow it’s still working as it should (i guess), judging by results and check50. Here is my version.

    #include
    #include

    int get_cents(void);
    int calculate_quarters(int cents);
    int calculate_dimes(int cents);
    int calculate_nickels(int cents);
    int calculate_pennies(int cents);

    int main(void)
    {
    // Ask how many cents the customer is owed
    int cents = get_cents();

    // Calculate the number of quarters to give the customer
    int quarters = calculate_quarters(cents);
    cents = cents – quarters * 25;

    // Calculate the number of dimes to give the customer
    int dimes = calculate_dimes(cents);
    cents = cents – dimes * 10;

    // Calculate the number of nickels to give the customer
    int nickels = calculate_nickels(cents);
    cents = cents – nickels * 5;

    // Calculate the number of pennies to give the customer
    int pennies = calculate_pennies(cents);
    cents = cents – pennies * 1;

    // Sum coins
    int coins = quarters + dimes + nickels + pennies;

    // Print total number of coins to give the customer
    printf(“%i\n”, coins);
    }

    int get_cents(void)
    {
    int cents;
    while (true) //Loop with condition of a value being positive
    {
    cents = get_int(“Change owed: “); //Prompting user for a height value
    if (cents >= 0) //Value must be positive
    {
    break; //If negative, loop breaks
    }
    }
    return cents;
    }

    int calculate_quarters(int cents)
    {
    return cents / 25;
    }

    int calculate_dimes(int cents)
    {
    return cents / 10;
    }

    int calculate_nickels(int cents)
    {
    return cents / 5;
    }

    int calculate_pennies(int cents)
    {
    return cents / 1;
    }

  3. I think this is one of the most important information for me. And i am glad reading your article. But want to remark on some general things, The web site style is ideal, the articles is really nice : D. Good job, cheers