58.8k views
4 votes
What set of code correctly initializes all elements of the array ar to the value 0, given the declaration

int ar[3];
Group of answer choices

A. ndx = 1;
while (ndx < 3) {
ar[0] = ndx;
ndx++;
}

B. ndx = 1;
while (ndx < 3) {
ar[ndx] = 0;
++ndx;
}

C. ndx = 0;
while (ndx < 3) {
ar[ndx] = 0;
ndx++;
}

D. ndx = 0;
while (ndx < 3) {
ar[0] = ndx;
ndx++;
}

User Cybermaxs
by
4.2k points

1 Answer

4 votes

Answer:

The correct answer is:

C. ndx = 0;

while (ndx < 3) {

ar[ndx] = 0;

ndx++;

}

Step-by-step explanation:

The declaration given is:

int ar[3];

This means the array consists of three locations and is named as ar.

We know that the indexes are used to address the locations of an array and the index starts from 0 and goes upto to 1 less than the size of the array which means the indexes of array of 3 elements will start from 0 and end at 2.

Now in the given options we are using ndx variable to run the while loop.

So the code to assign zero to all elements of array will be

ndx = 0;

while(ndx<3)

{

ar[ndx] = 0;

ndx++;

}

Hence, the correct answer is:

C. ndx = 0;

while (ndx < 3) {

ar[ndx] = 0;

ndx++;

}

User Weiwei Yang
by
4.7k points