For each of the problems below you are to consider making a function to solve th
ID: 3862094 • Letter: F
Question
For each of the problems below you are to consider making a function to solve the problem. As before, state the purpose of the function in your own words. Next specify input that is needed for the function to complete the task, and state what input should be passed to the function as parameters and what would be acquired inside the function using the input function. Specify the expected output and state if the output will be returned to the function call or be sent to the monitor. Finally give the step by step process that will obtain the output from the input (the algorithm). In addition to these 4 items also specify test data that can be used for each function. Remember to describe your steps in enough detail so someone other than you could follow your algorithm and solve the problem. Also do not write any actual code.
Problem 1: Describe a function that will accept change needed from a purchase as a whole number from 0 to 99 inclusive (you do not have to check the validity of this value). A The function determine the number of quarters, number of dimes, number of nickels, and number of pennies needed to make the change if you want to minimize the total number of coins used. The number of each coin needed should be sent back to the function call.
Explanation / Answer
Test Plan:
Purpose of the function: Given the number of pennies, will give the minimum number of coins needed to deliver the specified number of pennies.
Input: An integer value in the range 0 - 99 inclusive.
Output: Four values for number of quarters, number of dimes, number of nickels, and number of pennies, for the given input number of pennies.
Given the input to the function, the function will return the values back to the called function, using call-by-reference.
1. Read in the number of cents.
2. if cents > 100 or less than 0, //This is error checking.
print some error message, and stop execution.
3. numOfQuarters = cents / 25 //This will make as many quarters as possible.
cents = cents % 25 //This will just save the remaining cents.
4. numOfDimes = cents / 10 //Of the remaining cents, make as many dimes as possible.
cents = cents % 10 //This will just save the remaining cents.
5. numOfNickels = cents / 5 //Of the remaining cents, make as many nickels as possible.
cents = cents % 5 //This will just save the remaining cents.
6. pennies = cents. //The remaining cents should be paid as pennies.
Consider an example:
99 cents.
This is a valid number of cents.
When applied step 3.
numOfQuarters = 99 / 25 = 3. So, 3 quarters are made.
cents = 99 % 25 = 24.
So, remaining cents is 24.
When applied step 4.
numOfDimes = 24 / 10 = 2. So, 2 dimes could be made.
cents = 24 % 10 = 4. So, remaining cents is 4.
When applied step 5.
numOfNickels = 4 / 5 = 0. So, no nickels.
cents = 4 % 10 = 4. So, still left with 4 cents.
When applied step 6.
pennies = 4. So, 4 pennies.
So, the change could be made is 3 quarters, 2 dimes, and 4 pennies.
Consider another example:
-5 cents.
This is an invalid number of cents.
So, step 2 fails. It prints a message and will stop executing.
Consider one more example:
49 cents.
This is a valid number of cents.
When applied step 3.
numOfQuarters = 49 / 25 = 1. So, 1 quarter could be made.
cents = 49 % 25 = 24. So, remaining cents is 24.
When applied step 4.
numOfDimes = 24 / 10 = 2. So, 2 dimes could be made.
cents = 24 % 10 = 4. So, remaining cents is 4.
When applied step 5.
numOfNickels = 4 / 5 = 0. So, no nickels.
cents = 4 % 10 = 4. So, still left with 4 cents.
When applied step 6.
pennies = 4. So, 4 pennies.
Therefore, the change made is: 1 quarter, 2 dimes, and 4 pennies.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.