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;
}