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
  • Problem Set 9
  • Problem 9.1
  • Problem 9.2
  • Problem Set 10
  • Problem 10.1
  • Problem 10.2
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Tutorial

Tut 03 - More on Conditionals

Thanks for my tutor Eric Han!

Problem Set 9

Problem 9.1

Problem 9.1(a)

In the snippet of code, we can easily find out that a counterexample is a=b=c≠0a=b=c\neq0a=b=c=0. Our program will output 0. That's incorrect.

Problem 9.1(b)

To find all the test cases that the function would fail, we can start from the counterexample we have given in Problem 9.1(a).

What if any two of these three numbers are equal and not zero?

Let's say a=b≠0a=b\neq0a=b=0. That means all the if conditions that contain a==b will output false and due the the short-circuiting property of the && operator, the first two conditions won't pass and max is still 0. Now we have only one condition remaining. How to make it "false"?

If ccc is bigger than both aaa and bbb, then max will be ccc, our function will output the correct answer. But think it reversely, if ccc is less than aaa and bbb, our max will be zero and the actual max should be either aaa or bbb.

Now, we have come to one of our solutions, that's when 0≠a=b>c0\neq a=b>c0=a=b>c, our function will output wrongly.

Similarly, we can get the other two conditions

  1. 0≠b=c>a0\neq b=c>a0=b=c>a

  2. 0≠a=c>b0\neq a=c>b0=a=c>b

Adding the counterexample from Problem 9.1(a), we have written all the cases when our function will output wrongly.

Problem 9.2

To solve this question, we should form our "truth table" first

vac(p)
vac(q)
child(p)
child(q)
same_household(p,q)

yes

yes

dc

dc

dc

yes

no

dc

yes

yes

no

yes

yes

dc

yes

no

no

yes

yes

yes

Then, we use this table to form our code

if (vac(p) && vac(q))
    return true;
if (same_household(p, q))
{
    if (vac(p) && child(q))
        return true;
    if (vac(q) && child(p))
        return true;
    if (child(p) && (child(q))
        return true;
}
return false;

Problem Set 10

Problem 10.1

  1. (x≤1)∣∣(y==10)(x\leq1)||(y==10)(x≤1)∣∣(y==10)

  2. eating∣∣!drinkingeating || !drinkingeating∣∣!drinking

  3. (!has_cs2030(!has\_cs2030(!has_cs2030 && !has_cs2113)∣∣!has_cs2040c!has\_cs2113) || !has\_cs2040c!has_cs2113)∣∣!has_cs2040c

Problem 10.2

long score = 4;
if (something) {
  score = 10;
} else {
  score = 0;
}
// { score == 0 || score == 10 }

if (score == 4) {
    score = 1;
} else {
    score += 10;
}
// { score == 20 || score == 10 }

if (score >= 10) {
    cs1010_println_string("ok");
} else {
    cs1010_println_string("failed");
}

Based on the assertion we have derived in the comment in the code above, we can safely say the string "ok" will be printed.

What's the use of assertion

Assertion can not only help us get a better view of our code flow, but can also help us find the dead code (the code that will never be executed) in our program. For example, in Problem 10.2, our dead code will be the condition that judge whether our score will be 4.

PreviousTut 02 - Functions and ConditionalsNextTut 04 - Loops

Last updated 8 months ago