CS 1400 Lab #21: Passing By Value and Passing By Reference


In this exercise, you will create a method that passes one of its parameters by value and one by reference.

The problem

In this exercise, you will write a program that will determine how many coins of each denomination (1¢, 5¢, 10¢, 25¢, and 50¢) are required to make up a given amount of change. For example, to make 57¢ we could use 1 half dollar, 1 nickel, and 2 pennies.

Deriving the Solution

Given an amount of change to make, it should be obvious that many combinations of coins are possible. In the example above, we could make up 57¢ with: 57 pennies; 5 dimes, a nickel, and 2 pennies; 2 quarters and 7 pennies; and so on. For this exercise we will use what is known as a greedy algorithm. Greedy algorithms build up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Our algorithm will figure out how many of the largest denomination coin we need first, and work down through each succeeding coin denomination until we are done. This algorithm is described in the following diagram.

making change

To see how the algorithm works, let's take our 57¢. First consider how many 50¢ pieces we can get out of 57¢. We could get 1. After taking out the 50¢, we have 7¢ left. Now consider quarters. We can't get any quarters out of 7¢. Then look at dimes. We can't get any dimes out of 7¢ either. Then look at nickels. We can take one nickel out of 7¢. This leaves 2¢, so finally we have two pennies.

The ComputeChange( ) Method

If you look carefully at the diagram above, you will notice that the code does the same thing over and over again, but with different values for n and for the coin denomination. Whenever you see this pattern in a program, you should consider writing a method. In this case, you want a method that takes as its inputs n, the amount of change left, and a coin denomination. The method should

  1. Compute the number of coins of the given denomination it can take out of n, and
  2. Compute the new value of n, after taking out those coins.

We now have a problem. We need to return both the number of coins we computed, and the new value of n. However, a method can only return one thing. The solution here is to pass the value of n by reference. When the new value of n is computed, it is computed in place, so we do not need to return the value. We only need to return the computed number of coins. The signature for the method would then look like

    static int ComputeChange(ref int changeValue, int coinValue);

Writing the Code

Download the .cs file that is located here. Where shown in the code, write the ComputeChange method, following the steps in the activity diagram above. Be sure to write a method prologue for your method. Also be sure that you have no magic numbers. Before compiling your program do a code walkthru as decribed in the reading material for this lab. Test and debug your program. When your code is complete and working to your satisfaction, submit it on Canvas.


When you submit this lab, add some comments to your submission to answer the following questions:

(1) What is the value of a code walkthrough when debugging a program like this?

(2) What is the value of setting a watch in the debugger when debugging a program like this?

File(s) to Submit:

Place the complete project folder in a zip file and name the zip file lab_21_your-initials_V1.0.zip. For example, I would name my file lab_21_RKD_V1.0.zip. Submit this assignment as Lab #21 on Canvas.

Grading Guidelines

Description Points possible

Assignment meets grading guidelines:
o Source code files contain a declaration that you did not copy any code, except that provided.
o Assignment has been properly submitted to Canvas
o Code meets style guidelines
o Code contains a Console.ReadLine( ) statement at the end


Program executes correctly and meets all requirements


You have correctly answered the two questions in this lab.

Total 7