0

How would I print the number of operations and time taken to compute Fibonacci numbers recursively versus that needed to compute them iteratively. Here is my code currently:

#include<bits/stdc++.h>
using namespace std;

int iterative(int n){
    int num1 = 0, num2 = 1, num3, j;
    cout<<num1<<" "<<num2<< " ";
    if( n == 0)
      return num1;
    for (j = 2; j <= n; j++){
      num3 = num1 + num2;
      cout<<num3<<" ";
      num1 = num2;
      num2 = num3;
      }
      return num2;
}
int recursive(int num){
   if (num <= 1)
       return num;
   return recursive(num-1) + recursive(num-2);
}
int main (){
    // main method to test the function
    int n;
    cout << "Enter a value for n: ";
    cin >> n;
    printf("\nWhen n is %d ,the iterative method number is : %d", n,iterative(n));
    getchar();
    printf("\nWhen n is %d ,the recursive method number is : %d", n,recursive(n));
    getchar();
    return 0;
}
  • For the time, you can use an `std::chrono::high_resolution_clock`. For operations, you first need to specify what you mean by "an operation". – François Andrieux Dec 16 '20 at 19:56
  • 1
    [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – François Andrieux Dec 16 '20 at 19:57
  • Assuming by 'operations' you mean additions, then I would just declare two global variables, and increment one of them each time each method does an addition. – john Dec 16 '20 at 19:57
  • 3
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – François Andrieux Dec 16 '20 at 19:57

1 Answers1

0

Try something like this. As mentioned above you should not using include #include<bits/stdc++.h> . If you want to know more about problems with that include you should read this.

#include <chrono>
#include <iostream>

int iterative(int n, int &cnt){
    int num1 = 0, num2 = 1, num3, j;
    std::cout<<num1<<" "<<num2<< " ";
    if( n == 0)
      return num1;
    for (j = 2; j <= n; j++){
      num3 = num1 + num2;
      std::cout<<num3<<" ";
      num1 = num2;
      num2 = num3;
      cnt++;
      }
      return num2;
}

int recursive(int num, int &cnt){
   if (num <= 1)
       return num;
   
   cnt++;
   return recursive(num-1, cnt) + recursive(num-2, cnt);
}

int main (){
    // main method to test the function
    int n;
    int cnt = 0;
    std::cout << "Enter a value for n: ";
    std::cin >> n;
    
    auto start = std::chrono::high_resolution_clock::now();
    int iterativeNum = iterative(n, cnt);
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<float> duration = end - start;
    
    printf("\nWhen n is %d , num of operation is : %d , time spent is : %f , the iterative method number is : %d", n, cnt, duration.count() ,iterativeNum);
    getchar();
    
    cnt = 0;
    start = std::chrono::high_resolution_clock::now();
    int recNum = recursive(n, cnt);
    end = std::chrono::high_resolution_clock::now();
    duration = end - start;
    
    cnt = 0;
    printf("\nWhen n is %d , num of operation is : %d , time spent is : %f , the recursive method number is : %d" , n, cnt, duration.count(), recNum);
    getchar();
    return 0;
}
Samuel B.
  • 156
  • 5