In the below code snippet, for example, take n as 9 and the array a's elements as {5,5,4,5,5,5,4,5,6}. Sublime text and visual studio code are showing the correct output as 3 but Leetcode online ide is showing "runtime error: index 8 out of bounds for type 'int [k]' ". I want to know why sublime text and visual studio code are not showing the runtime error, and if there is an ide/text editor which shows runtime error, please recommend it to me.
int k = n - 1;
int temp[k];
for (int i = 0 ; i < k ; i++)
{
temp[i] = (a[i] - a[i + 1]);
}
int res = INT_MIN;
for (int i = 0 ; i < k ; )
{
int x = temp[i];
int c = 0;
while (x == temp[i])
{
i++;
c++;
}
res = max(res, c + 1);
}
cout<<res;
int k = n - 1;
int temp[k]; // variable length array, must be either #define k <constant num>
// or 'const int k'
The variable-length arrays (VLAs) aren't supported in C++ standard. The Sublime Text & VS Code doesn't matters at all, but the compiler.
You might've not enabled your compiler warnings, thus, you're unable to see any error. You can do it by appending -pedantic
flag in your compilation option to see all warnings.