Code:
// EX_3.c CHAPTER 8
#include <stdio.h>
struct time1
{
int hour;
int minute;
int second;
};
struct time2
{
int hour;
int minute;
int second;
};
struct time3
{
int hour;
int minute;
int second;
};
int main (void)
{
struct time3 time_calculate_function (struct time1 link_1, struct time2 link_2);
struct time1 one[3] = { { 3, 45, 15 } }; // link_1
struct time2 two[3] = { { 9, 44, 3 } }; // link_2
struct time3 three[3];
int i;
for(i = 0; i < 1; ++i)
{
printf("%i:%i:%i\n", one[i].hour, one[i].minute, one[i].second);
printf("%i:%i:%i\n", two[i].hour, two[i].minute, two[i].second);
printf("Difference in time: \n");
three[i] = time_calculate_function (one[i], two[i]);
printf("%i:%i:%i\n", three[i].hour, three[i].minute, three[i].second);
}
return 0;
}
struct time3 time_calculate_function (struct time1 link_1, struct time2 link_2)
{
// return difference 5:58:48 seconds
struct time3 foo;
return foo;
}
quick question, i'm trying to get the fourth printf function call to print "5:58:48" seconds and I will be doing the code inside the time_calculate_function which will be returning a structure. Also, i have a question about passing a structure array
I am looking for the answer (your reply) how to have the structure's time1 and time2 passed to the time_calculate_function so I can access each member inside the function for example one.hour, two.hour etc. also as stated in the book this function (time_calculate_function) has to return a structure and I understand the link_1 and link_2 are the references passed from the function but how to reference the structure time3.
The question in the programming book is:
CH 8 EX #3.) Write a function elapsed_time that takes as its arguments two time structures and returns a time structure that represents the elapsed time (in hours, minutes, and seconds) between the two times. So the call
elapsed_time (time1, time2)
where time1 represents 3:45:15 and time2 represents 9:44:03, should return a time structure that represents 5 hours, 58 minutes, and 48 seconds. Be careful with times that cross midnight
Thanks all!