CS 1320 (Principles of Algorithm Design I):
Homework #9
- Assigned:
- November 24, 1999.
- Due:
- December 6, 1999, at start of class.
- Credit:
- 30 points.
Programming problems
- Credit:
- 10 points each.
- Instructions:
- Write a C++ program to solve each of the problems below.
For each program, unless otherwise instructed, turn in:
- A printed listing of your program.
It should begin with comments giving your
name, the assignment number, and a description
of what the program does in terms of its inputs
and outputs. If you make choices or assumptions
in writing the program, this is the place to
mention them.
If your program consists of more than one function,
precede each function (either its prototype or its
definition) with comments explaining its purposes,
parameters, and return value.
- If the program requires input, the input(s) you
used to test it.
- Your program's output, when executed with the input(s)
you used to test it.
See the
guidelines for
programming assignments for a more detailed description.
Remember that at least one other human will read your program,
so try to make it clear and readable. See the guidelines
for tips on what makes for readable code, and the
sample programs
for examples.
Revised timesheets program
Ms. Gotta Haveit Now was very pleased with your work on the
timesheet program (Homework #7).
Tuesday, she attended the meeting of the
Java Users Group of San Antonio
and thought to herself
that using a class to deal with times would simplify the code and
improve its maintainability. She hires you to rewrite the code to use
a time class.
For a description of what the program should do, see the
problem description in Homework #7.
Ms. Now wants you to create a Time class and then use
Time objects in the program. To improve the code's
maintainability, she really likes private members, both data and
functions. Also, remember how she muttered "I hate reference
variables. A mark of a good programmer is to use them only when
necessary." You succeeded last time in avoiding unnecessary
reference variables; try to do it again.
Hints
- When creating the Time class, ask yourself what the
class needs to do and what a Time object needs to store.
- Some classes (including perhaps the Time class) do not
need constructors or destructors. Writing your own constructor is
useful only if you need to specify an object's state when creating it.
Writing your own destructor is necessary only if some action needs to
be taken when an object is destroyed; the example presented in the
checkingAcct
class was a gratuitous example.
- It is possible to write the program using reference parameters
only for passing stream objects. Do so.
- You can write a function to subtract two different times
(it might need to be a friend function of the Time
class). Or you might try
writing a member function decreasing a Time object's
value by the value of another Times object.
- You may start from your own solution to the previous homework
or from the
sample solution.
For this problem, you should need to write very little new code;
most of what you need to do can be done by rearranging
existing code.
Recursive functions
Write a recursive function for each of the computational tasks
described below. Also write a main program to test your function,
such that someone could use this program to perform a variety of
tests on the function. (See the recursive examples linked
from the
sample programs page
for examples of such main programs.)
Note:
To receive full credit, your function must be recursive.
Summing numbers read from a file
Write a recursive function to return the sum of
integers read from a file.
For example, if the file contains the following lines
20
10
40
30
the function should return 100.
You can assume that the calling program has opened the file.
Your function should not print anything; it should just return
the sum for the calling program to print.
Scaling an array
Write a recursive function to multiply every element of an array
of doubles by a scaling factor (another double).
For example, if the elements of the array are as follows:
2.2
1.1
3.3
-1.0
-2.0
-3.0
and the scaling factor is -2, the function should change the
elements of the array to the following:
-4.4
-2.2
-6.6
2.0
4.0
6.0
Your function should not print anything; printing should be
done by the calling program.