Have to write a code with a function that returns the number of seconds since the last time the clock struck 12.

I need write a code with a function that returns the number of seconds since the last time the clock struck 12. Im really new to C++ and I want to see how to do this and can really use the help! Please and Thank You!!


1. Write a function that takes in three integer parameters (representing hours, minutes, and seconds) and returns the number of seconds since the last time the clock "struck 12" (i.e. was at 12:00:00 -- AM or PM doesn't matter).

2. To test this function, write a main() routine (in the same file) that prompts the user to enter hours, minutes, and seconds for two different clock times; then uses the Seconds function to calculate the shortest amount of time in seconds between the two times (both of which are within one 12-hour cycle of the clock); the print out the number of seconds since "striking 12" for both clocks, as well as the number of seconds between the two clock times.


@CocaCola9898

First of all, we need to see that you're trying to learn to program, by showing us your code so far. This is not a homework site. Second, it's really not that hard to do the programming. Let's say you get a user to type in the hour, minutes and seconds. Figuring the total seconds would be:
Lets say hour is 8. There are 3600 seconds in an hour, so take 8 * 3600 equals 28800
Minutes might be 12. There are 60 seconds in a minute. 12 * 60 equals 720
Add the two, then add the seconds inputted. Let's say it was 17.
28800 + 720 + 17 equals 29537 seconds have passed since 12:00.
Last edited on
@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.
Last edited on
Topic archived. No new replies allowed.