Hello,
I've written the following code to convert time in seconds to time in hours, minutes and seconds. The time in seconds is stored in the variable total, there's no user input yet. Is my code simple or is there a simpler or faster way to write the code?
Code:
#include <stdio.h>
#define NROFITEMS 3
int values[NROFITEMS] = {3600, 60, 1};
int count[NROFITEMS];
char *names[NROFITEMS] = {"hours", "minutes", "seconds"};
int main (int argc, const char * argv[])
{
int i, total = 6789;
for (i = 0; i < NROFITEMS; ++i) {
count[i] = total / values[i];
total -= count[i] * values[i];
}
for (i = 0; i < NROFITEMS; ++i) {
printf("\n%s: %i", names[i], count[i]);
}
return 0;
}