A function using C programming that converts time to seconds
(Time in Seconds) Write a function that takes the time as three integer arguments (for
hours, minutes, and seconds) and returns the number of seconds since the last time the clock “struck
12.” Use this function to calculate the amount of time in seconds between two times, both of which
are within one 12-hour cycle of the clock.
Answer:
#include <stdio.h> #include <math.h> // function prototype int seconds( int h, int m, int s ); int main() { int hours;// current time's hours int minutes;// current time's minutes int secs;// current time's seconds int first;// first time, in seconds int second;// second time, in seconds int difference; // difference between two times, in seconds printf( "%s", "Enter the first time as three integers: " ); scanf( "%d%d%d", &hours, &minutes, &secs ); // calculate first time in seconds first = seconds( hours, minutes, secs ); printf( "%s", "Enter the second time as three integers: " ); scanf( "%d%d%d", &hours, &minutes, &secs ); // calculate second time in seconds second = seconds( hours, minutes, secs ); // calculate difference difference = fabs( first - second ); // display difference printf( "The difference between the times is %d seconds\n", difference ); } // end main // seconds returns number of seconds since clock "struck 12" // given input time as hours h, minutes m, seconds s int seconds( int h, int m, int s ) { return 3600 * h + 60 * m + s; }
Leave a reply