CS1101C Lab 1 (Odd Week)

Time Difference

The deadline for this lab question is Wednesday 07 September 2005, 23:59:59 hours.

The name of your C program file must be called timediff.c, files with any other name will not be marked.

You are expected to spend a maximum of 2 hours for this assignment.

The Task

It is often useful to know the time difference between a starting time and an ending time. Suppose the starting time is 11:46:39 (11:46 hours and 39 seconds) and the ending time is 22:31:17 (22:31 hours and 17 seconds). The time difference is then 10:44:38 (10 hours 44 minutes and 38 seconds). Use the following assumptions:
  1. All times are in 24-hour format.
  2. The starting time and ending time are always in the same day.
  3. The ending time is always after the starting time.

Write a program that prompts the user for the starting time and ending time, and calculates and displays the time difference.

Sample Run

The following shows a sample run, where $ indicates the Unix prompt. You are reminded to follow the sample outcome exactly, else marks will be deducted. User input is underlined.

Hint: The "%02i" format specifier may be useful for displaying an integer in the format shown in the sample run below.

$ gcc -Wall timediff.c -o timediff
$ ./timediff

Enter starting hour, minute, and second: 1 2 3
Enter ending hour, minute, and second: 13 12 11

The starting time is:    1:02:03
The ending time is:     13:12:11
The time difference is: 12:10:08

$ ./timediff

Enter starting hour, minute, and second: 11 46 39
Enter ending hour, minute, and second: 22 31 17

The starting time is:   11:46:39
The ending time is:     22:31:17
The time difference is: 10:44:38

$


This document, index.html, has been accessed 15 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 12 different hosts have accessed this document in the last 445 days; your host, 216.73.216.39, has accessed it 1 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.