Question 1 Report
A procedure needs to return multiple values to the calling code.
(a) Write pseudocode for a procedure MinMaxAvg that takes an array of 10 integers and uses BYREF parameters to return the minimum, maximum and average values. [5]
(b) Write pseudocode for the calling code that uses this procedure and outputs the results. [2]
(c) Explain why a procedure with BYREF parameters is used instead of a function for this task. [1]
(a) The MinMaxAvg procedure uses BYREF parameters to return three computed values to the caller.
PROCEDURE MinMaxAvg(Values : ARRAY[1:10] OF INTEGER, BYREF MinVal : INTEGER, BYREF MaxVal : INTEGER, BYREF AvgVal : REAL)
DECLARE Total : INTEGER
DECLARE i : INTEGER
MinVal ← Values[1]
MaxVal ← Values[1]
Total ← 0
FOR i ← 1 TO 10
IF Values[i] < MinVal THEN
MinVal ← Values[i]
ENDIF
IF Values[i] > MaxVal THEN
MaxVal ← Values[i]
ENDIF
Total ← Total + Values[i]
NEXT i
AvgVal ← Total / 10
ENDPROCEDUREThe BYREF keyword means changes to MinVal, MaxVal, and AvgVal inside the procedure directly modify the caller's variables. [1] Both MinVal and MaxVal are initialised to the first element. [1] The loop checks each element against both the current minimum and maximum. [1] Total accumulates the sum, and the average is calculated after the loop. [1] [1]
(b) Calling code:
DECLARE Data : ARRAY[1:10] OF INTEGER
DECLARE Low : INTEGER
DECLARE High : INTEGER
DECLARE Mean : REAL
CALL MinMaxAvg(Data, Low, High, Mean)
OUTPUT "Minimum: ", Low
OUTPUT "Maximum: ", High
OUTPUT "Average: ", MeanAfter the procedure call, Low, High, and Mean hold the computed values because they were passed by reference. [1] [1]
(c) A function can only return a single value, but this task needs to return three values (minimum, maximum, and average). A procedure with BYREF parameters can modify multiple variables in the calling code, effectively returning multiple values at once. [1]
Everything you need to excel in your exams