Answer to Question #21406 in C++ for george

Question #21406
Create a thread that will use a runner function and from the thread function (e.g. runner) call the Fibonacci function to generate the Fibonacci series and store them in the array. Use the parameter supplied in the function runner in order to determine how many Fibonacci numbers must be generated.
1
Expert's answer
2013-01-28T12:27:04-0500

# include <iostream>
#include <vector>
using namespace std;
//returns
std::vector<int> fib_n(int n)
{
std::vector<int> dp(n + 1);
dp[1] = 1;
if (n<2) return dp;
dp[2] = 1;
if (n<3) return dp;
for (int i = 3; i <= n; i++)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp;
}
void print (vector<int> a){
for(int i=1;i<a.size();i++){
cout<<a[i]<<" ";
}
}

int main(){
setlocale( LC_ALL,"Russian" );
vector<int> c;
int n;
cout<<"Enter n : ";
cin>>n;

c=fib_n(n);
print(c);

system("PAUSE");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS