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!!~
#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 = newint[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;
elseif (x==2)
return b;
elsereturn cal(a, b, x-1) + cal(a, b, x-2);
}
#include <iostream>
#include "calFib.h"
usingnamespace 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;
}
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!