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
  • 3. Integer Division
  • 6. Redundant Comparisons
  • 11. Flowchart*
  • 12. Recursion*
  • Tips
Edit on GitHub
  1. Past Year Exam
  2. Midterm Paper

Midterm (AY22/23)

PreviousMidterm (AY21/22)NextPE1 Review

Last updated 8 months ago

Problems

3. Integer Division

For two integers a and b, as long as abs(b) is bigger than abs(a), a / b = 0.

6. Redundant Comparisons

Before we talk about this question, let's take a look at what are redundant comparisons from :

A condition is redundant if it is always true or always false.

Note that this condition can be part of the logical expression! So, if we can find any part of the logical expression that is always true or false, and if this part is a comparison, then this part will be a redundant comparison

Using this convention, we can easily see that for the option D, the else if condition is always false

// Option D
if (x > y) {
    foo();
} else if (x > y && x == 0)
    bar();
}

For option C, since in the else-if block, we can always say that x <= y, which means x > y is always false, so the condition can be simplified as else if (x == 0).

11. Flowchart*

Whenever you encounter a flowchart that contains the below pattern,

Create a new if block when converting it into C code!!!

12. Recursion*

Also a very good type of question to test your recursion thinking. Worth to try again.

Note that in this question, the 0 is skipped!!!

Tips

  1. Include the essence of the remainder operator % in the cheatsheet!

  2. For loop invariant, always remember to check every option in the question! Also remember to include the three requirements for the loop invariants in the cheatsheet.

CS1010 Notes
Create a new if block