CSCI 201 Lab 5 -- Functions and Conditional Operators in C++

Getting ready

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

We're going to give you two projects to work on in this lab. The projects are stored in a zip archive so you will have to use a special program to extract them. Download a ZIP'ed copy of the Lab 5 projects and store it in the c:\files directory.

Use PowerZip to open this archive and then to extract the files to the c:\files directory. You should be able just to double click on the archive icon in the c:\files window to run the program PowerZip from the start menu. Using its menu set the download directory to c:\files and hit the extract button to create:

Check to make sure these directories exist before proceeding.

Plot in 2-D

Start up Visual C++ and open the workspace c:\files\lab05\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. 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.

Conditional statement execution

Start up Visual C++ and open the workspace c:\files\lab05\Color100\Color100.dsw. This workspace should contain three files

The driver program Color100.cpp plots four functions that you will write. You will use the mouse to move between these functions by clicking on the graphics window.

"Templates" for the four functions you will modify are found in Funky.cpp. These functions receive two constant integers as inputs and return an EzWindows color. The driver function will divide the screen into 100 squares, 10 on each side. These squares will be addressed by their x and y co-ordinates, integers from zero to nine. Your program will be called with each of the 100 possible co-ordinate pairs and will be expected to return a color for the corresponding squares. Examine Funky.cpp and then build and execute Color100 to learn a bit more about the process.

Your task and your instructor's checkoff

You are to write four functions that do the following:
0 Returns Red, if x > y; White, if x equals y; and Blue, if x < y
1 Returns values that cause the window to resemble the flag of Ireland (three vertical stripes: green, white, and red) or any other European country.
2 Returns values that cause the window to resemble the flag of the state of North Carolina, but without the seal.
3 Returns values that cause the window to resemble the flag of the United States, but without the stars.

Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page