-4
int n;cin>>n;
int arr[n]{};

I have a small problem ,why is this decleration of array wrong?I have used it on Codechef several times until recently i got a WA!

After this I declared array as,

int n;cin>>n;
int arr[1001]{0} ; //max size of input possible

I got an AC!

Jarod42
  • 203,559
  • 14
  • 181
  • 302

1 Answers1

1

You can not declare an array in c++ of variable length .But you can create an array of variable length dynamically.

int n;
cin>>n;
int*arr = new int[n];
Dev
  • 11
  • 3