Consider the following pseudocode and appointment data. What does the algorithm print?
sum <- 0
count <- 0
FOR EACH row IN AppointmentTable
IF row.Status = "Scheduled"
sum <- sum + row.Urgency
count <- count + 1
ENDIF
ENDFOR
avg <- sum / count
PRINT avg
Student Urgency Status
Alice K. 4 Scheduled
Bob D. 2 Completed
Michael S. 5 Scheduled
Javier T. 3 Canceled
3.54.5914Status is Scheduled update the running values, so sum ends at 9, count ends at 2, and avg prints 4.5.
Explanation
Selection filters the data set so the algorithm processes only the rows that matter to the question. The loop visits each row one at a time, but the IF test decides which rows are allowed to change sum and count. The final division happens once, after every row has been visited.
- A is not correct: This is what you get by adding the
Urgencyof all four rows and dividing by four, which ignores theStatustest that selects only the Scheduled rows. - C is not correct: This is the value held in
sumafter the loop finishes, reported before the algorithm divides bycountto form the average. - D is not correct: This adds the
Urgencyvalue of every row, so it skips both the filtering condition and the division that turns a total into an average.