Answer to Question #326496 in C++ for nickname

Question #326496

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


1
Expert's answer
2022-04-09T17:53:31-0400
#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
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS