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

Pointers, Arrays, and Functions 1. The nth Fibonacci number is defined as the su

ID: 3887724 • Letter: P

Question

Pointers, Arrays, and Functions

1. The nth Fibonacci number is defined as the sum of the previous two Fibonacci numbers. The first and second

Fibonacci numbers (which are needed to get the recurrence started) are 0 and 1, respectively.

Write a function that takes a single argument, an int* that corresponds to an array of size (at least) 3. The

precondition of the function is that the first two elements of the array are the (n-2) and (n-1) Fibonacci numbers.

The function should compute the nth Fibonacci number and place it in the third element of the array.

2. Fill in the following main function so that it fills the first n Fibonacci numbers into the fib array. Use the

function you defined above to compute each number.

int main(){

const int n = 25;

int fib[n];

int i;

fib[0] = 0;

fib[1] = 1;

/* fill this in */

}

Explanation / Answer

#include<stdio.h>

int i=2;
void fibo( int *a)
{
   int x=i;
   a[x]= a[x-2]+a[x-1];
}
int main()
{
const int n=25;
   int fib[n];

fib[0]=0;
fib[1]=1;

for(;i<n;i++)
   {
     fibo(fib);
   }

   for(i=0;i<n;i++)
    printf(" %d ", fib[i]);

   printf(" ");
   return 0;
}

/*
output:-
========

lenovo@lenovo-Vbox:~/chegg$ ./a.out
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
lenovo@lenovo-Vbox:~/chegg$

*/