Midterm (AY18/19)
Problems
8. Program Flow
Which of the following function or functions correctly compute the sum of all integers between i and j, inclusive?
The function below, which uses a while
loop, can achieve "inclusivity".
Because the last time you enter the loop, x
will be j-1
. Then execute line 5
, x
will be j
. Then execute line 6
, j
will be added to sum
also.
This tells us that in a while
loop, where the loop update is done before the loop body, the second part of loop condition is inclusive.
10. Assertion
a logical expression that must always be true for the program to be correct.
After knowing this, we can observe our question. Here, we use a do-while
loop, which means we will enter the loop once at least. And the question becomes, what is always true at the line // { ?? }
, which is also the end of the function.
We can divide the problem into two small parts:
What will happen if we exit the loop
What will happen in our loop
For the first part, it is trivial. If we exit the loop, we just need to negate the loop condition. So, our assertion for the first part should be !a
.
For the second part, we can further divide it into two parts:
Either we enter the
if-else
structure, orWe do not enter the
if-else
structure.
For the first part, we can safely state that X || Y
will always be true.
For the second part, we can assert that !b && !c
to be always true.
Thus, forming our assertion for these two, we can assert that (X || Y) || (!b && !c)
. This assertion will be true during every iteration of the loop! Also, this will help us think of the general form of the assertion at the end of the if-else
structure.
Then, we should think about what's the relationship between these two assertions we have made: !a
and (X || Y) || (!b && !c)
. Since it is a do-while
loop, we will enter the loop body at least once. So, at the end of the function, we will always have (X || Y) || (!b && !c)
to be true. And since we have exited the loop already, so !a
must be true at the same time! So, we should use the &&
operator to connect these two assertions.
General Form of Assertion for "if-else" structure
The
if-else
structure is . (a, b, X, Y, Z
are all logical expressions)
The assertion we can make at // { ?? }
is
The
if-else
structure is . (a, b, X, Y
are all logical expressions)
The assertion we can make at // { ?? }
is
11. Algorithm
Design an algorithm to find the second largest integer from a given list L = with k elements. You can assume that k ≥ 2 and the list L does not contain any repetition (i.e., the same number does not appear twice).
The main idea behind this problem is to use two variables, one to store the max, the other to store the second-max. And update the second-max only when what you have encountered is a max or if it is not the max, it is bigger than the current second-max.
One thing to notice is that you should initialize the second-max to be and max to be the first element of the list, . The example flowchart is below,
12. Flowchart
This is a "give-away" question itself, but one thing we should pay attention to is how to point the arrows from an if/else if
(diamond) block to to another diamond. The example flowchart is below:
14. Invariant*
A loop invariant is an assertion that is true before the loop, after each iteration of the loop, and after the loop.
it is true at the end of the first iteration of the loop
if it is true at the end of the -th iteration of the loop, then it is true at the end of the -th iteration.
Now, we can take a look at the question
The first three parts are tivial
Then all we need to do is to find pattern, the first pattern I found when doing this question is . But let's see why this invariant is wrong.
Now, let's try other patterns based on what we get, we can see the the pattern should be . And use this to do the last part about how to prove your loop invariant during each iteration of the loop body, which is very important!
At Line C, assuming x == n Â- y*k
,
after
y += 1
, we havex == n Â- (y - 1)*k
.In the next line, we decrement
x Â-= k
, sox + k == n Â- (y - Â1)*k
, simplifying the righthand side, we havex == n - y*k
again.
15. Recursion
This recursion is quite easy compared to the one that appears in the Midterm PE.
The solution using only one line can be:
Tips
Add the flowchart of find the maximum number of a given list in your cheatsheet. (Use this to get a glimpse of how to draw the flowchart)
Include the 10. Assertion and 14. Invariant* into cheatsheet.
Last updated