Catalan number problem

The code is running fine, but the problem is that we ahve declared array cn of size n, so consifering 0 index, it should run till n-1. But it is working for. how can this happen. And the answer should be cn[n-1] not cn[n].

#include
using namespace std;
int main() {
int n;
cin>>n;
long long cn[n];
cn[0] = cn[1] = 1;
for(int i = 2; i<=n; i++){
cn[i] = 0;
for(int j = 0; j<i; j++){
cn[i] += cn[j]*cn[i-j-1];
}
}
cout<<cn[n]<<endl;;

return 0;

}

Hey @Username_cb,
Yes sometimes it may work for n because array is stored as a contiguous memory . But if that memory is already occupied by some other program then the program will throw a runtime error . As it is very rare for programs to have their memory so close to each other that is why no error is occurred . If you try for n+1 as well it will not give any error but a garbage value .
So your program works but only as long as the nth index is free.

Hope it helps :blush: .
If your doubt is now resolved please mark it as resolved and rate me accordingly .
Else you can further questions here .
Thankyou