Final (AY18/19)
Last updated
Last updated
Question paper without answers:
Question paper with answers:
Answer sheet:
In this question, what we should pay attention to is that since string has a null character ('\0'
) at the end. So, when you calloc a string of length len
, the len-1
-th element will be '\0'
. Hence, when you manipulation each of its character using a for-loop like below, you should stop at the len-2
-th element, which means the terminating condition should be i < len - 1
.
Uninitialized variables may cause strange behavior, and you cannot tell what will be going.
For a loop like below:
To judge its time complexity quickly, we can only pay attention to the terminating condition and the loop changing condition to decide the time complexity of the loop. In this problem, the time complexity for the outer loop is just . And for inner loop, it's just . So, the overall time complexity will be .
Note that this method of only look at terminating condition and loop changing condition works at most of the cases except for
the edge cases. e.g. for (i=0;i<n;i*=2)
, here it's an infinite loop and
when the initialization involves terms of input n
. e.g. for(i=n;i<n+1;i+=1)
.
This is the same with:
In CS1010, the convention for the drawing of the call stack diagram is shown below (including how to draw arrays, pointers and variables):
This kind of diagram is very useful when doing pointer questions! Remember to draw it every time you deal with pointer questions!
When working with strings in C, always account for the null character by stopping the loop at len - 1
(excluding) to avoid overwriting it.
Uninitialized variables can cause unpredictable behavior and lead to errors that are difficult to diagnose.
Include the operator precedence in C in the cheatsheet, e.g. ->, &
, which one is first operated.
Include the 15. Call Stack Diagram in the cheatsheet. (Use the AY22/23 Version)
It was taught in . Pay attention to the operating sequence using macro. If not braket is included, we just insert the expression into the macro and follows the +,-,*,/ operation sequence.
Include the into Cheatsheet.