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
  • 6. Logical Expression
  • 10. Invariant*
  • 11. Recursion*
  • Tips
Edit on GitHub
  1. Past Year Exam
  2. Midterm Paper

Midterm (AY20/21)

PreviousMidterm (AY18/19)NextMidterm (AY21/22)

Last updated 8 months ago

Problems

6. Logical Expression

This question assesses whether students can form a logical expression given a set of conditions.

For this kind of question about forming a logical expression given a set of conditions, if the conditions are not too many, we should try the Truth Table Method. That is list all the possible conditions and see the output's relationship between the different set of possibilities.

The columns of the table are the bool variables given in the table, the output is bool. Since we have three variables, so the total possibilities will be 23=82^3=823=8, which means we will have 8 rows in total.

is_with_gf
is_with_mom
is_raining
umbrella?

Y

Y

Y

Y

Y

Y

N

Y

Y

N

Y

Y

Y

N

N

Y

N

Y

Y

N

N

Y

N

N

N

N

Y

Y

N

N

N

N

Then, what we need to do is to find all the rows that make the output True/Y. We will find that when is_with_gf == true, output is always true. When !is_with_mom && is_raining, the output will be true. Otherwise, the output will be false. So, we can form our final logical expression as:

is_with_gf || (!is_with_mom && is_raining)

10. Invariant*

This question is another tricky question about the invariant. But before we start finding the invariant, let's find what the code is doing by substituting some numbers in.

After a series of trial and error, we may find that the function is basically doing res = n/3 + n/7. And we should find out that n = i-1. Using these two results, we can form our invariant that relates res and i as res = (i - 1)/3 + (i - 1)/7. And we start proving that the loop variant holds at Line A.


Proof:

Let's assume that the loop invariant holds at the start of the loop. After that, we may or may not enter the first if structure.

  1. Enter the if structure, which means i % 3 == 0. Now, our res = (i - 1)/3 + (i - 1)/7 + 1. Since i % 3 == 0, we can find that i % 3 == (i - 1) % 3 + 1. So, our invariant will become res = i / 3 + (i - 1)/7.

  2. Not enter the if structure, which means i % 3 != 0. Now, we can say the (i - 1) / 3 == i / 3, so we can also get res = i / 3 + (i - 1)/7.

Simiarly, we can do the analysis on the second if structure and finally we will get res = i/3 + i/7. Now, we execute i = i + 1. Our invariant will become res = (i - 1)/3 + (i - 1)/7, which is the same as when we enter the loop.

Sometimes if it is hard to prove the loop invariant from forward, we can use the answer and try to prove it backward (think of what we want at last and then think forwardly about how can we get that)

11. Recursion*

This is a recursion question, but it wants to test how to convert the if-else structure recursion to a single return statement including logical expressions only. But the basic idea is to write the if-else structure recursion first!!!

So, for this question, the normal are_coprime_upto_c() should be

// returns true if a and b share no common factors from 2 to c
bool are_coprime_upto_c(long a, long b, long c) {
    if (c == 1)
    {
        return true;
    }
    if (a % c == 0 && b % c == 0)
    {
        return false;
    }
    return are_coprime_upto_c(a, b ,c - 1);
}

First thing first, to make our thinking easier, let's consider when we will return true.

  1. When c==1 or

  2. when !(a % c == 0 && b % c == 0) && are_coprime_upto_c(a, b ,c - 1), assuming that are_coprime_upto_c() will return as expected.

Knowing this, we can form our single return statement with the logical expression only easily. That's

return ( c == 1 || (!(a % c == 0 && b % c == 0) && are_coprime_upto_c(a, b, c - 1))

Tips

  1. If the variables are not too much, always try the Truth Table Method since it will make your life and analysis much easier!

  2. "Imprecisions using floating-point values might mean that certain conditions may be evaluated incorrectly", this occurred when you want to do the equality test between a floating number and an integer.

  3. When you want to form a single return statement in recursion, write out the normal if-else recursion first, then consider under which cases we will get true output.

  4. Before we try to form the invariant, do some examples and see what the code is doing!!!

As discussed in . To prove a program is false, one counterexample is enough. But to prove an algorithm is true, we may use assertion or some other proof techniques. Usually try to find counterexample first!!!

Problem 11.1
Q6
Q10
Q11