graph LR
accTitle: Maximum-value candidate A
accDescr: The maximum starts at the first list value, the index starts at one, and larger later values replace the maximum.
A((Start)) --> IN[/L/]
IN --> B[Set max to<br>first element]
B --> C[Set i to 1]
C --> D{"Is i<br>less than<br>L length?"}
D -- No --> E[/Output max/]
D -- Yes --> F{"Is L[i]<br>greater<br>than max?"}
F -- Yes --> G["Set max<br>to L[i]"]
F -- No --> H[Increment i by 1]
G --> H
H --> D
E --> I((End))
Finding the max. value in a list
Which of the following flowcharts correctly represents an algorithm for finding the maximum value—labeled max—in a list L? (Note: More than one flowchart may be correct.)
For example:
- if
L = [1, 2, 3, 4, 5], the algorithm should output5. - if
L = [5, 4, 3, 2, 1], the algorithm should output5. - if
L = [1, 3, 2, 5, 4], the algorithm should output5. - if
L = [5, 6], the algorithm should output6.
You can consider that the list L:
- Is non-empty and contains at least two elements.
- Is indexed starting from 0.
Choose the correct flowchart from the options below:
- Flowchart in Figure 1 (a)
- Flowchart in Figure 1 (b)
- Flowchart in Figure 1 (c)
- Flowchart in Figure 1 (d)
- Flowchart in Figure 1 (e)
Figure 1 groups the five candidate flowcharts for comparison.
graph LR
accTitle: Maximum-value candidate B
accDescr: The maximum starts at the first list value, the index starts at zero, and each larger value replaces the maximum.
A((Start)) --> IN[/L/]
IN --> B[Set max to<br>first element]
B --> C[Set i to 0]
C --> D{"Is i<br>less than<br>L length?"}
D -- No --> E[/Output max/]
D -- Yes --> F{"Is L[i]<br>greater<br>than max?"}
F -- Yes --> G["Set max<br>to L[i]"]
F -- No --> H[Increment i by 1]
G --> H
H --> D
E --> I((End))
graph LR
accTitle: Maximum-value candidate C
accDescr: The maximum starts at zero and the index at one. Until the index is greater than the list length, a larger value at the index replaces the maximum and the index increases by one.
A((Start)) --> IN[/L/]
IN --> B[Set max to 0]
B --> C[Set i to 1]
C --> D{"Is i<br>greater than<br>L length?"}
D -- Yes --> E[/Output max/]
D -- No --> F{"Is L[i]<br>greater<br>than max?"}
F -- Yes --> G["Set max<br>to L[i]"]
F -- No --> H[Increment i by 1]
G --> H
H --> D
E --> I((End))
graph LR
accTitle: Maximum-value candidate D
accDescr: The maximum starts at zero and the index at zero. While the index is less than the list length, a smaller value at the index replaces the maximum and the index increases by one.
A((Start)) --> IN[/L/]
IN --> B[Set max to 0]
B --> C[Set i to 0]
C --> D{"Is i<br>less than<br>L length?"}
D -- No --> E[/Output max/]
D -- Yes --> F{"Is L[i]<br>less than<br>max?"}
F -- Yes --> G["Set max to L[i]"]
F -- No --> H[Increment i by 1]
G --> H
H --> D
E --> I((End))
graph LR
accTitle: Maximum-value candidate E
accDescr: The maximum starts at the first list value and the index at one. Until the index equals the list length, a smaller value at the index replaces the maximum and the index increases by one.
A((Start)) --> IN[/L/]
IN --> B[Set max to<br>first element]
B --> C[Set i to 1]
C --> D{"Is i<br>equal to<br>L length?"}
D -- Yes --> E[/Output max/]
D -- No --> F{"Is L[i]<br>less than<br>max?"}
F -- Yes --> G["Set max<br>to L[i]"]
F -- No --> H[Increment i by 1]
G --> H
H --> D
E --> I((End))