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
a
isint [3]
. - The type of
a
isint *
. - We can write
b = a;
inmain
function, this operation could copy arraya
to arrayb
. - We can write
b = a;
inmain
function, this operation could letb
be a pointing array which points to arraya
. - We can write
b = {100, 101, 102};
inmain
function, this operation could reassign value in arrayb
. - After initialization, array
c
has only one elementc[0]
is1
, all other elements are0
. - Array
c
hasc[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 *pa
is a part ofint *
, meaning thatpa
is a pointer toint
. - The asterisk (
*
) in the statementint tmp = *pa
is the dereference operator, which is used to access the object thatpa
points to. - The asterisk (
*
) in the statement*pb = tmp
is the pointer symbol, meaning thatpb
is a pointer. - The asterisk (
*
) in the statement*pb = tmp
is the dereference operator, which is used to access the object thatpb
points to. - We can change the declaration and initialization of
px
andpy
toint* 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
px
andpy
point to, but not the values ofx
andy
. -
If we rewrite the function
swap
as followsvoid swap(int a, int b) { int tmp = a; a = b; b = tmp; }
and replace
swap(&x, &y)
withswap(x, y)
in themain
function, the values ofx
andy
can be exchanged successfully. -
The parameter declarations of
swap
can be rewritten asint *pa, pb
, becausepa
andpb
are of the same type. -
The parameter declarations of
swap
can be rewritten asint *pa, *pb
, becausepa
andpb
are of the same type. -
The function
swap
will output the values of*pa
and*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
a
and arrayb
are exchanged successfully, arraya
has content{9,9,8,2,4,4,3,5,3}
and arrayb
has content{1,2,3}
. -
The swap cannot be done successfully, because
a
andb
are 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
swap
as 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
main
contains 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
array
inmin_element
we called isint [6]
. - The type of the parameter
array
isint []
, where the length is determined at run-time and may change. - The type of the parameter
array
isint *
. - Since we want to find the minimum element in the entire array
a
, we can simply writemin_element(a)
, and the parametersl
andr
will be assigned with0
and6
automatically.
- Given the functions
swap
andmin_element
as 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 indexi
by swapping witharray[i]
. - The effect of
sort
on 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.