Classwork 17: Reference Parameters


To practice implementing a function with reference parameters.


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.

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.

/* File: makechange.c Name: This program uses function that determines the number quarters, dimes, nickels and pennies make up the cents given. */ #include <stdio.h> /* The function prototype is written for you. This prototype says that makechange() has five parameters. The first parameter is a normal int. The next four parameters are int reference parameters (i.e., pointers to int). */ void makechange(int , int *, int *, int *, int *) ; /* Don't change the main() function!!! */ int main () { int cents ; int quarters, dimes, nickles, pennies ; printf("This program will figure out the change for you.\n\n") ; printf("Enter number of cents: ") ; scanf("%d", &cents) ; makechange(cents, &quarters, &dimes, &nickles, &pennies) ; printf("Make change using %d quarters, %d dimes, %d nickles and %d pennies.\n", quarters, dimes, nickles, pennies) ; return 0 ; } /* end of main function */ /* Function makechange Stores # of quarters, dimes, nickels and pennies add up to n cents. */ /* Implement the function makechange() here: */

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.


Use the script command to record yourself compiling and running the program. Run your program several times! Submit your C source code and the typescript file as usual:

submit cs104_chang cw17 makechange.c
submit cs104_chang cw17 typescript

Be sure to logout completely when you have finished!