The Microwave Problem – Solution


The challenge for this month’s Exercise is to write a microwave oven input routine: The user types in a given number of seconds and your program translates the value into the proper number of hours, minutes, and seconds. This type of problem may sound familiar.

Back in 2014, I presented an Exercise about making change: An amount of cents is presented and the solution is to calculate how many quarters, dimes, nickels, and pennies are required to equal that amount of change.

My approach for the making change Exercise was an if-else if-else contraption that peeled away quarters, dimes, nickels, and eventually pennies from the total, tallying the result. When I presented this solution online, someone called my solution dumb. Perhaps. Regardless, for this month’s similar problem I refrained from using any decision structures. Nope, my solution is all math:

2022_02-Exercise.c

#include <stdio.h>

int main()
{
    /* constants to set seconds in a minute and seconds in an hour */
    const int minutes = 60;
    const int hours = 60 * minutes;
    int h,m,s;

    /* accept input */
    printf("Enter time in seconds: ");
    scanf("%d",&s);

    /* trim down the chunks */
    h = s/hours;        /* hours rounded */
    s -= h*hours;       /* remaining seconds after removing hours (h) */
    m = s/minutes;      /* minutes rounded */
    s -= m*minutes;     /* remaining seconds after removing minutes (m) */

    printf("That's %d:%02d:%02d\n",h,m,s);

    return(0);
}

Lines 6 and 7 set constants for minutes and hours: 60 seconds are in a minute and 60 minutes are in an hour. Lines 11 and 12 prompt for and receive input.

The math starts at Line 15:

h = s/hours;

The number of hours h is calculated as the input value, s (seconds) divided by hours, or 3600. As int variables are involved, the result is rounded automatically. The result is the number of hours in the given number of seconds — even if it’s zero.

At Line 16, the number of hours h in seconds is subtracted from the original number of seconds s:

s -= h*hours;

The remainder is the number of minutes and seconds left to separate.

At Line 17, math is echoed from Line 15, but to calculate the number of minutes:

m = s/minutes;

The value in s is divided by constant minutes. The result, the number of minutes remaining, is stored in variable m.

Line 18 subtracts the minutes value (total seconds in the minutes calculated) from variable s, leaving the number of seconds remaining:

s -= m*minutes;

Line 20 outputs the results: hours, minutes, and seconds stored in variables h, m, and s.

No decisions are necessary in this code as the math removes a larger and larger chunk from the original value. Variable s always holds seconds. After hours (chunks of 3600 seconds) are pulled away, and minutes (chunks of 60 seconds) are removed, what’s left are seconds.

Here’s a sample run:

Enter time in seconds: 241
That's 0:04:01

I hope your solution met with success. Remember, many different solutions are possible so your’s doesn’t have to match mine. As long as the program runs and the output is correct, you’ve passed the Exercise.

Leave a Reply