#include <iostream>
#include <vector>
void getUserNum(int &);
int rFib(int n);
void dispSeq(std::vector<int> &vRef);
int main()
{
int userNum;
getUserNum(userNum);
rFib(userNum);
return 0;
}
void getUserNum(int &refVal)
{
std::cout << "Enter a positive integer:\n";
std::cin >> refVal;
}
int rFib(int n)
{
static std::vector<int> fib;
static bool baseReached = false;
fib.push_back(n);
if (baseReached)
{
dispSeq(fib);
}
if (n <= 1)
{
baseReached = true;
return n;
}
else
{
return rFib(n-2) + rFib(n-1);
}
}
void dispSeq(std::vector<int> &v)
{
for (size_t i = 0; i < v.size(); i++)
{
std::cout << v[i] << " ";
}
std::cout << std::endl;
}
|
|||||
closed as off-topic by 200_success♦, Jamal♦, Yuushi, Vedran Šego, Brian Reichle Oct 5 '13 at 11:41This question appears to be off-topic. The users who voted to close gave this specific reason:
|
|||||
|
Its normal to put parameter names even in the declarations:
We use this to help us identify what the function is doing. In C++ the
Be careful with your use of out parameters.
It seems more natural to return a value from a function then get sombody inside to set an external value via a reference (not unheard of and it will depend a lot on the context). But in this situation I would return a value. It will then make using the code eaiser.
Error Checking (especially input from a human) needs to be more rigorous.
If a user enters a non positive value things will go bad. If the user inputs a string the value of
In Fib() you should probably not use recursion. Prefer a while loop. Loop from 0 upto the point you want to reach. This makes using a vector easy. You can also start from where you left off last time rather than 0.
|
|||||
|
@Loki Astari has covered the important points, so I won't repeat those. I'll just address recommended integer and size types:
|
|||||||||
|