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

C++ - Implement the Function is_Fibonacci_array that takes an array of integers

ID: 3871624 • Letter: C

Question

C++ - Implement the Function is_Fibonacci_array that takes an array of integers and its size as input. the goal of the function is to return true if every element in the array is a Fibonacci number, false otherwise. You do not need to change the main function. The function should work for ANY FIBONACCI NUMBER NOT JUST THE ONES IN THE MAIN FUNCTION.

HERE IS THE CODE

#include using namespace std; //Implement the function below bool is_fibonacci_array(int*,int); bool is_fibonacci_number(int); //Param 1: pointer to the beginning of an array of integers //Param 2: size of that array //Returns: true, is every element in the input array is a Fibonacci number //(the order of the numbers in the array need not be ordered //as per the Fibonacci sequence) //false, otherwise //A Fibonacci sequence is generated as follows. /The first two numbers in the sequence are and 1 //The following numbers in the sequence are calculated by adding //the previous two numbers in the sequence. Thus the third number in the //sequence would be 1 (i.e., 0+1), the fourth being 2 (i.e., 1+1), the //fifth being 3 (i.e., 1+2), and so on, giving the following sequence: //0, 1, 1, 2, 3, 5, 8, 13, 21, 34, etc int mainO1 int a [5] 1317811,28657,75025,3,8; int b[31-0,1,4; int flag-isfibonacci_array(a,5); //should print "Is a Fibonacci array." if (flag) cout

Explanation / Answer

// function that is taking an array and length of it
bool is_fibonacci_array(int* a,int n)
{
   // looping through each element
for(int i=0;i<n;i++)
{
   // if it is not fibonacci number, returning false right away
    if(!is_fibonacci_number(a[i]))
    {
      return false;
    }
}
// if it doesn't have any number which is not fibonacci, then it will come here and returns true
return true;
}