|
CS 2731, Spring 2004 |
Recitation 4 must be submitted
following directions at: submissions on or before
|
Outline: This recitation has you write two MIPS programs that will print the so-called Fibonacci numbers: the sequence
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...,
First Program (C) | First Program (Java) |
---|---|
/* fibonacci.c: simple Fibonacci numbers */ #include <stdio.h> int main() { int F0, F1, F2; int i, last; F0 = 0; F1 = 1; i = 0; last = 16; do { F2 = F0 + F1; printf("%i", F0); printf(" "); F0 = F1; F1 = F2; i = i + 1; } while (i < last); printf("\n"); } | // Fibonacci.java: simple Fibonacci numbers public class Fibonacci { public static void main (String[] args) { int F0, F1, F2; int i, last; F0 = 0; F1 = 1; i = 0; last = 16; do { F2 = F0 + F1; System.out.print(F0); System.out.print(" "); F0 = F1; F1 = F2; i = i + 1; } while (i < last); System.out.print("\n"); } } |
C Run and Output | Java Run and Output |
% cc -o fibonacci fibonacci.c % fibonacci 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 | % javac Fibonacci.java % java Fibonacci 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 |
This second program should have two separate loops, the first to use the array to help calculate the numbers. In this case the calculation is much simpler, and it is easier to see that it is correct. However, in MIPS there is the additional complexity of coping with the array. You must use the array in this way as illustrated below.
Then the second loop should print the numbers from the array.
You should realize that pieces of these programs have already been supplied and discussed in class: for example, printing an array.
The second program can be based on the following C or Java programs, which again are essentially the same and have the same output as before.
Second Program (C) | Second Program (Java) |
---|---|
/* fib_array.c: Fib nums in an array */ #include <stdio.h> int main() { int F[16]; int i, last; F[0] = 0; F[1] = 1; i = 2; last = 16; do { F[i] = F[i-1] + F[i-2]; i = i + 1; } while (i < last); i = 0; do { printf("%i", F[i]); printf(" "); i = i + 1; } while (i < last); printf("\n"); } | // FibArray.java: Fib nums in an array public class FibArray { public static void main(String[] args) { int[] F = new int[16]; int i, last; F[0] = 0; F[1] = 1; i = 2; last = 16; do { F[i] = F[i-1] + F[i-2]; i = i + 1; } while (i < last); i = 0; do { System.out.print(F[i]); System.out.print(" "); i = i + 1; } while (i < last); System.out.print("\n"); } } |
C Run and Output | Java Run and Output |
% cc -o fib_array fib_array.c % fib_array 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 | % javac FibArray.java % java FibArray 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 |
Contents of submission
for Recitation 4: Last Name, First Name; Course Number; Recitation Number (4).
|