Question 1 Report
A shop records the prices of items sold during a day. The shopkeeper enters prices one at a time, using -1 to indicate the end of input.
(a) Write pseudocode to calculate and output: the total sales, the number of items sold, the average price per item, the most expensive item sold, and the cheapest item sold. [6]
(b) State what the initial value of a variable used to track the "most expensive" item should be. Explain your choice. [2]
(c) Explain why the program should check that at least one item was entered before calculating the average. [2]
(d) Describe the difference between a WHILE loop and a REPEAT...UNTIL loop. [2]
(a) The algorithm uses a sentinel (-1) to end input, tracks the total, count, max, and min. The first valid item initialises both max and min trackers: [6]
DECLARE Price : REAL
DECLARE Total : REAL
DECLARE Count : INTEGER
DECLARE Average : REAL
DECLARE MaxPrice : REAL
DECLARE MinPrice : REAL
Total ← 0
Count ← 0
OUTPUT "Enter price (-1 to finish): "
INPUT Price
IF Price <> -1 THEN
MaxPrice ← Price
MinPrice ← Price
ENDIF
WHILE Price <> -1 DO
Total ← Total + Price
Count ← Count + 1
IF Price > MaxPrice THEN
MaxPrice ← Price
ENDIF
IF Price < MinPrice THEN
MinPrice ← Price
ENDIF
OUTPUT "Enter price (-1 to finish): "
INPUT Price
ENDWHILE
OUTPUT "Total sales: ", Total
OUTPUT "Items sold: ", Count
IF Count > 0 THEN
Average ← Total / Count
OUTPUT "Average price: ", Average
OUTPUT "Most expensive: ", MaxPrice
OUTPUT "Cheapest: ", MinPrice
ELSE
OUTPUT "No items entered"
ENDIF[1 mark for sentinel loop, 1 mark for totalling, 1 mark for counting, 1 mark for max tracking, 1 mark for min tracking with correct initialisation, 1 mark for average with zero check]
(b) The initial value for the maximum tracker should be the first valid item entered, not 0 or an arbitrary large/small number. [1] Initialising with the first actual data value ensures accurate tracking regardless of the data range. If set to 0 and all prices are positive, 0 would never be replaced as the minimum unless the code explicitly handles it. [1]
(c) If no items are entered (Count = 0), calculating the average would require dividing Total by 0. [1] This division by zero would cause a runtime error and crash the program. The check allows the program to display an informative message instead. [1]
(d) A WHILE loop tests the condition before the loop body executes, so the body may never run if the condition is initially false. [1] A REPEAT...UNTIL loop tests the condition after the loop body executes, so the body always runs at least once. [1]
Everything you need to excel in your exams