CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Problems
  • 5. Recursion
  • 6. Memory Leak
  • 7. Multi-dimensional Array Decay
  • 8. Illegal Memory Access
  • 15. Recursion
  • 16. Call Stack Diagram
  • Tips
Edit on GitHub
  1. Past Year Exam
  2. Final Paper

Final (AY22/23)

PreviousFinal (AY21/22)NextFinal (AY23/24)

Last updated 6 months ago

Problems

Question paper without answers:

Question paper with answers:

5. Recursion

In this question, every time we need to make two recursive calls. So, we need to think both cases to decide how we can modify to make the recursion finite!

6. Memory Leak

Whenever you call a malloc(), always be careful not to point the pointer to else where. This will cause memory leak!

7. Multi-dimensional Array Decay

Since an array in C consists only of a contiguous region of memory that stores the elements of the array, the address of an array is the same as the address of the first of the array.

However, &image[1][0], &image[0][1], &image[1][1] all have the type as a long, which is not an array. For image[0] + 1, it has the , and its value is the second long in the first row! Wrong also.

For the correct answer &image[1], firstly, image[1] is of type long, but add & operator will change it to type "array". So, now it is of type array (second array) and has value of the address of the first element in the second row.

8. Illegal Memory Access

This is a trickly question in the final paper. Illegal Memory Access needs to satisfy two requirements:

  1. you try to the memory address

In this the code below:

list[0] = calloc(ncols * nrows, sizeof(long));
for (size_t i = 1; i < nrows; i += 1)
{
    list[i] = list[i - 1] + ncols;
}

We are allowed to have pointers pointing to arbitrary region in memory, but as long as we don’t those memory, we are not doing anything illegal.

15. Recursion

The official solution for this problem is awesome!

bool can_sum_to (long a[], size_t i , size_t n, long q)
{
  if (q == a[i])
  {
    return true;
  }
  if (i >= n - 1)
  {
    return false;
  }
  return can_sum_to(a, i + 1, n, q) || can_sum_to(a, i + 1, n , q - a[i])
}

For the recursion part, it utilises the idea that we can narrow our list range by either using the a[i] to try forming our sum or not using the a[i] to form our sum.

For the base case, every time we check whether our "updated" q is equal to the first element or not, if it is, means we can find one such combination that sums to q in the list. Otherwise, if the range of the list is 0 or negative, means that we cannot find such a combination.

The recurrence relation is T(n)=2T(n−1)+1T(n)=2T(n-1)+1T(n)=2T(n−1)+1. And use the basic knowledge of geometric sequence, we can get T(n)=O(2n)T(n)=O(2^n)T(n)=O(2n).

16. Call Stack Diagram

The quesition is to draw the stack diagram for the following code:

void qux(long **ptrptr, long *numptr2)
{
  long *numptr1 = numptr2;
  *ptrptr = numptr2;
  *numptr1 = 57;
  // Line X
}

void baz(long **ptrptr, long *numptr)
{
  qux(ptrptr, numptr);
}

int main()
{
  long *ptr;
  long num = 1;

  baz(&ptr, &num);
}

The answer should be:

The most important thing to note is inside baz() to call qux(), we are actually passing the address of the num and ptr in the main()! So the pointer should point to these two variables in the main(), instead of pointing to the temp variable in the baz()!

Tips

See for more detail information. But this statement is pretty useful:

With this information, image + 1, first, use the knowledge of , the pointer image points to an array of long *, so +1 will move it to the second row. So, it has the type as the second row (an array) and its value is the address of the first long in the row.

the memory address is Illegal (NULL and the examples in which appears in Final (AY20/21))

If list[0] is NULL, as long as we don't access its memory address (read/write), we are not doing anything illegal. (See this )

Include the five examples in the cheatsheet and include the small observation behind the examples into the cheatsheet also.

question
Pointer Arithmetic
Array Name Decay (Multidimensional Array)
Array Name Decay (Multidimensional Array)
145KB
final-2223-s1-sheet.pdf
pdf
Question without Answers
127KB
final-2223-s1-with-comments.pdf
pdf
Question with Answers
Stack Diagram with Pointer & Double Pointers
9. Illegal Memory Access