Date | May 2021 | Marks available | 1 | Reference code | 21M.1.HL.TZ0.14 |
Level | HL | Paper | 1 | Time zone | no time zone |
Command term | State | Question number | 14 | Adapted from | N/A |
Question
The following flowchart is intended to represent an algorithm in which numbers that are input cannot be negative.
The flowchart contains a logic error that will affect the algorithm’s functionality.
The algorithm is to be altered to restrict the values that are input to whole numbers between 0 and 1000.
Identify the logic error in the algorithm.
Outline how the error in the algorithm identified in part (i) can be corrected.
State the name of the method that could be used to restrict the values that are input.
A further change has been requested for the algorithm to enable it to calculate the average of all the numbers entered. The average will be output when the algorithm terminates.
Based on the flowchart, construct this algorithm using pseudocode. You must include the required changes:
- correction of logic error
- only allow input of integers between 0 and 1000
- calculation of average of all numbers entered
- output of final average.
Markscheme
Award [1 max]
The algorithm will always output zero as the value of LOWEST, for any set of the input values (because all input values are greater than or equal to 0);
The LOWEST is initialized to 0 and negative inputs are not allowed, zero will be the permanently lowest number;
Award [2 max]
Example 1:
Replace LOWEST = 0;
With LOWEST=99999999/ any very high number;
Example 2:
Before the loop/ Accept “at the beginning”;
The first number NUMBER should be inputted and LOWEST set to NUMBER / LOWEST=input(NUMBER);
Loop only 999 times (because 1 number already inputted)/ condition in the loop should be changed to COUNTER>998;
Example 3:
If statement should be used within the loop;
after the statement ‘input NUMBER’;
For example, if COUNTER==0 then LOWEST = NUMBER/ set LOWEST to the first inputted number;
Award [1 max]
Validation (check);
Data type check;
Range check;
Award [8 max]
Correct initialization of HIGHEST, LOWEST and TOTAL;
Correct use of loop for 1000 inputs (doesn’t have to be while loop);
Checking if NUMBER is between 0 to 1000(accept inclusive or exclusive range);
Checking if NUMBER is a whole number;
Running total of numbers (for average calculation);
Correct comparison and changing the value of HIGHEST if needed;
Correct comparison and changing the value of LOWEST if needed;
Appropriate calculation of AVERAGE;
Output of AVERAGE, HIGHEST and LOWEST (outside loop);
Example answer 1
HIGHEST = −1 //any value lesser than or equal to 0
LOWEST = 10000 //any value greater than or equal to 1000
COUNTER = 0
TOTAL = 0
loop while COUNTER < 1000 // allow COUNTER <= 999
input NUMBER
if NUMBER<0 or NUMBER>1000 or NUMBER div 1 ≠ NUMBER/1
//accept NUMBER<1 or NUMBER>999 or NUMBER mod 1 ≠ 0
output " Your number is invalid, please try again"
else
if NUMBER > HIGHEST then
HIGHEST = NUMBER
end if
if NUMBER < LOWEST then
LOWEST = NUMBER
end if
TOTAL = TOTAL + NUMBER
COUNTER = COUNTER + 1
endif
end loop
AVERAGE = TOTAL / COUNTER // allow TOTAL / 1000
output HIGHEST
output LOWEST
output AVERAGE
Example answer 2: (the lowest and the highest are set to the value of the first inputted number)
input NUMBER
loop while NUMBER<0 or NUMBER>1000 or NUMBER div 1 ≠ NUMBER/1
output " Your number is invalid, please try again"
input NUMBER
end loop
HIGHEST = NUMBER
LOWEST = NUMBER
TOTAL = 0
loop COUNTER FROM 0 to 998
input NUMBER
loop while NUMBER<0 or NUMBER>1000 or NUMBER div 1 ≠ NUMBER/1
output " Your number is invalid, please try again"
input NUMBER
end loop
if NUMBER > HIGHEST then
HIGHEST = NUMBER
end if
if NUMBER < LOWEST then
LOWEST = NUMBER
end if
TOTAL = TOTAL + NUMBER
end loop
AVERAGE = TOTAL / 1000
output HIGHEST, LOWEST, AVERAGE
Example answer 3
HIGHEST = -1 //any value lesser than or equal to 0
LOWEST = 10000 //any value greater than or equal to 1000
C = 0
TOTAL = 0
loop while C<1000
input NUMBER
if NUMBER>=0 and NUMBER<=1000
if NUMBER mod 1 == 0
if NUMBER > HIGHEST then
HIGHEST = NUMBER
else
if NUMBER < LOWEST then
LOWEST = NUMBER
endif
end if
TOTAL = TOTAL + NUMBER
C=C+1
endif
else
output(“invalid input, enter another number”)
endif
end loop
output HIGHEST, LOWEST, TOTAL/1000
Examiners report
The term logic error was not always familiar to candidates. Many candidates correctly identified the logic error in Part(a)(i).
A large number gave less precise or incorrect answers to Part(a)(ii).
A large number of candidates did not score any marks for question in Part (b) because they were not able to state the name of the method that could be used to restrict the values that are input.
In Part (c) candidates were required to produce an algorithm based on the flowchart given in the question. Most candidates achieved some marks for this algorithm, but full marks were rare.