CSE 130 Fall 2009 Lab 9 DO NOT SUBMIT THIS. THIS IS FOR YOUR PRACTICE ONLY. ---------------------------------------------------------------------- 1. Structures - This exercise is about structures and how to use them in func- tions. Study the global definition structure `tm' given to you. It has been renamed as time using typdef. See that statement as well. This structure has been declared above main, so it is glo- bal to all functions (available everywhere). - Copy lab9.c from this page. Cut and paste it in your pico window. // lab9.c // CSE 130 Fall 2009. Lab 9, Nov 12, 2009. // Sample program for studying structures and functions // Global declaration for type time. #include struct tm // struct tm will be redefined as time { int hours; int minutes; }; // Seconds are not included. typedef struct tm time; // This defines time. // time is another name for type struct tm. // Now type time is available to all functions. // Code for function add // Code for function rownd // Code for allHours int main (void) { // Declare structure variables a, b for // time duration-1 and duration-2. // Additional Variable declarations . printf("Type in duration-1 as hours and minutes.\n"); scanf ("%d%d", &a.hours, &a.minutes ); printf("Type in duration-2 as hours and minutes.\n"); scanf ("%d%d", &b.hours, &b.minutes ); // Call functions as required and print results. return 0; } - After pasting it in pico window, save it as lab9.c. Do not com- pile. Insert declaration for a, b. Both are of type time. Now compile and execute. The program will ask for durations a and b. Just type in some values. Program will end without doing any- thing, as there are no additional statements. 2. Function add a. Write code for function add that takes two arguments x, y of type time. It adds them and returns the result as a structure. This code should be in its place indicated by the comment above main( ). b. First write the header for add. What is its type? What is the type of its arguments? c You will need a local variable c of type time. Declare that inside the body of `add'. Now calculate c.hours and c.minutes. Handle the case if c.minutes exceed 59. d. Return c at the end. Enclose the body with { }. e. In the main program, insert a call to function add. Parameters are a, b. Get the result back in another variable of type time. You may use c again. Declare c of type time. f. Insert a print statement for printing c.hours and c.minutes. See scanf statement for some hints. g. Save, compile and execute. Does it print addition of two dura- tions? Test with a = 20 87 and b = 01 45 Here answer will be 22 hours and 32 minutes. 3. The function rownd. h This function rounds a given time duration and returns the rounded value as an integer hours. It has one parameter x of type time. This code should be in its place indicated for round by the comment above main( ). i. First write the header for round. What is its type? What is the type of its argument? j You will need a local variable i of type int. Declare that inside the body of `round'. Calculate i as follows. if x.minutes < 30 then i = x.hours else i = x.hours + 1; k. Return i at the end. Close the body. l. In the main program, insert two calls to function rownd. One for duration a and one for duration b. To save typing extra state- ments, have these calls in the printf statement itself. For exam- ple, printf("\nRounded time a is: %d", \n rownd(a)); Insert one for b as well. m. Save, compile and execute. Does it print rounded values for dura- tions a and b? Test with a = 20 47 and b = 01 15 Here rounded time duration a is 21 and that for b is 1. 4. Function allHours n This function converts the given duration to a decimal value in hours and returns it. Type of returned value is float. It has one parameter x of type time. This code should be in its place indicated for allHours by the comment above main(). o. First write the header for allHours. What is its type? What is the type of its argument? p You will need a local variable f of type float. Declare that inside the body of `allHours' Calculate f as follows. f = (float)x.hours + (float)x.minutes/60.0; q. Return f at the end. Close the body. See if you can do this without declaring f. Just use the expression in your return statement. r. In the main program, insert two calls to function `allHours'. One for duration a and one for duration b. To save typing extra statements, have these calls in the printf statement itself. For example, printf("\nDuration in hours for a is: %f", \n allHours(a)); Insert one for b as well. m. Save, compile and execute. Does it print decimal values for dura- tions a and b? Test with a = 20 47 and b = 01 15 Here decimal duration for a is 20.7833 and that for b is 1.25.