Programming Assignment 7 Implement a function named fibo that will get a 20 elem
ID: 3719885 • Letter: P
Question
Programming Assignment 7
Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function.
It will set the array to the Fibonacci sequence.
This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…).
Add another function named findNum that will get the newly created array by fibo from the main function.
It will ask the user to enter the element number to find what the Fibonacci value is.
Example: if the user enters 6 the output would be 13
Given Program:
Explanation / Answer
#include <iostream>
using namespace std;
void fibo (int a[], int n) {
a[0]=1;
a[1]=2;
int i=2;
while(i<n){
a[i] = a[i-1]+a[i-2];
i++;
}
}
int findNum (int a[], int n) {
return a[n-1];
}
int main()
{
int a[20] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int n;
fibo(a,20);
cout<<"Enter the element number to find what the Fibonacci value is: "<<endl;
cin >> n;
cout<<"Result: "<<findNum(a,n)<<endl;
return 0;
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.