Task 1
What does the following program do?
1 Small = 1000
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Small = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Small
Run the trace table so you can find out the goal of the above program.
(i) Identify three changes you would need to make to find the largest number input instead of the smallest number.
(ii) Rewrite the program code with your changes (in Javascript)
Task 2
Read this section of program code that inputs 5 positive numbers and then outputs the total.
1 Total = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 Total = Total + Num
6 PRINT Total
7 Counter = Counter + 1
8 UNTIL Counter = 5
This code works, but it is inefficient.
(i) Suggest three improvements that could be made.
(ii) Rewrite the program code with your improvements (in Javascript)
Task 3
Read this section of code that inputs the ages of people entering an event. The input sequence is ended by inputting a negative value for age. The code outputs the number of people at the event over the age of 18.
01 Num18 = 0
02 INPUT Age
03 WHILE Age >= 0 DO
04 IF Age >= 18 THEN
05 Num18 = Num18 + Age
06 ENDIF
07 ENDWHILE
08 PRINT Num18 - Age
There are four errors in this code.
Locate these errors and suggest code correction to remove each error.
Error 1 ............................................................................................................................................... Correction.......................................................................................................................................... ..........................................................................................................................................................
Error 2 ............................................................................................................................................... Correction.......................................................................................................................................... ..........................................................................................................................................................
Error 3 ............................................................................................................................................... Correction.......................................................................................................................................... ..........................................................................................................................................................
Error 4 ............................................................................................................................................... Correction.......................................................................................................................................... ..........................................................................................................................................................
Task 4
Read this section of program code that inputs positive numbers, discards any negative numbers and then outputs the average. An input of zero ends the process.
1 Total = 0
2 Counter = 100
3 REPEAT
4 REPEAT
5 INPUT Num
6 UNTIL Num < 0
7 Total = Total + 1
8 Counter = Counter + Num
9 UNTIL Num = 0
10 Average = Total / (Counter - 1)
11 Print Average
There are four errors in this code.
Locate these errors and suggest a correction to remove each error.
Error 1 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 2 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 3 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 4 ..............................................................................................................................................
Correction .........................................................................................................................................
.....................................................................................................................................................
(b) Explain the differences between a WHILE … DO … ENDWHILE and a REPEAT … UNTIL loop.
Task 5
Rewrite the following pseudocode algorithm using a WHILE loop.
INPUT Num
FOR Counter ← 1 TO 12
Num ← Num * Counter
OUTPUT Num
NEXT Counter