Basic Knowledge
该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。
Problem 1. Basic knowledge
- Read the following code. Which of the following statements is/are true?
int a[] = {1, 2, 3};
int b[] = {10, 11, 12};
int c[100] = {1};
int main(void){
...
}
{{ multiselect(1) }}
- The type of
aisint [3]. - The type of
aisint *. - We can write
b = a;inmainfunction, this operation could copy arrayato arrayb. - We can write
b = a;inmainfunction, this operation could letbbe a pointing array which points to arraya. - We can write
b = {100, 101, 102};inmainfunction, this operation could reassign value in arrayb. - After initialization, array
chas only one elementc[0]is1, all other elements are0. - Array
chasc[0]...c[100]all 101 elements.
- (1) Read the following code. Which of the following statements is/are true?
void swap(int *pa, int *pb) {
int tmp = *pa;
*pa = *pb;
*pb = tmp;
}
int main(void) {
int x = 10, y = 15;
int *px = &x;
int *py = &y;
swap(px, py);
printf("%d %d\n", x, y);
}
{{ multiselect(201) }}
- The asterisk (
*) in the function parameter declarationint *pais a part ofint *, meaning thatpais a pointer toint. - The asterisk (
*) in the statementint tmp = *pais the dereference operator, which is used to access the object thatpapoints to. - The asterisk (
*) in the statement*pb = tmpis the pointer symbol, meaning thatpbis a pointer. - The asterisk (
*) in the statement*pb = tmpis the dereference operator, which is used to access the object thatpbpoints to. - We can change the declaration and initialization of
pxandpytoint* px = &x, py = &y, which is the same as we do in the code.
(2) Which of the following statements is/are true?
{{ multiselect(202) }}
-
The code cannot swap correctly, because we swapped the values that
pxandpypoint to, but not the values ofxandy. -
If we rewrite the function
swapas followsvoid swap(int a, int b) { int tmp = a; a = b; b = tmp; }and replace
swap(&x, &y)withswap(x, y)in themainfunction, the values ofxandycan be exchanged successfully. -
The parameter declarations of
swapcan be rewritten asint *pa, pb, becausepaandpbare of the same type. -
The parameter declarations of
swapcan be rewritten asint *pa, *pb, becausepaandpbare of the same type. -
The function
swapwill output the values of*paand*pb. -
None of the above.
- Read the following code. Which of the following statements is/are true?
void swap(int *pa, int *pb) {
int tmp = *pa;
*pa = *pb;
*pb = tmp;
}
int a[] = {1, 2, 3};
int b[] = {9, 9, 8, 2, 4, 4, 3, 5, 3};
int main(void){
int *pa = a, *pb = b;
swap(pa, pb);
for (int i = 0; i < 3; ++i)
printf("%d ", pa[i]);
for (int i = 0; i < 3; ++i)
printf("%d ", pb[i]);
}
{{ multiselect(3) }}
-
The code cannot run, because we cannot assign value of an array to a pointer.
-
After swap, array
aand arraybare exchanged successfully, arrayahas content{9,9,8,2,4,4,3,5,3}and arraybhas content{1,2,3}. -
The swap cannot be done successfully, because
aandbare of different length, this would incur array subscript out of range. -
The code would output
9 2 3 1 9 8. -
If we rewrite the function
swapas follows
void swap(int **ppa, int **ppb){
int *tmp = *ppa;
*ppa = *ppb;
*ppb = tmp;
}
and replace swap(pa, pb) with swap(&pa, &pb) in the main function, the code would output 9 9 8 1 2 3 .
- Write down the type of each expression. For pointer types, place exactly one space between the pointer type and the first asterisk, and no spaces between consecutive asterisks (e.g.
int *,int **, orint ***, but notint*orint * *).
int *pa;
int x;
| Expression | pa |
*pa |
&pa |
*&pa |
x |
&x |
|---|---|---|---|---|---|---|
| Type | {{ input(401) }} | {{ input(402) }} | {{ input(403) }} | {{ input(404) }} | {{ input(405) }} | {{ input(406) }} |
- Read the following code.
int min_element(int *array, int l, int r) {
int pos = l;
while (l < r) {
if (array[l] < array[pos])
pos = l;
++l;
}
return pos;
}
int a[] = {1, 4, 2, 8, 5, 7};
(1) Which of the following statements is/are true?
{{ multiselect(501) }}
min_element(array, l, r)returnsarray[i].min_element(array, l, r)returnsarray[i].min_element(array, l, r)returns the index of the first occurrence ofarray[i].min_element(array, l, r)prints the index of the first occurrence ofarray[i]to the standard output.- To make
min_element(array, l, r)return the index of the last occurrence ofarray[i], replacearray[l] < array[pos](on line 4) witharray[l] <= array[pos].
(2) Suppose we have the following main function.
int main(void) {
printf("%d\n", min_element(a, 0, 6));
}
Which of the following statements is/are true?
{{ multiselect(502) }}
- The function
maincontains undefined behavior. - Calling
min_element(a+2, 0, 6)contains undefined behavior. - Calling
min_element(NULL, 0, 0)contains undefined behavior. - The type of the parameter
arrayinmin_elementwe called isint [6]. - The type of the parameter
arrayisint [], where the length is determined at run-time and may change. - The type of the parameter
arrayisint *. - Since we want to find the minimum element in the entire array
a, we can simply writemin_element(a), and the parameterslandrwill be assigned with0and6automatically.
- Given the functions
swapandmin_elementas defined in the previous two questions. Read the following code.
void sort(int *array, int n) {
for (int i = 0; i < n - 1; ++i) {
int min_pos = min_element(array, i, n);
swap(&array[i], &array[min_pos]);
// Note: If you have difficulty understanding this function, uncomment the following lines, run the code and look at the output.
// for (int i = 0; i < n; ++i)
// printf("%d ", array[i]);
// printf("\n");
}
}
int main(void) {
int a[] = {8, 5, 7, 1, 4, 2};
sort(a, 6);
for (int i = 0; i < 6; ++i)
printf("%d ", a[i]);
printf("\n");
}
(1) Regarding the sort function, which of the following statements is/are true?
{{ multiselect(601) }}
swap(&array[i], &array[min_pos])is equivalent toswap(array + i, array + min_pos).- At each iteration, the smallest element in
{array[i], ..., array[n - 1]}is found and then placed at indexiby swapping witharray[i]. - The effect of
sorton the array is not changed if we rewrite the loop condition asi < n. sort(array, n)sorts the elements{array[0], ..., array[n - 1]}in non-decreasing order.
(2) Which of the following statements is/are true?
{{ multiselect(602) }}
- The program prints
1 2 4 5 7 8. - If we replace
sort(a, 6)assort(a, 4), the output becomes1 2 4 5 8 7. - If we replace
sort(a, 6)assort(a, 3), the output becomes5 7 8 1 4 2. - If we replace
sort(a + 2, 4), the output becomes8 5 1 2 4 7, i.e. only the last four elements are sorted.