CS 115 Lab 9 Functions

90 Points

Due on Monday April 5 by end of lab

Educational goals of this lab - verify that every student can

(90 points) Team problem: Comparing Times

Problem Description:

Comparing clock times is a common task to do in a program about payroll or scheduling. The times can be of different precision, this problem assumes they are represented as hours, minutes, and seconds. It is a 24 hour clock, so hours can run from 0 to 23. So you don't have to worry about AM or PM. The times are assumed to be both in the same day, so you don't have to worry about "the next day". You do NOT have to validate these inputs; you can assume they are valid. How are they compared? The possible results of a comparison is that the two times are the same, that the first time is earlier (before) the second time, or the first time is later (after) the second time.
The algorithm is:
first the largest units, the hours, are compared. If the hours are different, the decision can be made at that time. If the hours of the first time is less than the hours of the second time, the first time comes before the second time. Similiarly for greater than and comes after. If the two hours are the same, then the minutes need to be compared to see if they can determine the order of the times, as above. If the minutes are the same, then the seconds need to be examined. A time is only equal to another time if all three components are equal to the corresponding components of the other time.

Sample run

Enter first hours or -1 to stop: 1
Enter first minutes: 2
Enter first seconds: 3
Enter second hours: 1
Enter second minutes: 2
Enter second seconds: 3

Time 1: 01:02:03
Time 2: 01:02:03

They are the same times

Enter first hours or -1 to stop: 10
Enter first minutes: 15
Enter first seconds: 20
Enter second hours: 10
Enter second minutes: 25
Enter second seconds: 30

Time 1: 10:15:20
Time 2: 10:25:30

Time 1 is before Time 2

Enter first hours or -1 to stop: -1

(18 points) Test cases

(15 points) Design:

There are three functions in this program, time_compare, time_print and main. For each function there is a prolog (three P's!). The prolog for the main is the same as it's always been. The prolog for the time_compare function describes its purpose, its parameters (pre-cond), and its result or return value (post-cond). The prolog for the time_print function does the same.

After the prolog for a function, give a design of the function, what control structure(s) needed, how the thing returned is decided.

(50 points) Implementation:

  • Submit your finished program with the Canvas link.