Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I wrote a basic program in C that basically finds the difference in the two

ID: 3527354 • Letter: H

Question

Hi, I wrote a basic program in C that basically finds the difference in the two times in clock format. For example: Please enter the time #1 in clock format: 2:30 Please enter the time #2 in clock format: 3:30 The difference is -1:00 Please enter time #1 in clock format: 12:10 Please enter time #2 in clock format: 9:05 The difference is 3:05. However, when I type in statements that have equal hours (i.e. 7:00 and 7:30), the difference turns out to be 0:30, not -0:30. How do I make it so that it shows the negative sign WITHOUT If else statements. Here are parts of my program: void show_difference(int hours1, int hours2, int minutes1, int minutes2, int*hours_difference, int*minutes_difference) { int total1_minutes; int total2_minutes; total1_minutes = hours1 *60 + minutes1; total2_minutes = hours2 * 60 + minutes2; *hours_difference = (total1_minutes - total2_minutes)/ 60; *minutes_difference = abs(total1_minutes - total2_minutes)% 60; return; main() { printf("The difference is %d:d. ", hours_difference, minutes_difference);

Explanation / Answer

The reason you didn't get a negative is because if you look at your code for minutes difference you used the abs function, which will always give you a possitive number. To me it sounds odd that you'd want the negatives anyways since all it sounds like is trying to find the difference between two times (which I would think is just a possitive number). I would just use abs for both hour and minutes.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote