0
  #include<iostream>

    int fastFibonacci(int n) 
    {
      int numbers[n+2]; // int numbers[n].
      numbers[0] = 0;
      numbers[1] = 1;
      for (int i = 2; i <= n; i++)
      {
          numbers[i] = numbers[i - 1] + numbers[i - 2];
      }
      return numbers[n];
    }

    int main() {
      int n;
      std::cout << "Enter a Number";
      std::cin >> n;
      int result = fastFibonacci(n);
      std::cout << result << "\n";
      return 0;
    }

in this code when i enter input 0 or 1 get correct answer. But the problem is that when i replace int numbers[n+2]; with the commented part it start giving me wrong answer when input is 0 or 1. why? anyone please explain me.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Desg Aman
  • 19
  • 4
  • 9
    It is a non-standard C++ feature of declaring a variable length array. This is just a bad code and nothing more.:) – Vlad from Moscow May 08 '20 at 16:56
  • ok i understand but can u explain me that statement int numbers[n+2]; – Desg Aman May 08 '20 at 16:59
  • 1
    *in this code when i enter input 0 or 1 get correct answer.* -- You're lucky, since this code doesn't compile for Visual Studio all due to the non-standard C++ syntax. – PaulMcKenzie May 08 '20 at 16:59
  • 2
    @Desg Aman This is a declaration of an array with n + 2 elements. – Vlad from Moscow May 08 '20 at 17:00
  • oh okay thanks for the help i'm a beginner so that's why i'm making mistakes hoping for good, thank you so much for your help – Desg Aman May 08 '20 at 17:03
  • Side note: With `T array [n]`, `array[i]` is only valid if 0 <= `i` < `n`. `array[n]` would access out of bounds. In this case that +2 gives the extra space required and one more for good measure.. – user4581301 May 08 '20 at 17:21

2 Answers2

2

In this function

int fastFibonacci(int n) 
{
  int numbers[n+2]; // int numbers[n].
  numbers[0] = 0;
  numbers[1] = 1;
  for (int i = 2; i <= n; i++)
  {
      numbers[i] = numbers[i - 1] + numbers[i - 2];
  }
  return numbers[n];
}

there is used a variable length array with n + 2 elements declared in this line

  int numbers[n+2]; // int numbers[n].

Variable length arrays is not a standard C++ feature. It can be implemented as own language extension of a C++ compiler.

Using the variable length array makes the function very unsafe because there can occur a stack overflow.

As within the function there is explicitly used two elements of the array

  numbers[0] = 0;
  numbers[1] = 1;

then the array shall have at least two elements even when the parameter has a value less than 2.

To calculate the n-th Fibonacci number there is no need to declare an array of such a size.

Apart from this the function argument shall have an unsigned integer type. Otherwise the function can invoke undefined behavior if the user passes a negative number.

Also for big values of n there can be an integer overflow for the type int.

The function can be implemented in various ways.

Here is one of possible its implementations.

#include <iostream>
#include <functional>

unsigned long long fibonacci( unsigned int n )
{
    unsigned long long a[] = { 0, 1 };

    while ( n-- )
    {
        a[1] += std::exchange( a[0], a[1] );
    }

    return a[0];
}

int main() 
{
    const unsigned int N = 10;

    for ( unsigned int i = 0; i < N; i++ )
    {
        std::cout << i << ": " << fibonacci( i ) << '\n'; 
    }

    return 0;
}

The program output is

0: 0
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
8: 21
9: 34
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thankyou. Can you please tell me that did i do this correct this time `int fastFibonacci2(int n) { std::vector numbers; numbers.push_back(0); numbers.push_back(1); for (int i = 2; i <= n; i++) { numbers[i] = numbers[i - 1] + numbers[i - 2]; } return numbers[n]; }` – Desg Aman May 08 '20 at 19:25
  • 1
    @DesgAman Why are you asking me? You selected already the best answer. The idea with std::vector is bad. This approach is inefficient. I showed in my answer how the function can be simpler implemented. And presented by you code has undefined behavior because you may not use the subscript operator with non-existent elements of the vector. – Vlad from Moscow May 08 '20 at 19:29
  • I am asking you because I find your answer also very helpful. It doesn't mean that if I selected the best answer your answer is bad. I appreciate you for helping me. – Desg Aman May 08 '20 at 19:32
1

int numbers[n+2]; is the declaration of an array of ints with space for n + 2 ints, this is a variable lenght array and is not part of C++ standard, though some compilers allow it it's not somenthing you should use.

If you need a variable lenght array use std::vector.

With int numbers[n+2]; if n is equal to 0 you still have space for 2 ints, if you have int numbers[n]; the array will have space for 0 ints, so the code will fail because you are trying to access memory that does not exist with numbers[0] and numbers[1].

There are several good ways to implement the Fibonacci sequence, in the site you can find many questions regarding this matter in several programming languages, here is one of them Fibonacci series in C++

Edit

So I've seen your comments about using a vector, for making the sequence you wouldn't need the vector just two variables to store the two numbers to add, to store the sequence in a vactor, you can do somenthing like:

#include <iostream>
#include <vector>
#include <iomanip>

//passing the vector by reference
void fastFibonacci(unsigned long long n, std::vector<unsigned long long>& sequence) {

   unsigned long long first = 0;
   unsigned long long second = 1;
   sequence.push_back(first); //add first values to the vector
   sequence.push_back(second); //add first values to the vector
   for (unsigned long long i = 0, value = 0; i < n && value <= LLONG_MAX ; ++i) {
      value = first + second;
      first = second;
      second = value;
      sequence.push_back(value); //adding values to the vector
   }  
}

int main() {  
   unsigned long long limit; //number of values in the sequence
   int num = 1;
   std::vector<unsigned long long> sequence; //container for the sequence

   std::cout << "Enter upper limit: ";
   std::cin >> limit;
   fastFibonacci(limit, sequence);

   //print the sequence in a range based loop formatted with <iomanip> library
   for(auto& i : sequence){
      std::cout << std::setw(4) << std::left << num++ << " " << i << std::endl; 
   }
   return 0;
}

If you want to print just one of the numbers in the sequence, just use, for instance:

std::cout << sequence[10];

Instead of the whole vector.

The code you post in the comment to the other answer won't work because the access to the vector is out of bounds in numbers[i] = numbers[i - 1] + numbers[i - 2];, if for instance i = 5, your vector only has 2 nodes but you are accessing the 6th node numbers[5].

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    @DesgAman, you're welcome I added to the answer to address your question regarding `int numbers[n];`. – anastaciu May 08 '20 at 17:11
  • @DesgAman, I saw your comment in the other answer, I posted a possible solution and the reasons why the new code is ill-formed, I hope it helps, if you have any doubts you can ask. – anastaciu May 08 '20 at 22:16