Question: The following program generates an error. Why? const int NUM _ ELEMENTS = 5 ; int userVals [ NUM _ ELEMENTS ] ; unsigned int

The following program generates an error. Why?
const int NUM_ELEMENTS =5;
int userVals[NUM_ELEMENTS];
unsigned int i;
userVals [0]=1;
userVals [1]=7;
userVals [2]=4;
for NUM_ELEMENTS; ++i){
}
cout userVals [i] endl;
The for loop tries to access an index that is out of the array's valid range.
Variable i is declared as an unsigned integer.
The integer NUM_ELEMENTS is declared as a constant.
The array userVals has 5 elements, but only 3 have values assigned.
 The following program generates an error. Why? const int NUM_ELEMENTS =5;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!