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: 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

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.