Splitting Seconds Into Hours, Minutes and Seconds

Medium

Write one function that hands back three answers at once. main is already written and does not change: it reads a single number with scanf, rejects anything that is not a number and anything negative, declares three of its own local variables, calls your function, and then prints those three variables by their own names. That last detail is the whole exercise. split_time returns void, so nothing comes back through return, and yet main must end up holding the right hours, minutes and seconds. The only way that can happen is the pattern this lesson taught: main passes &hours, &minutes and &seconds, so your function receives three addresses, and writing through those addresses with the dereference operator lands the values in main's variables rather than in copies. Write *hours = ... and not hours = ..., because the second one assigns to your own parameter, which is a separate variable in your own frame, and lesson 1 already proved the caller never hears about that. The arithmetic is chapter 2's two operators. There are 3600 seconds in an hour, so total_seconds / 3600 is the whole hours, integer division throwing the remainder away. What is left after those whole hours are removed is total_seconds % 3600, and dividing that by 60 gives the whole minutes. The leftover seconds are total_seconds % 60. Do not edit main, and leave split_time above main where it is, because a definition placed after its call is a function the compiler has not heard of yet. main always passes the addresses of three real variables, so split_time is never handed NULL here and needs no guard; a function that could be called with NULL would check, as the lesson's split_seconds does. Every path out of main returns 0, the two error paths included, because the checker compares what you print and reports a nonzero exit status as a failure however right the output looks.

Success Criteria

Your code must pass 6 test case(s) to complete this exercise. 3 hint(s) are available if you need help.

Sign in to track your progress

You can work on exercises as a guest, but sign in to track your progress and save your submissions.

This platform is built by its community

Every lesson, project, and tool on HelloC++ is funded by sponsors. Join them and help shape what we build next.

Become a Patron