@CocaCola9898
I was intrigued by this problem, so I went ahead and wrote it. I won't give you the code, but I will point you in the direction.
I'm sure you know how to convert the hours, minutes and seconds, to a full second count. I used an array of two, called
total_seconds[2]
. The first clocks seconds went into
total_seconds[0]
and the second clock, into
total_seconds[1]
To find the difference in seconds, you can do
int seconds = abs(total_seconds[0]-total_seconds[1]
. Using the abs command, you're making sure you end up with a positive number. But, as this way is, if the times were 10:00:00 for clock one and 2:00:00 for clock two, you end up with an answer of difference of 8 hours total. But the way I understand your problem, you
want the nearest time, which should be only a 4 hour difference. The way to get this correctly, is first check if your answer is over 6 hours, or 21600 seconds. The way I did this is.
1 2 3 4 5
|
int seconds = abs(total_seconds[0] - total_seconds[1]);// subtract the two, to get difference
// At 10:00:00 (#1)and 2:00:00 (#2), we get 28800 seconds equals 8 hours
if( seconds > 21600) // If difference is over 6 hours, otherwise, all is okay
seconds = 43200 - seconds; // subtract seconds from 12 hours
// 43200 - 28800 = 14400 which equals 4 hours
|
Hope this helps you to figure out your program.