I am taking an online class and I want to know where I went wrong in a flowchart assignment. I received a score of 66%, which indicates that I may have made a mistake. I believe my answer was correct, although I may not have used the most efficient approach.
The Prompt :
Now, draw the flowchart for an algorithm (described below) that will find the two smallest values in the collection. You can assume that the collection has at least two values and that all values are distinct.
The algorithm works by making one pass through the collection and keeping track of the two smallest values seen so far:
Initialize the two smallest values so far to be the first two elements of the list
For each remaining value in the list: if the value is smaller than either of the two smallest so far, replace the larger of the two
There are a few ways to represent the two smallest values seen so far, but for this assignment, please use the following approach:
Set “min1” to the first value of the collection and “min2” to the second
Next, look at each remaining element in the collection:
If the element is less than “min1” and “min1” is less than “min2”,
Then set “min2” to that element’s value
Otherwise, if the element is less than “min1” but “min1” is greater than “min2”,
Then set “min1” to that element’s value
Otherwise, if the element is greater than “min1” and is less than “min2”,
Then set “min2” to that element’s value
After inspecting all elements, output “min1” and “min2,” which hold the two smallest values
I made this flowchart:
What could be the reason(s) I didn't get the maximum score?
Some of the issues:
The main issue is that the flowchart has a missing arrow: it does not define what to do when the last test ("item>min1 AND item<min2") is false.
The phrase "set ... to ..." is used in the opposite sense. The assignment uses this phrase in the propper way, like "set “min2” to that element’s value", but in the flowchart you have it like "set item to min2". Although I understand what you mean it is important to be clear about the direction of the assignment, and it is common practice to first mention the target of the assignment, and then the value to be assigned. So in your flowchart it should actually read "set min2 to item", or even better, use an arrow, like "min2 ← item".
The flowchart has a test for an empty list. Since it is a given that there are at least two items in the list, there are some remarks to make:
If you wouldn't have included the is-empty test, it would have been a good indication that you had read the assignment carefully. I wouldn't assume that including this unnecessary test would be harmless to your score.
Having included it, it is odd (pun intended) that there is no test for when the list has just one item. If indeed the list could have fewer than two items, then the flowchart is not correct.
There are several ways to make a flowchart, and what is considered correct could depend on what your course material has. For instance, it is not uncommon to actually ask a question in a decision-box (diamond). So like "Is item<min1 and min1<min2?", and the outgoing arrows could have labels "Yes" and "No" instead of "True" and "False".
Another remark concerns the logic presented in the assignment, so it cannot be considered a mistake in your flowchart:
If the first two tests are false, then it follows that item>min1. Having this condition tested in the bottom case is overkill: it is guaranteed to be true. So that last test could be expressed as "Is item<min2?" only.