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

2. (Fibonacci Series) (50 points) Modify a program generating a Fibonacci series

ID: 3888128 • Letter: 2

Question

2. (Fibonacci Series) (50 points) Modify a program generating a Fibonacci series (Program 3 from the Homeworkl) in the following way. The Fibonacci series shall be generated in the function int FibSsp int ptrl, int ptr2) where ptrl is a pointer to the Ist member of the series and ptr2 is a pointer to the 2nd member of the series. A series generated in the function shall be located in the dynamical array and return a pointer to this array to a calling program. The (0h) element of this array shall be reserved for the number of the series members generated, the series members shall be located starting from the (1 element of the resulting array The series members shall be generated until the ratio of adjacent values converges to the golden section within 0.001, which means that the process should be stopped when the following condition holds 0.001 -1 4-2 where x-x,,x,, xi-2-1M, is a Fibonacci series. Test this function by writing a main function which shall a) prompt the user to enter two first members of the series and accept them; b) pass pointers to the members entered to the "FibGen function; c) accept a returned dynamical array from the FibGen function; d) display the number of the series members generated (0 element of the array) and the series members. (Hint: First use a while loop to calculate how many elements a Fibonacci series should contain based on the values of first two elements and the criterion above. Then create a dynamical array containing exactly that number of elements, which was found in the while loop and use a for loop to finally generate a Fibonacci series and put its elements in the dynamical array.)

Explanation / Answer

#include<iostream>

using namespace std;

int *FibGen(int *ptr1, int *ptr2)

{

    int a, b, c;    /* Let c be the current (nth) member, b be the previous member ((n-1)th) and a be the second previous ((n-2)th) member */

    a = *ptr1;      // Initialize a with value of 1st number

    b = *ptr2;      //Initialize b with value of 2nd number

    int count = 0; // count is a counter to keep track of number of members

    double result1, result2;

    while(true)             // using a while loop to find out the number of members in the series

    {

        c = a + b;

        result1 = c/b;

        result2 = b/a;

        if(result1 - result2 <= 0.001)

            break;

        b = c;

        a = b;

        count +=1;

    }

    int *Fibonacci = new int[count+1]; /* Declaring a dynamic array of size count + 1 (We have added 1 here because 0th element will be used to hold number of members) */

    Fibonacci[0] = count;           // Assigning the value of count to 0th element of the array

    Fibonacci[1] = *ptr1;

    Fibonacci[2] = *ptr2;

    for(int i = 3; i <= count; i++)

    {

        Fibonacci[i] = Fibonacci[i-1] + Fibonacci[i-2];

    }

    return &Fibonacci[0];

}

int main()

{

    int number1, number2;

    cout<<"Please enter first number: ";

    cin>>number1;

    cout<<" Please enter second number: ";

    cin>>number2;

    int *Result = FibGen(&number1, &number2);

    int numberOfMembers = Result[0];

    cout<<"Number of members in the series is: "<<numberOfMembers<<endl;

    cout<<"Members of the series are: "<<endl;

    for(int i = 1; i <= numberOfMembers; i++)

    {

        cout<<Result[i]<<" ";

    }

    return 0;

}