The template code defines a Time struct. Given main() that reads two Times and computes the difference between the two, complete the following two functions: main() provides Read Time() to read input from a user and use the input to create a new Time by calling CreateTime(). PrintTime() is provided to output Time in 24 hour format (hh:mm:ss). Ex: If the input is: the output is: 1. Time* CreateTime(int hours, int minutes, int seconds) • Allocate and return a new Time according to the parameters 2. Time* TimeDifference (Time* end, Time* start) 22 23 24 25 o Allocate a new time to store the computed time difference o Compute the amount of time elapsed from start to end. Assume start is always before end. • Adjust for negative minutes and seconds. o Return the pointer to Time that contains the difference 02 55 10 04 15 50 8} Time; 9 1 #include 2 #include 3 Start: End: 4 typedef struct Time_struct { 5 int hours; 6 int minutes; 7 int seconds; 30 31 26 } 27 32 33 10 // ALLocate and return a Time as per parameters 11 Time CreateTime(int hours, int minutes, int seconds) { 12 34 35 36 37 38 39 02:55:10 Difference: 01:20:40 Time* newTime = NULL; 13 /* Type your code here. */ 14 15} 16 17 // Read integers hours, minutes, seconds; 18 // create and return a Time using the input values 19 Time ReadTime() { 20 21 04:15:50 49 50 28 Time TimeDifference (Time* end, Time* start) { 29 Time* diff = NULL; int hours; int minutes; int seconds; scanf("%d %d %d", &hours, &minutes, &seconds); return CreateTime (hours, minutes, seconds); /* TODO: allocate a new Time here */ // Begin with uncorrected arithmetic diff->hours end->hours start->hours; diff->minutes = end->minutes start->minutes; diff->seconds end->seconds start->seconds; /* Type your code here. */ 40 } 41 42 void PrintTime(Time t) { 43 printf("%02d:%02d:%02d", t->hours, t->minutes, t->seconds); 44 } 45 46 int main(void) { 47 Time* start = NULL; 48 Time end = NULL; Time* difference = NULL; 51 52 53 54 55 56 57 58 PrintTime (end); start = ReadTime(); end ReadTime(); difference = TimeDifference (end, start); printf("Start:\t"); PrintTime(start); printf("\nFnd-\+"). 59 printf("\nDifference: "); 60 PrintTime(difference); 61 62 63 64 65 66 67 68 69} printf("\n"); // Always free dynamically allocated memory when no longer needed free (start); free (end); free (difference); return 0;
The template code defines a Time struct. Given main() that reads two Times and computes the difference between the two, complete the following two functions: main() provides Read Time() to read input from a user and use the input to create a new Time by calling CreateTime(). PrintTime() is provided to output Time in 24 hour format (hh:mm:ss). Ex: If the input is: the output is: 1. Time* CreateTime(int hours, int minutes, int seconds) • Allocate and return a new Time according to the parameters 2. Time* TimeDifference (Time* end, Time* start) 22 23 24 25 o Allocate a new time to store the computed time difference o Compute the amount of time elapsed from start to end. Assume start is always before end. • Adjust for negative minutes and seconds. o Return the pointer to Time that contains the difference 02 55 10 04 15 50 8} Time; 9 1 #include 2 #include 3 Start: End: 4 typedef struct Time_struct { 5 int hours; 6 int minutes; 7 int seconds; 30 31 26 } 27 32 33 10 // ALLocate and return a Time as per parameters 11 Time CreateTime(int hours, int minutes, int seconds) { 12 34 35 36 37 38 39 02:55:10 Difference: 01:20:40 Time* newTime = NULL; 13 /* Type your code here. */ 14 15} 16 17 // Read integers hours, minutes, seconds; 18 // create and return a Time using the input values 19 Time ReadTime() { 20 21 04:15:50 49 50 28 Time TimeDifference (Time* end, Time* start) { 29 Time* diff = NULL; int hours; int minutes; int seconds; scanf("%d %d %d", &hours, &minutes, &seconds); return CreateTime (hours, minutes, seconds); /* TODO: allocate a new Time here */ // Begin with uncorrected arithmetic diff->hours end->hours start->hours; diff->minutes = end->minutes start->minutes; diff->seconds end->seconds start->seconds; /* Type your code here. */ 40 } 41 42 void PrintTime(Time t) { 43 printf("%02d:%02d:%02d", t->hours, t->minutes, t->seconds); 44 } 45 46 int main(void) { 47 Time* start = NULL; 48 Time end = NULL; Time* difference = NULL; 51 52 53 54 55 56 57 58 PrintTime (end); start = ReadTime(); end ReadTime(); difference = TimeDifference (end, start); printf("Start:\t"); PrintTime(start); printf("\nFnd-\+"). 59 printf("\nDifference: "); 60 PrintTime(difference); 61 62 63 64 65 66 67 68 69} printf("\n"); // Always free dynamically allocated memory when no longer needed free (start); free (end); free (difference); return 0;
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question

Transcribed Image Text:The template code defines a Time struct. Given main() that reads two Times and computes the difference between the two, complete the
following two functions:
main() provides ReadTime() to read input from a user and use the input to create a new Time by calling Create Time(). PrintTime() is
provided to output Time in 24 hour format (hh:mm:ss).
Ex: If the input is:
the output is:
22
23
24
1 #include <stdio.h>
2 #include <stdlib.h>
3
30
31
1. Time* CreateTime(int hours, int minutes, int seconds)
o Allocate and return a new Time according to the parameters
2. Time* TimeDifference (Time* end, Time* start)
o Allocate a new time to store the computed time difference
02 55 10
04 15 50
4 typedef struct Time_struct {
5
int hours;
int minutes;
int seconds;
Time;
25
26 }
27
32
33
34
• Compute the amount of time elapsed from start to end. Assume start is always before end.
o Adjust for negative minutes and seconds.
o Return the pointer to Time that contains the difference
6
7
8
9
10 // ALLocate and return a Time as per parameters
11 Time* CreateTime (int hours, int minutes, int seconds) {
12
Time newTime = NULL;
13
/* Type your code here. */
14
35
36
37
38
39
Start:
End:
15}
16
17 // Read integers hours, minutes, seconds;
18 // create and return a Time using the input values
19 Time* ReadTime() {
20
int hours;
21
int minutes;
int seconds;
scanf("%d %d %d", &hours, &minutes, &seconds);
return CreateTime (hours, minutes, seconds);
Difference: 01:20:40
28 Time TimeDifference (Time* end, Time* start) {
29
Time* diff = NULL;
/* TODO: allocate a new Time here */
// Begin with uncorrected arithmetic
diff->hours end->hours
52
53
54
02:55:10
44}
45
04:15:50
61
62
63
40
}
41
42 void PrintTime (Time* t) {
43
64
65
66
46 int main(void) {
47
48
49
50
51
67
68
69}
diff->minutes = end->minutes - start->minutes;
diff->seconds = end->seconds - start->seconds;
/* Type your code here. </
printf("%02d:%02d:%02d", t->hours, t->minutes, t->seconds);
Time start = NULL;
55 printf("Start:\t");
56
PrintTime (start);
Time* end = NULL;
Time* difference = NULL;
57
58 PrintTime (end);
start = ReadTime();
end = ReadTime();
difference = TimeDifference (end, start);
59 printf("\nDifference: ");
60
PrintTime(difference);
start->hours;
printf("\nEnd*\+").
printf("\n");
// Always free dynamically allocated memory when no longer needed
free(start);
free (end);
free (difference);
return 0;
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 4 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education