In: Computer Science
C Code Please!
Declares a constant variable N and sets it to 42.
* Declares an array of integers, with N elements, called
fibonacci.
* Declares an array of double floating-point numbers, with N-1
elements, called ratio.
* Fills the fibonacci array with the numbers in the Fibonacci
sequence (Wikipedia is your friend if you don't know what this
sequence is about but you want to know).
Into the i-th element of the fibonacci array, you need
to put the i-th term of the Fibonacci sequence.
The Fibonacci sequence is defined like follows:
- The 0-th term fib[0] is 1
- The 1-th term fib[1] is 1
- For all other terms with i=2...., the i-th
term fib[i] is the sum of the (i-1)-th term fib[i-1] and the
(i-2)-th term fib[i-2].
* The program then prints the terms of the Fibonacci sequence, one
number per line.
* The program then fills the elements of the ratio array with the
*real-valued* quotient of consecutive terms of the Fibonacci
sequence.
This means, ratio[i] contains fib[i+1] divided by fib[i].
* The program then prints the elements of the ratio array, one
number per line.
CODE FOR THE FOLLOWING PROGRAM:-
#include <stdio.h>
int main()
{
const int N=42;
int fibonacci[N];
double ratio[N-1];
int first=1,second=1,nextTerm,i=0;
fibonacci[i]=first;
//Filling the fibonacci array with the numbers in the Fibonacci sequence
for(i=1;i<N;i++){
fibonacci[i]=second;
nextTerm=first+second;
first = second;
second = nextTerm;
}
//printing the terms of the Fibonacci sequence, one number per line.
printf("The Fibonacci sequence is: \n");
for(int i=0;i<N;i++){
printf(" %d\n",fibonacci[i]);
}
//Filling ratio array with the *real-valued* quotient of consecutive terms of the Fibonacci sequence.
for(int j=0;j<N-1;j++){
ratio[j]=((double)fibonacci[j+1]/(double)fibonacci[j]);
}
//printing the elements of the ratio array, one number per line.
printf("The ratios of Fibonacci sequence is: \n");
for(int i=0;i<N-1;i++){
printf("%lf\n",ratio[i]);
}
return 0;
}
SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-
SAMPLE RUN:-
HAPPY LEARNING