COMP 166 - Lab 4 - Functions

Learning Outcomes and Introduction

In this lab assignment you will practice:

Task 1: Enhanced Change Machine

Create a change machine program based on the Task 2: Money Changer program from Lab 1, with a few enhancements.

Important: do not use global variables for any part of this task!

Starter code

If you do not have a working solution for Lab 1's Money Changer program, you can use this simplified version instead:

download change.c
  1. Create a new function that accepts user input for an amount of money in normal decimal notation (eg. "$10.25" for 10 dollars and 25 cents), and returns the amount as an integer number of cents. Use this new function from main to handle all user input.
  2. Create another function to calculate the number of bills and coins to return as change. Remove the related code from main and use a call to this new function instead. The function will need at least two formal parameters: one for the amount of money to exchange (in cents), and a second for an array that will be used to store the resulting bill and coin amounts.

  3. Create a third new function that prints the array of bill and coin counts in a human-friendly format. Modify main so it uses this function instead of handling its own output.
  4. Use an infinite loop in your main function to allow the user to exchange many different amounts of money without restarting the program. If you haven't already done so, use your new functions to handle all input, calculations, and output – main should only be responsible for the overall program flow at this point!
  5. Modify your change calculation function so that it draws from a limited "inventory" of bills and coins that is reduced each time the function is used. If there are not enough of a larger denomination left in the inventory to cover the amount to exchange, compensate by using more of a lower denomination. If it is impossible to cover the full amount using what's left in the inventory, provide as much correct change as possible and report the unfulfilled amount using the return value of the function.

    Examples

    Given that there are two loonies, five quarters, and 22 pennies in the inventory when the function is called:

    • If $1.10 needs to be exchanged, the function should provide one loonie and 10 pennies in change, and return 0. One loonie, five quarters, and 12 pennies will be left in the inventory.
    • If $3.10 needs to be exchanged, the function should provide two loonies, four quarters, and 10 pennies in change, and return 0. One quarter and 12 pennies will be left in the inventory.
    • If $1.24 needs to be exchanged, the function should provide one loonie and 22 pennies in change, and return 2. One loonie and five quarters will be left in the inventory.
    • If $4.00 needs to be exchanged, the function should provide all of its coins in change (two loonies, five quarters, and 22 pennies), and return 45. Nothing will be left in the inventory.
  6. Modify your main function so that your program ends once the change calculation function is unable to exchange the full amount.
  7. Use Doxygen comments to document the purpose, parameters, and return values of each function other than main. Describe the overall program using a Doxygen comment at the top of your main program file.

Submission

Completing all tasks in this lab should result in 1 or more .c files comprising a single program within a folder named like LastnameFirstname_lab4. Compress the folder into a ZIP archive and upload it to the D2L assignment.

The marks for this lab are heavily weighted towards good coding practice and style. Pay attention to your code formatting, use comments to clarify your code, and make sure you are using defines, arrays, loops, and functions effectively.

NOTE: This assignment is to be done individually. You can help one another with problems and questions, but in the end everyone must write and submit their own code.

CriteriaMarks
Programs compile and run without error 3
Good coding style 3
Task requirements met or exceeded 3
Learning Outcomes achieved 3
Total 12