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
  • 11. Call Stack Diagram
  • 12. Recursion
Edit on GitHub
  1. Current Year Exam

Final (AY24/25)

PreviousPE2 (AY24/25)NextVim & Unix

Last updated 6 months ago

Problems

11. Call Stack Diagram

The first tricky point is in the declaration of the function inn. Here long *p[2] seems to create an array, however, it is actually declaring a pointer to pointer here! And when drawing the call stack diagram, you should only draw one variable, which is a pointer to pointer that points to the p in the main. (See more at )

The tricky point in this question actually lies in "printing" part. Is it a crash or undefined behavoir? Not too sure for now.

But an interesting point here is that the actual output of this program is "30". That's crazy right! This let's me have a deeper thinking about what's happening here. Now, the question is, how is the stack frame of inn not cleared/destroyed?

This reminds me of the working principle of calling a program in C. Indeed, we are using the idea of "stack". But the actual working principle of "removing" a stack is not clearing all its content but moving an amazing stuff called "stack pointer" back to its "jumping" location (sorry for this kind of weird words cuz I forgot the jargons ). (If you want further discussion, I think I need to study/review more about the Computer Organization part, iirc there is also something called pc (program counter), this will help us decide the exact location when our stack pointer moves back).

So, in this question, after calling inn, we move over stack pointer back to memory location that stores the instruction inn(p) (this will have the effect of "destroying" the inn stack frame, but actually it is not destroyed!) and maybe our pc will contain the address of the next instruction to be executed, which is cs1010_println_long(*p[1]). And then, we will execute the "print" instruction here. But notice or not, the content in the memory location of inn stack frame is still there! So theoritically speaking, we can still access that location and won't generate any error (from the computer organization side).

Okie, I just found that I am probably wrong, when calling inn, we are branching to another location maybe. Nvm, wait for future me to correct this bah hahahaha

And another reason why we are lucky (still get 30) is because we are not calling other functions, which will overwrite the content in the inn stack frame).

12. Recursion

This year the recursion seems to be a bit "easier" hahaha.

The most common mistake I can think of (myself ) is to try the largest coin every time. This is not the optimal solution. e.g. If your input is 40, using the "largest" method, you will get 3, which is 40=20+15+540=20+15+540=20+15+5. But the optimal solution should be 2, which is 40=20+2040 = 20+2040=20+20.

But, this wrong method can somehow give us some hints on how to arrive on the optimal solution. That's for every possible coin, we find the number of candidates it needs and update min to be the option with the smallest number of candidates.

long num_of_coins(long amount)
{
  if (amount == 0)
  {
    return 0;
  }
  coins[5] = {5, 10, 20, 25, 50};
  long min = LONG_MAX;
  for (i = 0; i < 5; i += 1)
  {
    if (amount >= coins[i])
    {
      long candidate = num_of_coins(amount - coins[i]) + 1;
      if (candidate < min)
      {
        min = candidate;
      }
    }
  }
  return min;
}
    
😂
😂
😂
Pass an array to a function