CIS071
Lab03 - Value of an English Coin Collection DUE:
(Noon, Wed, Feb 08, 2006)
Practice with functions Your lab assignment is to make changes to the uscoins.c program (p. 67 in the textbook) so that it makes use of functions. Part 1. Practice with a void function
a. Write a void function named coins_instruct and place it after your main function. Your function should be along the lines of the one shown in Fig. 3.16, p. 123, but modified for the coins problem.
void coins_instruct()
{ ... put some instructions for the user in here. See Fig. 3.16 for example.} b. Insert the function prototype line:
void coins_instruct();
before your main function.
c. Insert a call to this function
coins_instruct();
at the beginning of your main function - before you read in the user's initials.
d. Once you have finished doing these steps, compile, link and run your program.
Part 2. Practice with some simple functions that return a type int result.
Write 3 one-line functions that have these descriptions and prototypes. They will be responsible for doing the calculations on p. 70. Each of the assignment statements will be part of a separate function body and your main function will call the functions that contain these assignment statements. a. Here are the descriptions and prototypes for the 3 functions. Put the prototypes at the beginning of your source file.
/*Calculates and returns the total value (in cents) of coins *for a given amount of pennies, nickles, dimes, quarters *(the function arguments) */ int calc_coins_value(int, int, int, int); /*Calculates the dollars for a given value in cents (the argument)*/ int calc_dollars(int); /*Calculates the change value (cents) for a given amount in cents*/ int calc_change(int); b. Write the 3 function definitions and place them after the main function. As an example, the function calc_dollars would be:
int calc_dollars(int total_cents) { return total_cents / 100; } c. Replace the expression part of each assignment statement in your main function with a call to one of these functions. For example,
dollars = calc_dollars(total_cents); would replace the assignment statement in the main function that calculates a value for dollars.
d. Compile, link, and run the final program.
Part 3. Answer these questions.
· Explain how function calc_coins_value "gets its input".
· Explain what happens to the value returned by calc_coins_values - the function output.
· What type of data does C expect calc_coins_value to return and what type of data does the function expect as its inputs? How do you think C knows this.
Part 4. Add new functionality of coins program
Make a new version of this program that performs an additional operation. After calculating the value of the coin collection and the amount of dollars and change, it will also indicate how a bank teller should dispense the change using the fewest number of coins. For example, if the coin collection was worth 10 dollars and 97 cents, the bank teller should pay out the 97 cents in the following way: Dispense the following coins:3 quarters2 dimes0 nickels2 pennies To do this, write a new function dispense as described below. /* Displays the smallest amount of coins to dispense as change. * For example, if the argument is 43, function should display * 1 quarters, 1 dimes, 1 nickels, 3 pennies. If the argument is * 23, function should display 0 quarters, 2 dimes, 0 nickels, 3 * pennies. * Hint use the remainder operator to find the number of * each coin needed and then subtract the value of the coins * dispensed from the change left to dispense. */ Write a "case study" description for this function and then write the function. Use it in your coins program to show how a bank teller should dispense the change efficiently. Notice that the quantity of coins dispensed by the teller may be different from the original coins in the collection. This function needs as input the amount of change. It will not return any values but will display its results using printf statements. Deliverables:Submit your new coins program with modifications described in Parts 1, 2, and 4 above; show a few sample runs of the program; provide the answers to the questions in part 3.