CSE 130 Fall 2009 Lab 7 DO NOT SUBMIT THIS. THIS IS FOR YOUR PRACTICE ONLY. ---------------------------------------------------------------------- 1. Three parts of Lab7 - There are three parts to this exercise. In the first part, we write the average function. For the second part we change param- eters, and in the third we delete the local variable. - Copy lab7.c from this page. Cut and paste it in your pico window. Save it as lab7.c. This program just reads and prints m, n. // lab7.c // CSE 130 Lab 7, Oct 21, 2009. // Sample program for studying functions #include // Code for function average. int main (void) { int m, n; printf("Type in m, n (both positive)\n"); scanf("%d%d", &m, &n); printf("\nm is %d, and n is %d\n", m, n); // Insert code for function call average. // Print average of m and n. return 0; } - Compile lab7.c and execute. 2. Function average. a. Write an integer function average that calculates average of two integers and returns the value of type float. Insert code for average above main. b. First write the header for function average. What is its return type? It is float as average could be a decimal number with frac- tion. c. How many parameters does it need? It needs two parameters, both are of type int. Use (int i, int j) d. Store average in variable avg of type float. This is an example of a local variable. avg = (i+j)/2.0; e. You need to declare avg (of type float) in function average. Add a return statement that returns avg. f. From the main program call function average. Get the answer in variable b. g. Declare b of type float in main. b = average(m, n); h. Print b, using a printf statement. For example, printf("\nAverage of m and n is: %f\n", b); i. Save, compile and execute. Try m = 5 and n = 6. Average should be 5.5. 3. Changing i, j to m, n j. For function average, parameters can be m and n. But these m and n are different from those in main ( ). k. Replace i and j by m and n respectively. Change variables in the body as well. l. Save lab7.c, compile and execute. You should get the same answer that you got for m = 5, and n = 6. 4. Removing variable avg m. We actually do not need the variable avg, as we can return an expression of type float at the end of function average. There is no need to store it in variable avg first. n. Remove declaration for avg. Delete the statement avg = (m+n)/2.0; Now at the end, we must return (m+n)/2.0; o. Save your lab7.c program, compile and execute. Try m = 5 and n = 6.