C Program to Calculate Difference Between Two Time Periods .
-
I am not able to understand the logic used in the program below. Can someone elaborate how does the logic works in this program.
#include
struct TIME
{
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);int main()
{
struct TIME startTime, stopTime, diff;printf("Enter start time: \\n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds); printf("Enter stop time: \\n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Calculate the difference between the start and stop time period. differenceBetweenTimePeriod(startTime, stopTime, &diff); printf("\\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds); printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds); printf("= %d:%d:%d\\n", diff.hours, diff.minutes, diff.seconds); return 0;
}
//not able to understand the logic.
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
//how does this logic works??.
if(stop.seconds > start.seconds){
--start.minutes;
start.seconds += 60;
}diff->seconds = start.seconds - stop.seconds; if(stop.minutes > start.minutes){ --start.hours; start.minutes += 60; } diff->minutes = start.minutes - stop.minutes; diff->hours = start.hours - stop.hours;
}
Thank you
-
I am not able to understand the logic used in the program below. Can someone elaborate how does the logic works in this program.
#include
struct TIME
{
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);int main()
{
struct TIME startTime, stopTime, diff;printf("Enter start time: \\n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds); printf("Enter stop time: \\n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Calculate the difference between the start and stop time period. differenceBetweenTimePeriod(startTime, stopTime, &diff); printf("\\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds); printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds); printf("= %d:%d:%d\\n", diff.hours, diff.minutes, diff.seconds); return 0;
}
//not able to understand the logic.
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
//how does this logic works??.
if(stop.seconds > start.seconds){
--start.minutes;
start.seconds += 60;
}diff->seconds = start.seconds - stop.seconds; if(stop.minutes > start.minutes){ --start.hours; start.minutes += 60; } diff->minutes = start.minutes - stop.minutes; diff->hours = start.hours - stop.hours;
}
Thank you
The logic does not work because it is subtracting the stop time from the start time. It should subtract the start time from the stop time to get the difference. For example a start time of 11:33:47 and a stop time of 15:21:10 gives a difference of -4:12:37. The correct answer is 3:47:23. You can work through the logic by using some random times and working through each line,changing values as necessary and doing the subtractions.
-
I am not able to understand the logic used in the program below. Can someone elaborate how does the logic works in this program.
#include
struct TIME
{
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);int main()
{
struct TIME startTime, stopTime, diff;printf("Enter start time: \\n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds); printf("Enter stop time: \\n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Calculate the difference between the start and stop time period. differenceBetweenTimePeriod(startTime, stopTime, &diff); printf("\\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds); printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds); printf("= %d:%d:%d\\n", diff.hours, diff.minutes, diff.seconds); return 0;
}
//not able to understand the logic.
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
//how does this logic works??.
if(stop.seconds > start.seconds){
--start.minutes;
start.seconds += 60;
}diff->seconds = start.seconds - stop.seconds; if(stop.minutes > start.minutes){ --start.hours; start.minutes += 60; } diff->minutes = start.minutes - stop.minutes; diff->hours = start.hours - stop.hours;
}
Thank you
It solves the following equation for X. {stop time} + {X} = {start time} If you walk through examples by hand you will see that the code appears to solve that equation correctly. However like the other response I too find it counter intuitive. As noted in the other response the expected difference would be to subtract start from stop. This solution ends up with negative values. It also treats a time period on a fixed clock - one that does not cross midnight. Which is nonsensical in human terms.
-
It solves the following equation for X. {stop time} + {X} = {start time} If you walk through examples by hand you will see that the code appears to solve that equation correctly. However like the other response I too find it counter intuitive. As noted in the other response the expected difference would be to subtract start from stop. This solution ends up with negative values. It also treats a time period on a fixed clock - one that does not cross midnight. Which is nonsensical in human terms.
-
It solves the following equation for X. {stop time} + {X} = {start time} If you walk through examples by hand you will see that the code appears to solve that equation correctly. However like the other response I too find it counter intuitive. As noted in the other response the expected difference would be to subtract start from stop. This solution ends up with negative values. It also treats a time period on a fixed clock - one that does not cross midnight. Which is nonsensical in human terms.
-
jschell wrote:
{stop time} + {X} = {start time}
Really? Only if X is negative; or the interval stops before it starts.