+ 2
Array
Why have we got some result ? With index that not exist #include <iostream> using namespace std; int main() { int arr[] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 15; x++) { sum += arr[x]; } cout << sum << endl; return 0; }
1 Resposta
+ 14
When using an array literal to initialize an array (providing the data in {}), the array is made the correct size to hold the given data. This array could also be initialized like this:
int arr[5] = { 11, 35, 62, 555, 989 };
C++ will not throw an error for using an array index that doesn't exist. Instead, you will retrieve a garbage value, which is why you still receive an answer.