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]% 

The function that you write must be compatible with the main program in this file: countchange.c.

/* File: countchange.c Name: This program uses a function that counts the worth (in cents) of a given number of quarters, dimes, nickels and pennies. */ #include <stdio.h> /* The function prototype is written for you. This prototype says that countchange() has four int parameters and returns an int. */ int countchange(int , int , int , int ) ; /* Don't change the main() function! */ int main () { int cents ; int quarters, dimes, nickels, pennies ; printf("This program will compute the worth of your change.\n\n") ; printf("Enter number of quarters you have: ") ; scanf("%d", &quarters) ; printf("Enter number of dimes you have: ") ; scanf("%d", &dimes) ; printf("Enter number of nickels you have: ") ; scanf("%d", &nickels) ; printf("Enter number of pennies you have: ") ; scanf("%d", &pennies) ; cents = countchange(quarters, dimes, nickels, pennies) ; printf("Your %d quarters, %d dimes, %d nickels and %d pennies", quarters, dimes, nickels, pennies) ; printf(" are wort %d cents.\n", cents) ; return 0 ; } /* end of main() function */ /* Function countchange Computes the worth of the given number of quarters, dimes, nickels and pennies. */ /* Implement your function here */

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.

/* File: makechange.c Name: This program uses a 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, nickels, 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, &nickels, &pennies) ; printf("Make change using %d quarters, %d dimes, %d nickels and %d pennies.\n", quarters, dimes, nickels, 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.

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!