CSE 130 Fall 2009 Lab 8 DO NOT SUBMIT THIS. THIS IS FOR YOUR PRACTICE ONLY. ---------------------------------------------------------------------- 1. Main program for lab8.c a. Copy lab8.c from this page. Cut and paste it in your pico window. Save it as lab8.c. This program just reads and prints m. // lab8.c // CSE 130 Fall 2009. Lab 8 // Sample program for studying prototype for functions #include // Code for function fibo is after main ( ). // So we need a prototype for fibo. // int fibo (int m); // This is necessary initially. int main (void) { int m, i; printf("Type in m > 1\n"); scanf("%d", &m); printf("\nm is %d\n", m); // Insert code for function call fibo // i = fibo(m); // <-- make it a statement. // Print answer based on i (what fibo returns). return 0; } // Insert code for function fibo here. // This is below main ( ). b. Save and compile lab8.c. Execute with m = 4. See what does it print. 2. Function fibo c For hw-1 we wrote a small program to calculate Fibonacci numbers. This program does not use arrays. That is what we need here. Con- vert that piece of code to an integer function fibo with one parameter n. d. fibo returns 1 if n is a fibonacci number, else it returns 0. e. The function fibo checks if the parameter n is a Fibonacci number, by generating all numbers starting with 2. We have already initialized first and second to value 1. // Code for fibo based on HW-1 int fibo (int n) { int first = 0, second = 1, next; // local variables. // Have this inside a loop next = first + second; first = second; second = next; // End of loop // Check if next = n and return 0 or 1. } f. Open pico window for lab8.c. Cut and paste code for function fibo in your pico window after main ( ). See the comment in your main program. g. Function fibo uses 3 local variables. These have been declared at the top and initialized. h. Now we calculate next = first + second; followed by first = second; second = next; i. Have this calculations inside a while loop with condition (next < n). As long as next is less than n, we keep generating the next fibonacci number. j. When we get out of the loop, it is because next = n or next > n. If next = n, then our number n is a Fibonacci number. So we return 1, else we return 0. Add a if-else-return statement to fibo at the end. 3. Changes to main ( ). k. Make sure that i = fibo(m); is no longer a comment. l. After i = fibo(m), insert one statement to check value of i. If i is zero the print `m is not a fibonacci number', else print m is a fibonacci number. Print value of m. m. Make sure that the prototype statement about fibo(n) is no longer a comment. Save and compile lab8.c. Execute with m = 12, m = 13 etc. 4. Make prototype statement a comment n. Make the prototype statement a comment by adding // at the begin- ning. o. Save lab8.c and compile. You will get an error, as function fibo is no longer known to the compiler. 5. Insert the prototype statement back p. Insert the prototype statement back. Save and compile lab8.c. There will be no compilation errors.