Observe the following code:
const int SIZE = 3;
int nums[SIZE] = {1, 2, 3};
for (int i = 0; i <= SIZE; i++) {
std::cout << nums[i];
}
What is wrong with this code?
A: You cannot use constants with array definitions
B: The initialization list exceeds the size of the array
C: Should be i = 1 because you need to start with the first array element
D: Should be i < SIZE so that you don't access an array element that doesn't exist
#include<iostream>
using namespace std;
int main()
{
const int SIZE = 3;
int nums[SIZE] = { 1, 2, 3 };
for (int i = 0; i < SIZE; i++)
{
cout << nums[i];
}
//D: Should be i < SIZE so that you don't access an array element that doesn't exist
}
Comments
Leave a comment