I want to understand the code give by the problem set and try to figure it out from there. I can't even start solving the problem since I don't understand the code given. I'm having a difficult time wrapping my head around all these return operators and the calculation method for figuring out the cents for quarters, dimes, nickels, and pennies.
The code should, when I write the code in the todo and return sections, then prompt the user to ask how many cents are owed with an output of "Cents owed: ". It should also return the least amount of coins needed for cents owed using the greedy algorithm.
Main is already implemented but calls to functions that aren't implemented, including get_cents()
, calculate_quarters()
, calculate_dimes()
, calculate_nickels()
, and calculate_pennies()
.
I have an idea of using division to find the cents. I'd divide the cents the user inputted by 25 since that's the biggest being the quarter. Then it would return a positive integer. Additionally, I would do cents owed - 25. I would use that number to divide by 10 being the dime to find how many dimes I need to use and return a positive integer. I would keep moving down the stack until I reached pennies. I don't know how I'm gonna implement this yet but is my thought process in the right place?
#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
return 0;
}
int calculate_quarters(int cents)
{
// TODO
return 0;
}
int calculate_dimes(int cents)
{
// TODO
return 0;
}
int calculate_nickels(int cents)
{
// TODO
return 0;
}
int calculate_pennies(int cents)
{
// TODO
return 0;
}
CodePudding user response:
I believe that you are right, but I'll just reiterate for others who might have not read your question thoroughly:
The idea is that each function should return the most coins (of a specific type) for a balance, but not go over. For example, calculate_quarters(76)
should return 3
because 3 quarters make 75 cents and the next increment would have to be 100 cents.
Here's the coding part though: in C, dividing integers always returns an integer, so 76 / 3
will not return 25.3333... because 25.3333... is not an integer. Instead, the decimals will be truncated, so 76 / 3
evaluates to 3
. Notice that this will always round down the number.
In your case, this functionality makes the programming easier. Instead of explicitly rounding down, you can simply divide cents
by the coin's value. Here's an example of the completed code:
#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) {
int cents = 0;
scanf("%d", ¢s);
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; }
// NOTE: "cents / 1" is the same as "cents", so no need for division here
int calculate_pennies(int cents) { return cents; }
PS: For convenience, I also wrote get_cents()
. That function has nothing to do with this answer.