Classwork 11: Two Simple Functions
Objectives
To practice implementing functions.
Assignment
This assignment has two parts. In Part 1, you implement a function that counts change. In Part 2, you implement a function that makes change. In both parts, you are given a file to modify. The files have a main program that you are not allowed to change.
Part 1
In Part 1, your assignment is to implement a simple function, called countchange(), that computes the value of a certain amount of change, given the number of coins of each kind. Here's a sample run of the whole program.
PT[126]% ./a.out This program will compute the worth of your change. Enter number of quarters you have: 3 Enter number of dimes you have: 2 Enter number of nickels you have: 4 Enter number of pennies you have: 3 Your 3 quarters, 2 dimes, 4 nickels and 3 pennies are wort 118 cents. PT[127]%
First, download the main program in the file countchange.c. Make sure you save it with a filename that ends with .c. Then, add the implementation of your function at the bottom of the file.
Compile and run your program. Use the -Wall option with gcc. Your program should compile without any warnings or errors.
Part 2
In Part 2, your assignment is to implement a simple function, called makechange(), that figures out how to make change for a given number of cents. Here's a sample run of the program:
PT[127]% ./a.out This program will figure out the change for you. Enter number of cents: 68 Make change using 2 quarters, 1 dimes, 1 nickles and 3 pennies. PT[128]%
In this example, the function makechange() determined that 2 quarters, 1 dime, 1 nickle and 3 pennies equals 68 cents. (This is the fewest number of coins you can have to make up 68 cents.) The function "communicates" this result to the main program using 4 reference parameters.
The function that you implement must be compatible with the main program in this file: makechange.c.
First, download the main program in the file makechange.c. Make sure you save it with a filename that ends with .c.
Below the main program, write the C code to implement the makechange() function. Give meaningful names to your parameters. Give the reference parameters names that immediately tell you they are pointers to int.
To compute the number of quarters, dimes, nickels and pennies, you should use the integer division operator / and the modulus operator %.
Compile and run your program. Use the -Wall option with gcc. Your program should compile without any warnings or errors.
Submitting
Use the script command to record yourself compiling and running the two programs. Run your programs several times! Submit your C source code files and the typescript file as usual:submit cs104_chang cw11 countchange.c makechange.c typescript
Be sure to logout completely when you have finished!