0
#include <iostream>
using namespace std;

void printfn(int arr[], int n){
    if( n == 2 ){
        arr[0] = 0;
        arr[1] = 1;
        return ;
    }
    printfn(arr, n-1);
    arr[n-1] = arr[n-2] + arr[n-3];
}

int main()
{
    int n;
    cin >> n;
    int arr[n];
    
    printfn(arr, n+1);
    cout << arr[n];
    
    return 0;
}

In this problem when I try we can clearly see that array arr has been defined on the indices 0 to n-1.

When the last line of the code cout << arr[n] is executed, it gives correct answer but that should not happen as my array's maximum permissible index is n-1.

Code Context : This code is for printing the nth number in a fibonacci series of n numbers.

I have used vscode's debugger but it shows the elements of the array till n-1 only.

Where is this nth element stored? What am I missing?

Mat
  • 202,337
  • 40
  • 393
  • 406
Jasjeet Singh
  • 99
  • 1
  • 4

0 Answers0