ARRAYS 2. Choose answers that are applicable for the following questions. i. Whi
ID: 3738913 • Letter: A
Question
ARRAYS
2. Choose answers that are applicable for the following questions.
i. Which of the following is valid C code for displaying all of the elements in the array letters from the previous question on one line, but in reverse order?
a. int i;
for (i = 3; i > 0 ; i-- )
? { printf(" %c", letters[i - 1]) ; }
b. int i;
for (i = 2; i >=0; i-- )
{printf("%c", letters[i+1]) ; }
c. int i;
for (i = 0; i < 3 ; i++)
{ printf(" %c" , letters[i]) ; }
d. int i;
for (i = 1; i<=3; i++)
{printf(" %c", letters[i]) ; }
e. int i;
for (i = 3; i > 1 ; i++)
{ printf(" %c", letters[i]); }
f. none of the above
ii. Complete the following code to fill an array, then select the correct continuation test for the for-loop.
int main ()
{
int size, i;
scanf("%d" &size);
int arr[size];
for( i=l; ____________________________; i++)
{
scanf("%d" , ________________________________);
? printf("%d", arr[i]);
?}
}
a. The code will not work with any continuation test because the first element read is saved in position [1] of the array instead of index position [0].
b. i < size+l, because a variable (size) is used to store the array length.
c. It is impossible to say what the continuation test should be because we do not know ahead of time how large size is.
d. Because the array values are obtained by reading in values inside the loop, we must use i < arr[size].
e. i <= arr[size] must be used so that the code will fill the last position in the array.
f. The test i < size should be used so that statements in the body of the loop are not executed past the largest index number array.
iii. Select the code that was used to produce this pattern:
* * * *
* * * *
* * * *
?a. int i, j;
for ( j = 1; j < 3; j--)
{
for ( i = 1; i < j; i++)
{
printf(" %c", '*');
}
printf(" ");
}
b. int i, j;
for ( i = 0; i <=4; i++)
{
for ( j = i + 1; j < 4; j++)
{
printf(" %c," '*');
}
printf(" ");
}
c. int i, j;
for ( i = 1; i<=4; i++)
{
for ( j = i; j,<3; i++)
{
printf(" %c", '*');
}
printf(" ");
}
?d. int i, j;
for ( i = 1; i < 4; i++)
{
for ( j = 1; j <= 4; j++)
{
printf(" %c," '*');
}
printf(" ");
}
ii. Complete the following code to fill an array, then select the correct continuation test for the for-loop.
int main ()
{
int size, i;
scanf("%d" &size);
int arr[size];
for( i=l; ____________________________; i++)
{
scanf("%d" , ________________________________);
? printf("%d", arr[i]);
?}
}
a. The code will not work with any continuation test because the first element read is saved in position [1] of the array instead of index position [0].
b. i < size+l, because a variable (size) is used to store the array length.
c. It is impossible to say what the continuation test should be because we do not know ahead of time how large size is.
d. Because the array values are obtained by reading in values inside the loop, we must use i < arr[size].
e. i <= arr[size] must be used so that the code will fill the last position in the array.
f. The test i < size should be used so that statements in the body of the loop are not executed past the largest index number array.
iii. Select the code that was used to produce this pattern:
* * * *
* * * *
* * * *
?a. int i, j;
for ( j = 1; j < 3; j--)
{
for ( i = 1; i < j; i++)
{
printf(" %c", '*');
}
printf(" ");
}
b. int i, j;
for ( i = 0; i <=4; i++)
{
for ( j = i + 1; j < 4; j++)
{
printf(" %c," '*');
}
printf(" ");
}
c. int i, j;
for ( i = 1; i<=4; i++)
{
for ( j = i; j,<3; i++)
{
printf(" %c", '*');
}
printf(" ");
}
?d. int i, j;
for ( i = 1; i < 4; i++)
{
for ( j = 1; j <= 4; j++)
{
printf(" %c," '*');
}
printf(" ");
}
e. None of the above.Explanation / Answer
Question i
Answer:
b. int i;
for (i = 2; i >=0; i-- )
{printf("%c", letters[i+1]) ; }
Question ii
Answer:
f. The test i < size should be used so that statements in the body of the loop are not executed past the largest index number array.
Question iii
Answer:
d. int i, j;
for ( i = 1; i < 4; i++)
{
for ( j = 1; j <= 4; j++)
{
printf(" %c," '*');
}
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.