0% found this document useful (0 votes)
12 views11 pages

Cs Workshop2

The document contains various programming exercises related to validation checks, error correction, and flowchart analysis for O Level Computer Science. It includes code snippets that require identification of validation methods, error correction in loops, and modifications to find maximum values instead of minimum. Additionally, it features tasks to complete trace tables for given inputs in different programming scenarios.

Uploaded by

Zahra Lamia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Cs Workshop2

The document contains various programming exercises related to validation checks, error correction, and flowchart analysis for O Level Computer Science. It includes code snippets that require identification of validation methods, error correction in loops, and modifications to find maximum values instead of minimum. Additionally, it features tasks to complete trace tables for given inputs in different programming scenarios.

Uploaded by

Zahra Lamia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

O Level Computer Paper 02

|2
Programming Workshop

Workshop P
Afroz Mamun
11

11 This section of program code may be used as a validation check.

1 PRINT "Input a value between 0 and 100 inclusive"


2 INPUT Value
3 WHILE Value < 0 OR Value > 100
4 PRINT "Invalid value, try again"
5 INPUT Value
6 ENDWHILE
7 PRINT "Accepted: ", Value

(a) Give a name for this type of validation check.

............................................................................................................................................. [1]

(b) Describe what is happening in this validation check.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

............................................................................................................................................. [2]

(c) Complete the trace table for this program code using the test data: 200, 300, –1, 50, 60

Value OUTPUT

[3]

2210/21/M/J/19

www.himalayanspacecentre.org
12

12 This section of program code asks for 80 numbers between 100 and 1000 to be entered. It checks
that the numbers are in the correct range, and stores them in an array. It counts how many of the
numbers are larger than 500 and then outputs the result when the program is finished.

1 Count = 0
2 FOR Index = 1 TO 80
3 INPUT 'Enter a number between 100 and 1000', Number
4 WHILE Number = 99 AND Number = 1001
5 INPUT 'This is incorrect, please try again', Number
6 ENDWHILE
7 Num[80] = Number
8 IF Number > 500 THEN Count = Count + 1
9 UNTIL Index = 80
10 PRINT Index
11 PRINT ' numbers were larger than 500'

There are four lines of code that contain errors.

State the line number for each error and write the correct code for that line.

Error 1 Line Number .............................

Correct Code ....................................................................................................................................

Error 2 Line Number .............................

Correct Code ....................................................................................................................................

Error 3 Line Number .............................

Correct Code ....................................................................................................................................

Error 4 Line Number .............................

Correct Code ....................................................................................................................................


[4]

2210/23/O/N/17

www.himalayanspacecentre.org
13

13 Read this section of program code that inputs 10 positive numbers and then outputs the smallest
number input.

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

(i) Identify three changes you would need to make to find the largest number input instead
of the smallest number.

1 .........................................................................................................................................

...........................................................................................................................................

2 .........................................................................................................................................

...........................................................................................................................................

3 .........................................................................................................................................

.......................................................................................................................................[3]

(ii) Rewrite the program code with your changes.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[3]

2210/21/M/J/16

www.himalayanspacecentre.org
14

14 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 .........................................................................................................................................

..................................................................................................................................................... [8]

2210/22/O/N/16
www.himalayanspacecentre.org
15

15 (a) This pseudocode inputs an integer. The predefined function DIV gives the value of the
division, e.g. Y 10 DIV 3 gives the value Y = 3. The predefined function MOD gives the
value of the remainder, e.g. Y 10 MOD 3 gives the value Y = 1.

INPUT X
WHILE X > 15
DO
T1 X DIV 16
T2 X MOD 16
CASE T2 OF
10:OUTPUT A
11:OUTPUT B
12:OUTPUT C
13:OUTPUT D
14:OUTPUT E
15:OUTPUT F
OTHERWISE OUTPUT T2
ENDCASE
X T1
ENDWHILE
CASE X OF
10:OUTPUT A
11:OUTPUT B
12:OUTPUT C
13:OUTPUT D
14:OUTPUT E
15:OUTPUT F
OTHERWISE OUTPUT X
ENDCASE

(b) Complete the trace table for the input data:

48, 57

2210/23/O/N/15

www.himalayanspacecentre.org
16

16 This flowchart inputs the marks gained in an examination. An input of –1 ends the routine.

START

Total 0
Count 0
Distinction 0

INPUT Mark

Is Mark = –1 ? Yes
Total Total + Mark
Count Count + 1

No

No Is OUTPUT "Number of
Mark >= Distinctions ", Distinction
80 ? OUTPUT "Average Mark "
Total/Count
Yes

Distinction Distinction + 1 END

Complete the trace table for the mark input data: 50, 70, 65, 30, 95, 50, 55, 85, 65, 35, –1, 45

Total Count Distinction Mark OUTPUT

[4]
2210/22/M/J/19

www.himalayanspacecentre.org
17

17 (a) The flowchart inputs an integer. The predefined function DIV gives the integer result of the
division, e.g. Y 10 DIV 3 gives the value Y = 3. The predefined function MOD gives the
value of the remainder, e.g. Y 10 MOD 3 gives the value Y = 1.

START

Posn @ 1
New @ 0

INPUT X

T1 @ X DIV 2
T2 @ X MOD 2
New @New+T2 * Posn
Posn @Posn * 10

X @T1

Yes is T1
>= 2?

No

New @New+T1 * Posn

OUTPUT New

END

(b) Complete the trace table for the input data:

48, 57

2210/22/O/N/15

www.himalayanspacecentre.org
18

18 The flowchart below calculates the number of tins of paint required to paint walls. The flowchart
inputs the height and width of a wall in metres, the number of doors and the number of windows.
A value of –1 for the height stops the input.

START

Area 0
Tins 0

INPUT Height,
Width, Doors,
Windows

Is Yes
Height Tins INT(Area/10 + 0.5)
= –1?

No

OUTPUT Tins
Area Area + Height * Width
– Doors * 1.5 – Windows

END

Complete the trace table for the input data:

3, 5, 1, 0, 3, 7, 0, 0, 3, 5, 0, 3, 3, 7, 1, 1, –1, 0, 0, 0

Area Tins Height Width Doors Windows

[4]

2210/22/M/J/16
www.himalayanspacecentre.org
19

19 (a) This flowchart checks a batch of 10 rice sacks for weight. Sacks should weigh 50 kilograms
each. Sacks weighing over 50.5 kilograms or less than 49.5 kilograms are rejected. The
number of sacks accepted and the number of sacks rejected is output.

START

Accept 0
Reject 0
Count 0

Is Count Yes
= 5?
OUTPUT
Accept Accept + 1 Accept,
No
Reject

Count Count + 1
END

INPUT
Sack

Is Sack Yes Reject Reject + 1


< 49.5?

No

Is Sack
No > 50.5? Yes

(b) Complete the trace table for the input data:

49.5, 50.0, 50.6, 50.5, 52.0

2210/22/O/N/17

www.himalayanspacecentre.org
20

20 The flowchart checks the level of chlorine and the depth of water compared to the height of the
swimming pool. Error messages are output if a problem is found.

START

INPUT Height,
Depth, Chlorine

OK True

OUTPUT
Is Depth > Yes
"Water too
Height/2 ? deep"

No
OUTPUT
Is Depth < Yes "Water too
Height/3 ? shallow"

No
OK False

Yes OUTPUT "Too


Is Chlorine much chlorine
>3 ? add more water"
No

OUTPUT "Too
Yes little chlorine
Is Chlorine
<1 ? add more chlorine"
No

No
Is OK = True ?

Yes

OUTPUT "Pool OK
to use"

END

2210/22/O/N/18

www.himalayanspacecentre.org

You might also like