Intro to C programming question The Fibonacci number series

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.

Solution

#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;
}

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 n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site