In: Computer Science
use c++
1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers.
b) For the second part of this assignment, you must modify the Fib(n) you wrote for part a) so that it uses the array you fill in part a, to compute Fib (n+1) using Fib(n) and Fib(n-1) already saved in the array. This way we do not compute the same Fib numbers many times.
(Don't provide the same answer which is already posted. It should be correct and post the image of your output)
Code is Given Below:
========================
#include <iostream>
using namespace std;
//defining function
long int fib(int n)
{
//checking if n is less than equal to 1
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main()
{
//creating array of size 100
long int fibSeries[100];
//calling fib 100 times and storing result in array
for(int i=0;i<100;i++){
fibSeries[i]=fib(i+3);
}
//printing result
for(int i=0;i<100;i++){
cout<<fibSeries[i]<<endl;
}
return 0;
}
Code Part b)
================
#include <iostream>
using namespace std;
//defining function
long int fib(long int *f,int n)
{
//checking if n==3
if(n==3){
return 2;
}
//checking if n==4
if(n==4){
return 3;
}
//checking if n==5
if(n==5){
return 5;
}
return f[n-4]+f[n-5];
}
int main()
{
//creating array of size 100
long int fibSeries[100];
//calling fib 100 times and storing result in array
for(int i=0;i<100;i++){
long int temp = fib(fibSeries,(i+3));
fibSeries[i]=temp;
}
//printing result
for(int i=0;i<100;i++){
cout<<fibSeries[i]<<endl;
}
return 0;
}
Output:
===========
Code Snapshot:
==================