CS5O Cash Solution

By Suraj Chaudhary

Are you looking for the solution of Harvard’s CS50 PSet 1? Do you want the Cash solution of CS50?

If so you’re in the right place.

In this article, I’m going to share with you exactly how to solve the PSet 1 CS50 Cash Problem and also share with you the code for the CS50 Cash Solution.

When you start taking CS50, Hello, Mario (less and more), cash, and/or credit are some of the first questions that you will have to submit. For the Mario problem, you can either choose less comfortable or more comfortable.

Remember though about the academic honesty policy: you cannot copy a code from the internet, otherwise, your certificate can be revoked even years after you take the class.

Use this only to understand the problem and the way to solve it. Here’s how to solve the Cash problem in CS50.

Our goal is to find the most convenient way to minimize the number of coins we dispense for each customer: greedy algorithms.

It would appear in our terminal as

$ ./cash
Enter the cents owed: 56
Minimum coins: 4

We will already be provided with a program file. It will not be complete but we will have to do only the custom function parts marked by // TODO.

The first part that we will do is to get the cents owed from the user. To do so we will code in the int get_cents(void) function. Here is the code that we will use for that

int get_cents(void)
{
    // Get the cents from the user
    int cents;
    do
    {
        cents = get_int("Enter Cents Owed: ");
    }
    while (cents < 0);
    return cents;
}

As you can see from the code, first of all, we will prompt the user for the cents owed. The loop will help us to get valid input from the user. We will repeat the prompt unless the user gives a valid input, i.e., till the cents < 0. And then, we will return the valid cents the user provides.

Our next task is going to be to calculate the number of quarters to be given. To do so, we will use the following code:

int calculate_quarters(int cents)
{
    // Calculate the number of quarters
    int quarters = 0;

    while (cents >= 25)
    {
        quarters++;
        cents = cents - 25;
    }

    return quarters;
}

The above code will first check if the cents is more than 25, and while it is true, the program will first increase the number of quarters to be given by 1, and then it will subtract 25 from the cents so that there is no double-counting of the same amount. The loop will continue till cents are greater than or equal to 25. Then, it will return the number of quarters by return quarters;

Our next task is going to be to calculate the number of dimes to be given. We can use the following code to do so:

int calculate_dimes(int cents)
{
    // Calculate the number of dimes to be given
    int dimes = 0;

    while (cents >= 10)
    {
        dimes++;
        cents = cents - 10;
    }

    return dimes;
}

Similar to calculating the quarters, this code will also first check if the number of cents is greater than 10, and while it is true (greater than 10), it will increase dimes by 1 and will subtract 10 from the cents to avoid double counting. After that, it will return dimes.

Now, calculating the nickels and pennies is also going to be similar. We can use the below-given code for it.

int calculate_nickels(int cents)
{
    // Calculating nickels
    int nickels = 0;

    while (cents >= 5)
    {
        nickels++;
        cents = cents - 5;
    }

    return nickels;
}

int calculate_pennies(int cents)
{
    // Calculating pennies
    int pennies = 0;

    while (cents >= 1)
    {
        pennies++;
        cents = cents - 1;
    }

    return pennies;
}

After all of these functions are called, you will have a program that can calculate effectively the number of coins (quarters, dimes, nickels, and pennies) that should be given to the customer. The cash program will work in this way:

$ ./cash
Enter the cents owed: 56
Minimum coins: 4

Here is my entire solution code for CS50 Cash Solution:

#include <cs50.h>
#include <stdio.h>

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)
{
    // TODO
    int c;
    do
    {
        c = get_int("Money owed: ");
    }
    while (c < 1);
    return c;
}

int calculate_quarters(int cents)
{
    // TODO
    int coins = cents / 25;
    return coins;
}

int calculate_dimes(int cents)
{
    // TODO
    int coins = cents / 10;
    return coins;
}

int calculate_nickels(int cents)
{
    // TODO
    int coins = cents / 5;
    return coins;
}

int calculate_pennies(int cents)
{
    // TODO
    int coins = cents / 1;
    return coins;
}

If you have any questions or confusion, feel free to drop a comment down below, and I’ll get back to you as soon as possible. In the meantime, feel free to read these articles.

Also Read

  1. CS50: How To Study At Harvard For FREE (and get a free certificate)
  2. How to use Windows 11 on Mac for Free
  3. How To Get A Domain Name For FREE
  4. How To Use Microsoft Office Apps For FREE Legally (Mac & PC)

Suraj Chaudhary is a writer, developer, founder, and a constant learner. He shares lessons and resources for living a fuller life every week. On this blog, he shares helpful guides and helpful articles that help his 70,000+ monthly readers find answers, solve problems, and meet their curious needs.

8 thoughts on “CS5O Cash Solution”

  1. This is the suitable weblog for anyone who desires to search out out about this topic. You notice so much its nearly onerous to argue with you (not that I really would want…HaHa). You definitely put a brand new spin on a topic thats been written about for years. Nice stuff, just great!

    Reply
  2. 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

    Reply
  3. 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;
    }

    Reply
  4. 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!

    Reply
    • 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.

      Reply

Leave a Comment

Slide to prove you're not a bot/spammer *