Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using c++ a)Write a program that nds the rst Fibonacci numbers: 1,1,2,3,5,8,13,.

ID: 3574375 • Letter: U

Question

Using c++

a)Write a program that nds the rst Fibonacci numbers: 1,1,2,3,5,8,13,... The sequence is generated as follows:
1. For the rst two numbers: v[0] = v[1] = 1
2. Starting with the third number, numbers in the sequence are obtained by adding the previous two numbers: v[k] = v[k-2] + v[k-1]
b)Write a program that stores the rst n Fibonacci numbers into a vector. Your code should have the following
1. a function void fibonacci( vector<int>&, size t n) which constructs the vector.

2. a function void print( const vector<int>& ) to print the vector in the fashion {1, 1, 2, 3, 5, 8, 13} 3.

The main function should read the value n and call the two functions.

Explanation / Answer

a)Program;

#include<iostream.h>

#include<conio.h>

void main(){

int n,k,f=1,s=1,t;

clrscr();

cout<<"Enter Range:";

cin>>n;

int v[50];

v[0]=f;

v[1] = s;

cout<<v[0]<<" "<<v[1]<<" ";

for(k=2;k<n;k++){

t = f+s;

f = s;

s = t;

v[k] = t;

cout<<v[k]<<" ";

}

getch();

}

b)Program:

#include<iostream.h>

#include<conio.h>

#include<vector.h>

void fibonacci(vector<int> &v,size_t n);

void print(const vector<int>&v);

void main(){

clrscr();

int n;

vector<int> v;

cout<<"Enter n value;";

cin>>n;

fibonacci(v,n);

print(v);

getch();

}

void fibonacci(vector<int>& v, size_t n){

int f=1,s=1,t;

v.push_back(f);

v.push_back(s);

for(size_t k=2;k<n;k++){

t = f + s;

f = s;

s= t;

v.push_back(t);

}

void print(const vector<int> &v){

for(size_t i=0;i<v.size();i++){

cout<<v[i]<<" ";

}

cout<<endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote