*Program is in C language* Write a program to precompute the Fibonacci numbers a
ID: 3795244 • Letter: #
Question
*Program is in C language*
Write a program to precompute the Fibonacci numbers and store them in an array. Fibonacci numbers are defined as follows Fib(0) = 1, Fib(1) = 1 and Fib(i) = Fib(i - 1) + Fib(i - 2). Recursive implementation of Fibonacci is very slow and precomputing them and storing them in an array makes it easier to answer queries. Allocate an array of size 50 to store Fibonacci numbers and store the ith Fibonacci number at index i. Since Fibonacci numbers increase rapidly use double as element type for array. Have a loop in your program to read i and print i and i^th Fibonacci number. Use -1 to quit the loop. Use input redirection to test your program. Note that your program reads from the user using scanf and input redirection feeds the file contents to your program. You don't have to use any file operations in your program. Consider the following file a.txt 4 10 20 15 Sample output for this recitation using input redirection is as follows fox01> recitation4Explanation / Answer
#include <stdio.h>
int main(void) {
int i, j,k, n;
//precompute the fibonacci array
double fib[100000];
fib[0] = 1.0;
fib[1] = 1.0;
//fib[i] = fib[i-1] + fib[i-2]
for(j = 2 ; j<100000 ;j++){
fib[j] = fib[j-1] + fib[j-2];
}
scanf("%d",&n);
//While the entered value not equal to -1 scan the number and show the precomputed value from the fib array
while(n != -1){
printf("%d %f ",n,fib[n]);
scanf("%d",&n);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.