CSCI 201 Lab 9 -- Reference Parameters in C++

Getting ready

If a directory called C:\files\lab9 presently exists on your computer, delete it.

Download a ZIP'ed copy of the Lab 9 project and store it in the C:\files directory. Use PowerZip to extract the archive to the C:\files directory.

This should create C:\files\lab9\ with two subdirectories called SwapEm and Plot3D.

Parameter passing

In this lab we'll experiment with different ways of passing parameters. First open and build C:\files\lab9\SwapEm\SwapEm.dsw. Take a look at the program SwapEm.cpp. The program should look familiar. Notice that the two formal arguments of the SwapEm are not reference variables.

Rather than running the program, step through it with the debugger. Press F10 to move one statement at a time through the main routine. When the yellow arrow reaches the call to SwapEm, press F9 to move into the SwapEm routine. Then continue to press F10 to move through the statements of SwapEm.

As you step through the program, pay attention to the variables window at the bottom off your Visual C++ window. At first, it will display variables of main
Variables of main
but, as your move into SwapEm, it displays variables of SwapEm.
Variables of SwapEm
The information displayed in a variables window is the contents of C++ function activation records.

Step to the statement FormArg2 = Temp
Tracing
Now move your mouse into the variables and move between the activation records of main and SwapEm. You do this selecting from a pull-down menu that is displayed when you click on the down arrow at the right end of the textbox labeled Context.

Instructor Checkoff I

Modify the code so that SwapEm has reference parameters. Again step through to the statement FormArg2 = Temp. Show your instructor the activiation records for main and SwapEm.

Plot in 3-D using reference parameters

OK. You really can't plot in three dimensions, put you can do something close. Open the workspace c:\files\PLOT3D\PLOT3D.DSW. This workspace will contain three files

The driver program Main3D.cpp will call the function (also called a procedure when the function does not return a value) you will write. It will pass three float arguments, all of which will be passed as reference parameters. Your procedure will modify the values of the three parameters. The modified values will then be used in the plot. Since we don't have 3-D holographic displays, we have to plot the values produced by your function on a two dimensional screen. This is done by using the value of the parameter z to choose between six different colors. The succinct explanation is that everything before the decimal point in your floating point number is discarded, and the fractional part is used to select a color from the following table:
RangeColor
.00 to .1666..Red
.1666.. to .3333..Yellow
.3333.. to .50Green
.50 to .6666..Cyan
.6666.. to .8333..Blue
.8333.. to .00Magenta

Go ahead and build and run the program.

Your modifications

You will modify Funky.cpp which contains five function similar to:
float MyFuncN( float & x, float & y, float & z)
{
        z=x+y;
	return ;
}

The modifications that you need to make are as follows:

Lab instructor checkoff 2

When your plotting is complete, have your lab instructor take a look.
Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page