AP CSP Day 47: List Algorithm - Finding Maximum
Share
Big Idea 3
Day 47 Practice
Focus: List Algorithm - Finding Maximum
Practice Question
Consider the following procedure:
PROCEDURE findMax(numbers)
{
max ← numbers[1]
FOR EACH num IN numbers
{
IF (num > max)
{
max ← num
}
}
RETURN max
}Why This Answer?
The algorithm correctly finds the maximum. Initializing with numbers[1] is valid - the FOR EACH will check it again and update max if needed.
Why Not the Others?
B) If max is at [1], it stays as max throughout.
C) Works for negative numbers - just finds the largest (least negative).
D) Works for single element - returns that element.
Common Mistake
Watch Out!
Overthinking the initialization. Starting with numbers[1] is fine because the loop will compare it against itself.
AP Exam Tip
For finding max/min, any list element is a valid starting point if the loop checks all elements.