Answer to Question #300281 in C++ for Norri

Question #300281

How to execute this using vector



#include <cmath>


#include <iomanip>


#include <iostream>



using namespace std;



int fibo(int n);



int main() {



string header = " Index Fibonacci number Fibonacci quotient Deviation";



double lim = (1.0 + sqrt(5.0)) / 2.0;



int fib_sequence[21];




for (short i = 0; i < 21; i++) {


fib_sequence[i] = fibo(i);


}



cout << header << endl;



for (short i = 0; i < 20; i++) {



double q = (double)fib_sequence[i + 1] / (double)fib_sequence[i];



cout << setw(5) << i << setw(15) << fib_sequence[i] << setw(20) << fixed


<< setprecision(10) << q << setw(20) << scientific << setprecision(3)


<< lim - q << endl;


}



return 0;


}


int fibo(int n) {


if (n <= 1)


return n;



return fibo(n - 1) + fibo(n - 2);


}

1
Expert's answer
2022-02-20T14:40:31-0500
#include <cmath>
#include <iomanip>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int fibo(int n);

int main()
{
	string header = " Index Fibonacci number Fibonacci quotient Deviation";
	double lim = (1.0 + sqrt(5.0)) / 2.0;


	vector<int>fib_sequence;
	for (short i = 0; i < 21; i++)
	{
		fib_sequence.push_back(fibo(i));
	}
	cout << header << endl;
	for (short i = 0; i < 20; i++) 
	{
		double q = (double)fib_sequence[i + 1] / (double)fib_sequence[i];
		cout << setw(5) << i << setw(15) << fib_sequence[i] << setw(20) << fixed
		<< setprecision(10) << q << setw(20) << scientific << setprecision(3)
		<< lim - q << endl;
	}
	return 0;
}

int fibo(int n)
{
	if (n <= 1)
		return n;
	return fibo(n - 1) + fibo(n - 2);
}

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
New on Blog