CSE 130 Spring 2010 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 function average. For the second part we change param- eters and type, 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 Spring 2010. Lab 7, March 17, 2010. // Sample program for studying functions #include // Insert 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 int. Insert code for aver- age above main. b. First write the header for function average. What is its return type? It is integer as specified. 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 int. This is an example of a local variable. avg = (i+j)/2; e. You need to declare avg (of type int) 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 int in main. b = average(m, n); h. Print b, using a printf statement. For example, printf("\nAverage of m and n is: %d\n", b); i. Save, compile and execute. Try m = 5 and n = 10. Average should be 7. 3. Change i, j to m, n. Change return type. 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. Change the return type to float. Also change the type of variable avg to float. Now divide by 2.0 to get an answer of type float. m. In main, change type of variable b to float as well. Save lab7.c, compile and execute. You should get a correct answer (7.5) for m = 5, and n = 10. 4. Removing variable avg n. 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. o. Remove declaration for avg. Delete the statement avg = (m+n)/2.0; Now at the end, we must return (m+n)/2.0; p. Save your lab7.c program, compile and execute. Try again m = 5 and n = 10. You will get 7.5 as an answer.