## CS 115 Lab 7 Control Structures (Individual)

### 60 Points

Due by Sunday October 22 midnight

Educational goals of this lab - verify that every student can

• understand control structures - if statements and functions

INSTRUCTIONS:

• (30 points) Individual problem: Comparing Times
Write a function called time_compare that will have 4 parameters. These four integers represent 2 times, that is hour and minute of the first time, then the hour and minute of the second time. It returns an integer, which will be 0, 1 or 2. 0 means the two times were the same. 1 means that the first time came before the second one. 2 means the first time came after the second one.
• Example calls:
time_compare(1,2,3,4) would return 1 because 1:02 comes before 3:04.
time_compare (12, 19, 12, 1) would return 2 because 12:19 comes after 12:01.
time_compare (3, 4, 3, 4) would return 0 because they are the same time.
• The function returns a value by using ONE return statement at the bottom of the function definition. Use a variable to hold the return value until execution is done in the function, then return that variable.
• The time_compare function does NOT do any input from the keyboard or output to the screen. Its interaction with the rest of the program is through its parameters and its return value.
• HINT: compare the two hours first. If they are different, you have your answer without having to look at the minutes at all.
• Write the logic as efficiently as you can; don't have redundant tests.
• (10 points) Write a function called print_time. It has 2 parameters, an hour and a minute value. It should print out the hour value, then a colon, then print out the minutes with two digits. If the minutes value is only one digit, put a zero out before it. You do NOT need any new string formatting or anything like that. You can do it with just a couple of lines of code using things we have already done. Ask yourself how would you tell when you need the zero in front of the minutes.
• (10 points) Both functions, time_compare and print_time, should have the documentation described in class for functions. That is the purpose, the pre-conditions (the parameters) and the post-conditions (what the function returns or accomplishes).
• (10 points) Write a main function which asks the user for 4 numbers, then calls the function. It uses the result from the function to output a message that expresses the result. In other words, don't just print out 1 or 2 or 0. Note that the two times are printed out in the format described above. Call your print_time function twice to do this.

Sample run

```Enter hours of first time: 11
Enter minutes of first time: 30
Enter hours of second time: 10
Enter minutes of second time: 45

The first time: 11:30

The second time: 10:45

time1 comes after time2
```

Another sample run

```Enter hours of first time: 3
Enter minutes of first time: 9
Enter hours of second time: 4
Enter minutes of second time: 12

The first time: 3:09

The second time: 4:12

time1 comes before time2
```
• Submit your finished program with the link at the top. Use the Lab 7 and Code menu choices.

Log off properly - you don't want your account misused by someone else!

Remember not to leave files on the local hard drives in this lab or anywhere else on campus! Make sure you save your projects on a portable storage device you take with you!