Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Intro to C programming question: The Fibonacci number series is defined as follo

ID: 3775162 • Letter: I

Question

Intro to C programming question:

The Fibonacci number series is defined as follows:

f(0) = 0, f(1) = 1, and for i > 1, f(i) = f(i-1) + f(i-2).

The first 10 numbers from the series are 0,1,1,2,3,5,8,13,21,34. Thus, f(0)=0, f(1)=1, f(2)=1, f(3)=2, etc.

a) Write in the text box below a function called fib that returns void and that takes as parameters an array v of type int and an integer n that indicates the length of array v. This function computes each Fibonacci number f(i) and saves it to array element v[i], for 0 <= i < n.

If parameter n is negative, the function must print an error message and call exit(1).

Hint: when computing element v[i] use v[i-1] and v[i-2].

b) Write a main() function that declares an int array numbers of size N, where N is a constant equal to 100 and then calls the fib function defined above with parameters numbers and N. After that, print all elements from the numbers array to the terminal.

Ensure the code is correct, aligned, and indented properly. Use space characters to type indentation. Use meaningful variable names.

Explanation / Answer

#include <stdio.h>
void fib(int v[],int n)
{
   v[0]=0;
   v[1]=1;
   int i;
   for(i=2;i<n;i++)
   {
       v[i] = v[i-1]+v[i-2];
   }
}
int main()
{
   int N=100,i;
   int numbers[N];
   fib(numbers,N);
   for(i=0;i<N;i++)
   {
       printf("%d ",numbers[i]);
   }
   printf(" ");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote