CSCI 201 Lab 8 -- Functions in C++

Getting ready

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

We're going to give you two projects to start the lab. Download a ZIP'ed copy of the Lab 7 projects and store it in the C:\files directory.

Use PowerZip to open this archive and then to extract the files to the C:\ directory. This should create a new directory c:\files\lab08 with two subdirectories, PLOT2d and PLOT3d. Check to make sure these directories exist before proceeding.

Plot in 2-D

Start up Visual C++ and open the workspace c:\files\Lab08\PLOT2D\PLOT2D.DSW. This workspace should contain three files You'll still need to make sure that the ezwin include file directory is in your Visual C++ search path.

The driver program Main2D.cpp uses the EzWindows library to sequentially plot five functions that you will write. Build the programs in this project and then run the executable code. Use the mouse to move between functions. Press the X in the upper right corner of the display window to terminate the program.

You should not modify Main2D.cpp in this lab. However, you are encouraged to look at it to learn a bit more about C++ and EzWindows.

The program you will modify is called Funky.cpp. Presently, this program contains five simple functions similar to:

float MyFuncN(const float x)
{
	return N*x ;
}

The values returned by these functions produced the boring output that you saw when you ran the program.

Your task

You are to replace these five functions with five functions that return the following values:

  1. 0.7 sine(6 x) + 0.2 sine(20 x)
  2. absolute value of x, that is,
    -x, if x is negative,
    and x, if x is positive
  3. x * sine(1/x), if x is not zero,
    0, if x is zero
  4. sqrt(1 - x2)
  5. First, set a float variable z to x.
    Go into a for-loop, and execute z = z*z 10 times.
    In a second for-loop, execute z = sqrt(z) 10 times.
    Return z as the result of your function.

You will need to use functions from the math library to write the functions described above. Page 250 of your text lists a few of the functions in the math library. Notice that the file cmath is included in funky.cpp, this is the "include" file for the math library.

Lab instructor check-off 1

Once you've got the five functions plotted, show your work to the lab instructor.

Plot in 3-D

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

The driver program Main3D.cpp will call the functions that you will write. It will pass two float values (also called arguments) to your function. Your function will return a float which will be used in the plot. Since we don't have 3-D holographic displays, we have to plot your function on a two dimensional screen. This is done by using the number returned by your function 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 programs in this project. Then look at Main3D.cpp for details of the plotting routines.

Your modifications

Once again you modify Funky.cpp which, this time, contains five function similar to:

float MyFuncN(const float x, const float y)
{
	return x+N*y ;
}

Your five functions for the 3D plot are:

  1. x2 + y2
  2. x, if x >= y,
    y, if x < y
  3. 1/(x2 + y2),
    Don't worry, your function will not be called when either x or y is equal to zero.
  4. larger of the absolute values of x and y or
    MAX(|x|, |y|)
  5. 0.5 floor(12 x) + 0.5 floor(12 y)

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