Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hello, can someone help with my homework, the problem that I have is that I need to return how many times the count has added

hello, can someone help with my homework, the problem that I have is that I need to return how many times the count has added but my pass by reference is not working and it is returning 0.

I added comments on where to look. It should be easy to spot.

please let me know if you need more info thanks!

In C++

Here is the code

#include

#include

#include

#include

#include

using namespace std;

int fib1(int n, int & count) { // I am passing by reference but not returning the value that I need to

if (n <= 1) {

return n;

} else {

count++; //here is the count that I need to return

return fib1(n - 1, count) + fib1(n - 2, count);

}

}

void fib(int n) {

int cot=0;

int f[n + 2];

int i;

f[0] = 0;

f[1] = 1;

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

f[i] = f[i - 1] + f[i - 2];

cot++;

}

cout<<"The count was " << cot<

// return f[n];

cout<<" The "<< n<< "number was" <

};

int main() {

int count = 0;

int num1;

cout << "Find the fib" << endl;

cin >> num1;

auto start = chrono::high_resolution_clock::now();

ios_base::sync_with_stdio(false);

fib(num1);

auto end = chrono::high_resolution_clock::now();

// Calculating total time taken by the program.

double time_taken =

chrono::duration_cast(end - start).count();

time_taken *= 1e-9;

cout << "Time taken by program is : " << fixed << time_taken

<< setprecision(9);

cout << " sec" << endl;

start = chrono::high_resolution_clock::now();

ios_base::sync_with_stdio(false);

cout<

end = chrono::high_resolution_clock::now();

// Calculating total time taken by the program.

double time_taken_ =

chrono::duration_cast(end - start).count();

time_taken_ *= 1e-9;

cout << "Time taken by program is : " << fixed << time_taken

<< setprecision(9);

cout << " sec" << endl;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions

Question

1. Select the job or jobs to be analyzed.

Answered: 1 week ago