Create a function that takes a time in seconds as an input, and returns the numb
ID: 1811580 • Letter: C
Question
Create a function that takes a time in seconds as an input, and returns the number of hours and minutes that the time represents. The number of minutes shall be able to handle a decimal point. For example, if the time given is 10213 seconds, the function will give back 2 hours and 50.22 minutes. The function shall not print the results to console, but instead pass them back to the calling function. In the main function call the function using a few different times, and print the result to console.
Explanation / Answer
#include <stdio.h>
#include <time.h>
int main() {
time_t sec=12345;
struct tm *data;
data=gmtime(&t);
int s=data->tm_sec;
int m=data->tm_min;
int h=data->tm_hour;
int d=data->tm_yday;
printf("D:H:M:S = %d:%d:%d:%d ",d,h,m,s);
return(0);
}
Alternatively you could use division and remainders, starting by knocking off the days, then the hours, and finally the minutes from the total number of seconds:
- i.e.
int secs=12345678;
int days=secs/86400; /* no fractions as days is an int */
secs=secs400; /* seconds is now less than a day */
int hrs=secs/3600;
secs=secs600; /* seconds is now less than an hour */
int mins=secs/60;
secs=secs`; /* seconds is now less than a minute */
printf("D:H:M:S=%d:%d:%d:%d ",days,hrs,…
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.