Question #3168

What will be the values of k[1] and k[3] after execution of the code segment below using the data shown?

int k[6] = {0, 0, 0, 0, 0, 0};

int i, n;

for (i = 3; i < 6; ++i) {

scanf("%d", &n);

k[n] = i;

}

int k[6] = {0, 0, 0, 0, 0, 0};

int i, n;

for (i = 3; i < 6; ++i) {

scanf("%d", &n);

k[n] = i;

}

Expert's answer

This program asks the user enter three values being the indexes for elements of array k, and assigns to elements with these indexes the values 3, 4, 5 respectively.

So for safity it is necessary to enter numbers from 0 to 5.

Thus the values of k[1] and k[3] depend on indexes which the user enter.

For example

if the user input 0,1,2 then k[1]=4, k[3]=0

if the user input 3,2,4 then k[1]=0, k[3]=3

if the user input 5,0,3 then k[1]=0, k[3]=5

So for safity it is necessary to enter numbers from 0 to 5.

Thus the values of k[1] and k[3] depend on indexes which the user enter.

For example

if the user input 0,1,2 then k[1]=4, k[3]=0

if the user input 3,2,4 then k[1]=0, k[3]=3

if the user input 5,0,3 then k[1]=0, k[3]=5

## Comments

## Leave a comment