CS107 - Lab 8
C++ Functions

Due Tuesday 11/11

Overview:

This assignment introduces the idea of abstraction in problem solving using the C++ idea of a function as a medium. The key challenge here is, "How do software designers organize large programs, and how do the parts of these programs communicate with each other when they are running?" The readings and exercises for this assignment are taken from section 7.7 of your text.


Compiling and Running a C++ program

Drag a copy of csci107 - Invitation (OSX) to the desktop, and then close the csci107 folder. The button C++ Compiler is used for this assignment. When you select it, Open the file fig210.cpp in the Desktop -> Invitation -> Examples directory, and then select Compile in the Compiler menu. The following result should appear on your screen.

Notice here that the program has two parts, a main program and a function definition named getArray.

Now select Execute in the Compiler menu, and then select the Run button at the bottom -- you will be prompted to enter a series of numbers.If you enter the series of numbers 8 7 6 9 4 0 you should receive the following output.

When the main program begins to run, the first statement executed is getArray(L, n), which is a function call statement. That is, the main program is calling upon the function getArray to perform a service for it - namely to get a series of values from the user, store them into the array L and return the number n of values that it stores.

Now look at the function definition getArray, which begins with the line void getArray(int x[], int &m) { and ends with the single right brace }. This definition has statements that are executed only when the function is called, and not before. The two parameters x[] and &m are placeholders for the arguments that will be provided by the function call (in this case, L and n). When the function's statements are executed, the results are assigned to the arguments L and n (rather than the parameters x and m).Thus, the main program receives values in L and n at the completion of this single line that calls the function getArray.

You should carefully review the discussion of C++ functions in section 7.7 before answering the questions in this lab. Additional examples of simple C++ programs with functions are called EXAMPLE6.CPP - EXAMPLE9.CPP in the Invitation -> EXAMPLES folder on your desktop. You may want to run some of these to get a better feeling for writing and running programs with functions.

The Cryptography problem Load crypt_decode.cpp and crypt_encode.cpp programs from the EXAMPLES directory and write the encode/decode function that is missing. Work with your team!

Algorithmic Problem Solving

Complete practice problems 1-3 on page 347 of your text, and then complete Exercises 27, 29 and 30 on page 369 of your text. All of these can be handwritten except for Exercise 30, which should be written and run correctly using the C++ Compiler described above. Submit your completed program to the csci107 -> Drop Box when you have completed it.



You may do these exercises (except the last one) either by hand or with a word processor (e.g. MS Word). Also, you may choose to do this assignment either by yourself or in a group. However, solutions should be written up individually and handed in on the due date.

To submit a file electronically, you should first rename it so that you are identified as the author (e.g., give it a name like lab-x-ltoma). Then drag the file to the csci107 -> Drop Box folder. Be careful not to drag an entire folder into the Drop Box; only a single file at a time can be submitted.

Once you are finished in the lab, be sure to drag the CS107 icon to the Trash - this step disconnects you from the server and prevents someone else (who may use this iMac later in the day) from accidentally accessing files in your personal folder.