Computer Science 105 Assignment 8 - Abstraction: Designing C++
Functions
Due: 5:00pm November 10, 2003
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.
Part 1 - Compiling and Running a C++ Program
Drag a copy of the Invitation
folder from the csci105 server
to the desktop, and then close the csci105
folder. Double-clock the icon
Invitation (OSX) that is iside the folder Invitation on your
desktop and activate the familiar orange menu. 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 functiions.
Part 3 - 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 csci105 -> Drop Box
when you have completed it.
You may do these exercises (except the last one) either by hand or with
a word processor. Also, you may choose to do this assignment either by
yourself or in teams of two. If you work with someone else, your
team may hand in one copy of the completed assignment with both names
at the top.
Part 4 - Submitting Your Work
To submit a file electronically, remember to rename it so that you are
identified as the author (e.g., give it a name like asst8atucker.cpp).
Then drag the file to the csci105 -> Drop Box folder.
Once you are finished in the lab, be sure to drag the CS105icon
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.
To submit a handwritten assignment, leave it in the CS105
mailbox near my office (Searles 220).