Question

use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void...

use c++ and complete this lab

#include <iostream>

using namespace std;

class FibObj

{

public:

void setSize()

{

cout << "Enter an integer larger than 2: ";

cin >> size;

}

int getSize()

{

return size;

}

void fibTerms(int x) // Write a recursive function

{

}

explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion

{

setSize();

int *arr;

arr = new int[getSize()];

arr[0] = 1;

arr[1] = 1;

// Call recursive function here

// The next two lines will fill the array with the Fibonacci numbers; however, this is not recursive

for (int i = 2; i < size; i++)

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

for(int j = 0; j < getSize(); j++)

cout << arr[j] << " ";

};

private:

int size;

};

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Please find the modified code below.

#include <iostream>

using namespace std;

class FibObj

{

public:

void setSize()

{

cout << "Enter an integer larger than 2: ";

cin >> size;

}

int getSize()

{

return size;

}

void fibTerms(int x)
{
    static long int first = 0, second = 1, sum;
    if(x > 1)
    {
        sum = first + second;
        first = second;
        second = sum;
        cout << sum;
        fibTerms(x-1);    // recursive call
    }
    else
    {
        // after the elements, for line break
        cout << endl;
    }
}

explicit FibObj() // Default constructor was made explicit to avoid possible implicit type conversion

{

setSize();

int *arr;

arr = new int[getSize()];

arr[0] = 1;

arr[1] = 1;

// Call recursive function here

fibTerms(size);

// The next two lines will fill the array with the Fibonacci numbers; however, this is not recursive

for (int i = 2; i < size; i++)

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

for(int j = 0; j < getSize(); j++)

cout << arr[j] << " ";

};

private:

int size;

};

 
Add a comment
Know the answer?
Add Answer to:
use c++ and complete this lab #include <iostream> using namespace std; class FibObj { public: void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT