CSE 130 Spring 2010 Lab 9 DO NOT SUBMIT THIS. THIS IS FOR YOUR PRACTICE ONLY. ---------------------------------------------------------------------- 1. Recursive function mystery a. Look at the lab9 document using your browser and try to under- stand how recursive function mystery works. TA will write the recursive function mystery on the board and explain what it does. b. Function mystery returns 1 if b = 0. Otherwise it calls itself recursively, and returns the product a*mystery(a , b-1). The next call would calculate mystery(a, b-2), and return a*a*mystery(a, b-2). c. When the second parameter becomes zero, mystery would return 1. But it has already accumulated the product a*a*a.... b times. This product is a^b or a raised to b. 2. Program for lab9.c d. Copy lab9.c from this page. Cut and paste it in your pico window. Save it as lab9.c. // lab9.c // CSE 130 Spring 2010. Lab 9, April 7, 2010. // Sample program for studying recursive functions #include int mystery (int a, int b) { if (b == 0) return 1; else return (a*mystery (a, b-1)); } int main (void) { int i, m, n; printf("Type in m > 0, and n >= 0\n"); scanf("%d%d", &m, &n); printf("\nm is %d and n is %d\n", m, n); i = mystery(m, n); // Call to function mystery printf ("Function mystery returns %d\n", i); return 0; } e. Save and compile lab9.c. Execute with m = 4 and n = 3. It should print 64. Function mystery computes m raised to power n. (m^n). 3. Remove recursion f. Remove recursion from the function mystery and rewrite it using a loop. Here a for loop will be useful. But keep the function. g. Declare and initialize product to 1 before the for-loop. h. The header for the for loop: for ( ; b > 0; b--) i. Inside the loop, accumulate product = product*a; j. At the end return product. k. Save your program lab9.c. Compile and execute. Try m = 4, and n = 3. You should get 64 as answer. 4. Another example l. Suppose now we want to calculate the product m*n using a recur- sive function int mystery(int a, int b). m. Here we can calculate product a*b = a+a+a+.... b times. We assume that both a, b can be zero (0). n. What is the product when b is 0, or a = 0? If b is not zero AND a is not zero, then what is the expression for product that uses recursion ? o. Remove the for-loop from function mystery. Delete variables that you do not need. Write recursive function mystery using a and b only, for calculating product a*b. This would be an if-else statement. p. Save your program as lab9.c. Compile and execute. Try m = 4 and n = 3. You get 12 as an answer from main program. Try m = 0 and n = 0 to see if you get 0 as an answer. 5. Modification-I q. Rewrite the if-else statement of function mystery such that it uses not-equal-to (!=) check instead of equal-to (==) check. r. Save your program, compile and execute. Again check with dif- ferent values of m and n. Let at least one of them be zero. 6. Modification-II (nested function calls) s. Remove i = mystery(m, n); statement from main ( ). Change the printf statement that now prints mystery(m, n) instead of i. t. Save your program, compile and run for m = 4 and n = 3. You should get the same answer. What we have is a case of nested function calls. Main calls printf, and printf calls mystery.