Hi!
I am just learning recursive functions so I was wondering if you can give me any advice - here's my code:
#include <iostream>
#include <vector>
using namespace std;
int GetFibNumber(int);
//Recursive function
void ArrayFib(int);
void PresentArray();
vector<int> FibArray;
int main()
{
int FibIndex = 0;
cout << "Enter an index for the Fibonacci number you want: ";
cin >> FibIndex;
cout << "Here's the number at that FibIndex: " << GetFibNumber(FibIndex) << endl;
cout << "Enter the index up to where you want the Fibonacci numbers: ";
cin >> FibIndex;
cout << "Here're the first " << FibIndex+1 << " Fibonacci numbers: " << endl;
ArrayFib(FibIndex);
PresentArray();
cin.sync();
cin.ignore();
return 0;
}
int GetFibNumber(int index)
{
if(index < 2)
return index;
else
return GetFibNumber(index-2) + GetFibNumber(index-1);
}
void ArrayFib(int index)
{
if(index == 0)
{
FibArray.push_back(0);
return;
}
if(index == 1)
{
FibArray.push_back(0);
FibArray.push_back(1);
return;
}
if(index >= 2)
{
//Recursivity
ArrayFib(index-1);
FibArray.push_back(FibArray[index-2]+FibArray[index-1]);
return;
}
}
void PresentArray()
{
for(int i = 0; i<FibArray.size()-1; i++)
{
cout << FibArray << ", ";
}
if(FibArray.size() >= 1)
cout << FibArray[FibArray.size()-1] << " The End!" << endl;
return;
}
Are there any glaring mistakes? Anything you would recommend about this? (Btw I know I named my function and array kinda bad)