Midterm (AY22/23)
Last updated
Last updated
For two integers a
and b
, as long as abs(b)
is bigger than abs(a)
, a / b = 0
.
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.
Using this convention, we can easily see that for the option D, the else if
condition is always false
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)
.
Whenever you encounter a flowchart that contains the below pattern,
Create a new if
block when converting it into C
code!!!
Also a very good type of question to test your recursion thinking. Worth to try again.
Include the essence of the remainder operator % in the cheatsheet!
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.
if
block