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 . 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 . 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 is bigger than both and , then max will be , our function will output the correct answer. But think it reversely, if is less than and , our max will be zero and the actual max should be either or .
Now, we have come to one of our solutions, that's when , our function will output wrongly.
Similarly, we can get the other two conditions
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
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
Problem Set 10
Problem 10.1
&&
Problem 10.2
Based on the assertion we have derived in the comment in the code above, we can safely say the string "ok" will be printed.
Last updated