CSE 130 Spring 2010 Lab 10 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 `amnt' given to you. It has been renamed as amount 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 lab10.c from this page. Cut and paste it in your pico win- dow. // lab10.c // CSE 130 Spring 2009. Lab 10, April 14, 2010. // Sample program for studying structures and functions // Global declaration for type amount #include struct amnt // struct amnt will be redefined as amount { int dollars; int cents; }; typedef struct amnt amount; // This defines amount. // amount is another name for type struct amnt. // Now type amount is available to all functions. // Code for function add // Code for function rownd (round is in Math library) // So we use a different name. // Code for allDollars int main (void) { // Declare structure variables a, b of type amount. // Additional Variable declarations . printf("Type in Amount-1 in dollars and cents.\n"); scanf ("%d%d", &a.dollars, &a.cents ); printf("Type in Amount-2 as dollars and cents.\n"); scanf ("%d%d", &b.dollars, &b.cents ); // Call functions as required and print results. return 0; } - After pasting it in pico window, save it as lab10.c. Do not com- pile right away. Insert declaration for a, b. Both are of type amount. Now compile and execute. The program will ask for amounts a and b. Just type in some values. Program will end without doing anything, as there are no additional statements. 2. Function add a. Write code for function add that takes two arguments x, y of type amount. 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 amount. Declare that inside the body of `add'. Now calculate c.dollars and c.cents. Handle the case if c.cents exceed 99. 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 amount. You may use c again. Declare c of type amount. f. Insert a print statement for printing c.dollars and c.cents. See scanf statement for some hints. g. Save, compile and execute. Does it print addition of two amounts? Test with a = 20 87 and b = 1 45 Here answer will be 22 dollars and 32 cents. 3. The function rownd (see the name) h This function rounds a given amount and returns the rounded value as an integer dollars. It has one parameter x of type amount. This code should be in its place indicated for round by the com- ment 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.cents < 50 then i = x.dollars else i = x.dollars + 1; k. Return i at the end. Close the body. l. In the main program, insert two calls to function rownd. One for amount a and one for amount b. To save typing extra statements, have these calls in the printf statement itself. For example, printf("\nRounded amount a is: %d", \n rownd(a)); Insert one for b as well. m. Save, compile and execute. Does it print rounded values for amounts a and b? Test with a = 20 87 and b = 1 45 Here rounded amount a is 21 and that for b is 1. 4. Function allDollars n This function converts the given amount to a decimal value in dollars and returns it. Type of returned value is float. It has one parameter x of type amount. This code should be in its place indicated for allDollars by the comment above main(). o. First write the header for allDollars. 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 `allDollars' Calculate f as follows. f = (float)x.dollars + (float)x.cents/100.0; q. Return f at the end. Close the body. See if you can do this without declaring variable f. Just use the expression in your return statement. r. In the main program, insert two calls to function `allDollars'. One for amount a and one for amount b. To save typing extra statements, have these calls in the printf statement itself. For example, printf("\nAmount in dollars for a is: %f", \n allDollars(a)); Insert one for b as well. m. Save, compile and execute. Does it print decimal values for amounts a and b? Test with a = 20 87 and b = 01 45 Here decimal amount for a is 20.87 and that for b is 1.45.