ccountcountercs50greedy

What is wrong with my CS50 cash week 1 problem?


This is Cs50 problem set1 cash. i don't get it, if you get the largest coins then for 73 shouldn't it be ( 2 qualters, 2dimes and 3 pennies) and for 28 shouldn't it be (1 qualter, and 3 pennies). can anyone help me with the logic behigh why 7 dimes for 73 and 5 nikkels for 28. many thanks

this is my code

#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);
    printf("quarters: %i\ndimes: %i\nnickels:%i\npennies:%i\n ", quarters, dimes, nickels, pennies);
}
int quarters, dimes, nickels, pennies;
int get_cents(void)
{
    int cents;
    do
    {
        cents = get_int("Change owed: ");
    }
    while (cents < 0);
    return  cents;
}

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

int calculate_dimes(int cents)
{
    // TODO
    dimes = (cents % 25) / 10;
    return dimes;
}

int calculate_nickels(int cents)
{
    // TODO
    nickels = (cents % 25 % 10) / 5;
    return nickels;
}

int calculate_pennies(int cents)
{
    // TODO
    pennies = cents % 25 % 10;
    return pennies;
}

this is the testing of cs50

:) cash.c exists
:) cash.c compiles
:) get_cents returns integer number of cents
:) get_cents rejects negative input
:) get_cents rejects a non-numeric input of "foo" 
:) calculate_quarters returns 2 when input is 50
:) calculate_quarters returns 1 when input is 42
:) calculate_dimes returns 1 when input is 10
:) calculate_dimes returns 1 when input is 15
:( calculate_dimes returns 7 when input is 73
    expected "7", not "2"
:) calculate_nickels returns 1 when input is 5
:( calculate_nickels returns 5 when input is 28
    expected "5", not "0"
:) calculate_pennies returns 4 when input is 4
:) input of 41 cents yields output of 4 coins
:) input of 160 cents yields output of 7 coins

this is my out put

cash/ $ ./cash
Change owed: 73
7
quarters: 2
dimes: 2
nickels:0
pennies:3

 cash/ $ ./cash
Change owed: 28
4
quarters: 1
dimes: 0
nickels:0
pennies:3

Solution

  • The automated judge is testing the routines individually. When calculate_dimes is passed 73, it ought to return 7. It should not use % 25 to reduce the amount. You should remove the % operations from the subroutines.