Calculate Fibonacci with dynamic array and recursion

Here is a program to calculate the fibonacci number. I use a int function to calculate the fib num and I just want to use the int function in another function passArr, which will help me to store the num into a dynamic array. But it doesn't work well. Anyone has a better way to figure it out? Thanks!!~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Header.h

#include <iostream>
using namespace std; 

class calFib
{
public:
    void print();
    
    void passArr(int, int, int);
    
    calFib(int size=100);
    
    ~calFib();
    
    int cal(int, int, int);
    
    void getFSP(int, int, int);
    
private:
    int maxSize; 
    int *p;
    int first;
    int second;
    int position;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "calFib.h"
void calFib::print()
{
    for (int i=0; i< position; i++)
    {cout << p[i] << " ";}
}
//Give Fib num to the dynamic array
void calFib::passArr(int first, int second, int position)
{
    for (int i=1; i< position+1; i++)
    {
        int FibNum = 0;
        FibNum = cal(first, second, i);
        p[i] = FibNum;
    }
}
//constructor
calFib::calFib(int size)
{
    if (size<=0) {
        cout << "The array size must be positive. Creating an array of the size 100. " << endl;
        maxSize = 100;
    }
    else
        maxSize = size;
    first = 0;
    second = 0;
    position = 0;
    p = new int[maxSize];
}
//destroyer
calFib::~calFib()
{delete [] p;}

void calFib::getFSP(int a, int b, int pos)
{
    first = a;
    second = b;
    position = pos;
}
//to calculate the Fibonacci num at position x
int cal(int a, int b, int x)
{
    if (x==1)
        return a;
    else if (x==2)
        return b;
    else
        return cal(a, b, x-1) + cal(a, b, x-2);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include "calFib.h"

using namespace std;

int main()
{
    calFib myFibonacci;
    int a, b, pos;
    
    cout << "Please input the first number a = ";
    cin >> a;
    cout << "Please input the second number b = ";
    cin >> b;
    cout << "Please input the Fabonacci number position you want c = ";
    cin >> pos;
    
    myFibonacci.getFSP(a, b, pos);
    
    myFibonacci.print();
    
    myFibonacci.~calFib();
    
    return 0;
}
You shouldn't explicitly call the destructor. That will be called automatically when it goes out of scope.

When you call getFSP, it only stores the numbers. It's not actually doing any calculation. Wouldn't it make sense to do that?

(You spelled Fibonacci wrong on line 15.)


1
2
for ( int i = 0; i < maxSize; ++i )
    p[i] = cal ( first, second, i );
Thank you, Yay295. I finally figure out the function cal, I forget the calFib:: for it, so it always doesn't work well. Your advice is very helpful! Thanks!
Topic archived. No new replies allowed.