Cs Workshop1
Cs Workshop1
Workshop P. | 1
Programming Workshop
ON
O Level Computer Science
By:
www.himalayanspacecentre.org
1
Himalayan Space Centr
1 Write an algorithm to input 1000 numbers. Count how many numbers are positive and how
many numbers are zero. Then output the results. Use either pseudocode or a flowchart.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
.............................................................................................................................................. [6]
2210/21/M/J/18
www.himalayanspacecentre.org
2
2 This section of program code asks for 50 numbers to be entered. The total and average of the
numbers are calculated.
1 Total = 0
2 Counter = 50
3 PRINT ′When prompted, enter 50 numbers, one at a time′
4 REPEAT
5 PRINT ′Enter a number′
6 INPUT Number
7 Total + Number = Total
8 Number = Number + 1
9 UNTIL Counter = 50
10 Average = Number * Counter
11 PRINT ′The average of the numbers you entered is ′, Average
State the line number for each error and write the correct code for that line.
2210/21/M/J/17
www.himalayanspacecentre.org
3
3 An algorithm has been written in pseudocode to input 100 numbers and print out the sum.
A REPEAT … UNTIL loop has been used.
Count ← 0
Sum ← 0
REPEAT
INPUT Number
Sum ← Sum + Number
Count ← Count + 1
UNTIL Count > 100
PRINT Sum
Error ...........................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
[2]
(b) Rewrite the correct algorithm using a more suitable loop structure.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
2210/22/M/J/17
www.himalayanspacecentre.org
4
4 An algorithm has been written in pseudocode to input 100 numbers, select and print the
largest number and smallest number.
Count 1
INPUT Number
High Number
Low Count
REPEAT
INPUT Number
IF Number > High
THEN
High Number
ENDIF
IF Number > Low
THEN
Low Number
ENDIF
Count Count + 1
UNTIL Count = 99
PRINT "Largest Number is ", Number
PRINT "Smallest Number is ", Low
Find the four errors in the pseudocode and suggest a correction for each error.
Error 1 ........................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 2 ........................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 3 ........................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
Error 4 ........................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
[4]
2210/22/M/J/19
www.himalayanspacecentre.org
5
5 (a) Draw a flowchart for an algorithm to input numbers. Reject any numbers that are negative
and count how many numbers are positive. When the number zero is input, the process ends
and the count of positive numbers is output.
[6]
50.4, 50.3, 49.1, 50.3, 50.0, 49.5, 50.2, 50.3, 50.5, 50.6
[5]
(b) The size of the batch has increased to 50 sacks. It has been decided to only reject sacks that
are underweight.
2210/22/M/J/18
www.himalayanspacecentre.org
6
• inputs 10 numbers
• checks whether each number is within a specified range
• totals the numbers within the range and outside the range
1 InRange = 0
2 OutRange = 1000
3 FOR Count = 1 TO 10
4 INPUT Num
5 IF Num > 10 AND Num < 20 THEN InRange = InRange + 1
6 ELSE OutRange = OutRange - 1
7 Count = Count + 1
8 NEXT X
9 PRINT InRange, OutRange
Error 1 ........................................................................................................................................
Correction ..................................................................................................................................
...................................................................................................................................................
Error 2 ........................................................................................................................................
Correction ..................................................................................................................................
...................................................................................................................................................
Error 3 ........................................................................................................................................
Correction ..................................................................................................................................
...................................................................................................................................................
Error 4 ........................................................................................................................................
Correction ..................................................................................................................................
...............................................................................................................................................[4]
2210/23/O/N/16
www.himalayanspacecentre.org
7
7 An algorithm has been written in pseudocode to select a random number using the function
RandInt(n), which returns a whole number between 1 and the argument n. The algorithm then
allows the user to guess the number.
Number RandInt(100)
TotalTry 1
REPEAT
PRINT "Enter your guess now, it must be a whole number"
INPUT Guess
IF TotalTry > Number
THEN
PRINT "Too large try again"
ENDIF
IF Guess > Number
THEN
PRINT "Too small try again"
ENDIF
TotalTry Guess + 1
UNTIL Guess <> Number
TotalTry TotalTry - 1
PRINT "Number of guesses ", TotalTry
Find the four errors in the pseudocode and suggest a correction to remove each error.
Error 1 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 2 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 3 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
Error 4 ..............................................................................................................................................
Correction .........................................................................................................................................
..........................................................................................................................................................
[4]
2210/22/O/N/19
www.himalayanspacecentre.org
8
8 The flowchart checks the lengths of a batch of 10 ropes. For the batch to be accepted 90% of
the lengths need to be between 24.9 and 25.1 metres.
START
Reject 0
Count 0
Yes
Is Count =
10 ?
No
No
Count Count + 1 Is Reject
<= 1 ?
INPUT Length
Yes
OUTPUT "Batch
rejected"
No
Reject Reject + 1
OUTPUT "Batch
accepted"
END
24.88, 25.01, 24.98, 25.00, 25.05, 24.99, 24.97, 25.04, 25.19, 25.07
[4]
2210/22/O/N/19
www.himalayanspacecentre.org
9
9 Read this section of program code that should input 50 numbers and then output the average of
the positive numbers only.
1 Total = 0
2 PosCount = 0
3 FOR Counter = 1 TO 50
4 INPUT Num
7 Average = Total/PosCount
8 NEXT Counter
9 PRINT Num
Locate these errors and suggest code corrections to remove each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
2210/23/O/N/15
www.himalayanspacecentre.org
10
IF Response = 1
THEN
X X + Y
ELSE
IF Response = 2
THEN
X X – Y
ELSE
IF Response = 3
THEN
X X * Y
ELSE
IF Response = 4
THEN
X X / Y
ELSE
OUTPUT "No response"
ENDIF
ENDIF
ENDIF
ENDIF
(a) Name the type of statement demonstrated by the use of IF … THEN … ELSE … ENDIF
...................................................................................................................................................
............................................................................................................................................. [1]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
............................................................................................................................................. [4]
2210/23/O/N/19
www.himalayanspacecentre.org