Computer Science A Level Paper 2 Revisio PDF
Computer Science A Level Paper 2 Revisio PDF
Paper-2
Article # 252
A LEVEL NOTES SERIES
CIE SYLLABUS CODE 9608
2017 – 18 Edition
Fawad Khan
GULBERG | JOHAR TOWN | WAPDA TOWN | SADDAR CANTT | DHA PHASE-I | DHA PHASE-IV
For Books Order: 0336-531-4141
All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted,
in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without the prior
written permission of the Publisher.
DISTRIBUTORS
LAHORE KARACHI
BOOK WISE BURHANI BOOK CENTRE
12-Urdu Bazar, Lahore Shop # 6 Hashmi Trust Building Rotson Road
Tel 042-37112265, 042-37361468 New Urdu Bazar Karachi
Tel 021-32212640
BOOKLAND
16-Urdu Bazar, Lahore MARYAM ACADEMY
Tel 042-37124656, 042-37223210 Mool Chand DiyaRaam Building Sindh
Karachi.
NATIONAL BOOK SERVICE Tel: 021-32214243 , 021-32634243
22. Urdu bazar lahore.
Ph. 042-37247310, 042-37355262 RAWALPINDI / ISLAMABAD
BOOK VALLEY
BURHANI BOOK CENTRE Shop No. A-P-4,China market 5 Brothers
Shop # 7, Mian Market, Ghazni Street,Urdu Plaza College Road Rawalpindi
Bazar Lahore. Tel: 051-35770894, 051-35551630
Ph:042-37312
PREFACE
This is a comprehensive and carefully balanced compilation of all the relevant topics
that need to be essentially covered and understood by any O Level candidate who
wishes to ace his Chemistry paper. We have managed to adopt a purely focused
and goal oriented approach in this context that would enable students not only to
grasp the content but also assist them in analysing and evaluating individual
components.
Fawad Khan
Contents
Unit 1: Teacher’s Guide ...................................................................................................... 4
1.1 Context ................................................................................................................... 4
1.2 Outline .................................................................................................................... 4
1.3 Teaching time ......................................................................................................... 4
1.4 Programming languages ......................................................................................... 4
1.5 VB.Net Resources .................................................................................................. 4
Unit 2: Algorithms ............................................................................................................... 6
2.1 Pseudocode ............................................................................................................ 7
2.2 Program Flowcharts .............................................................................................. 17
Unit-3: Procedural high-level programming languages ................................................. 24
3.1 Program statements.............................................................................................. 24
3.2 Programming terms .............................................................................................. 24
3.3 Writing maintainable programs.............................................................................. 25
3.4 Matching Pseudocode and VB codes ................................................................... 26
3.5 Sample illustrations + VB Practical on basic programming constructs ................... 28
3.6 Further exercises on VB basic programming constructs ....................................... 39
Unit-4: Structure program development .......................................................................... 42
4.1 Modular/top down approach/design or stepwise refinement .................................. 42
4.2 Top down design/Structure/Jackson diagram........................................................ 46
Unit-5: Subroutines ........................................................................................................... 50
5.1 Procedure ............................................................................................................. 50
5.2 Function ................................................................................................................ 51
5.3 Parameters and local and global variables ............................................................ 53
5.4 Samples illustrations + VB practical on procedures, function, parameter (known as
formal parameters) and arguments (known as actual parameters) .................................. 61
5.5 Further exercises on functions and procedures ..................................................... 65
Unit-6: Structure Chart ..................................................................................................... 70
6.1 Symbols used in structure chart ............................................................................ 70
6.2 Structure chart representation of sequence code .................................................. 71
6.3 Structure chart representation for selection code .................................................. 74
6.4 Structure chart representation of Iteration code .................................................... 75
6.5 Combination of sequence, selection and iteration code ........................................ 77
6.6 Further exercises on structure chart ...................................................................... 79
Unit-7: VB in Built Functions............................................................................................ 82
Computer Science P-2 2 Contents
Topics
Unit-1
– Context
– Outline
– Teaching time
– Programming languages
Guide
A Level
Computer
Science
P-2 NOTES
Fawad Khan
Cell: 0321-6386013
[email protected]
1.1 Context
– Paper 2 should be completed prior to starting Paper 4.
– It is recommended that this paper be taught in a practical way with learners having
access to a computer that supports VB.Net (console mode).
– Learners should be encouraged to write their own programs, debug and execute them
using a computer with the assistance of their teacher
1.2 Outline
Paper 2 provides learners with knowledge and understanding of the following core aspects of
problem-solving and programming:
– Algorithm design
– Data representation
– Programming
– Software development
Topics
Unit-2
Pseudocode
Program Flowcharts
Alogrithms
A Level
Computer
Science
P-2 NOTES
Fawad Khan
Cell: 0321-6386013
[email protected]
Unit 2: Algorithms
An algorithm is a set of instructions that either solves a problem or informs the user that there
is no solution.
Representing algorithms
There are two main ways of representing algorithms using:
– Pseudocode
– Flowchart
Common keywords used when writing algorithms
Several keywords are often used to indicate common input, output, and processing operations.
(i) Keywords used to take data from a user
– INPUT, READ, OBTAIN, GET
– Example: Input radius
(ii) Keywords used to give information to a user
(iii) Keywords used for processing data
– COMPUTE, CALCULATE, DETERMINE, DO
– Example: Calculate volume
(iv) Keywords used for declaring variables
– INITIALISE, SET
– Example: Set count to 0 (Le. count = 0)
(v) Keywords used to increase a variable value
– ADD, INCREMENT
– Example: Add 1 to count (i.e. count = count + 1)
(vi) Keywords used to decrease a variable value
– SUB, DECREMENT
– Example: Decrement count by 1 (i.e., count = count -1)
Case Study: Consider the movement of an electronic toy
An electronic toy can move over the floor according to commands that it is given through a
keypad. As it moves, it draws a line. It can obey the instructions shown in table below:
Instruction Meaning
Forward n Move forward n cm
Backward n Move backwards n cm
Left n Turn left n degrees
Right n Turn right n degrees
Repeat n Repeat the instruction which follows n times
Computer Science P-2 7 Unit-2: Algorithms
Class Activity 1:
Write an algorithm for the toy to draw the diagram shown below:
10 cm
25 cm
Start
5 cm
2.1 Pseudocode
Pseudocode is the language that combines programming terminology or high-level
programming language terms (e.g. if, while, repeat etc.) and ordinary English (nouns and
verbs: number, total, set count to 0 etc.). It is a halfway house (i.e. combines two features)
between written English and the program code for the problem.
The idea is that, once the pseudocode is written, a programmer using any high-level language
(e.g. VB, Java, C ++ etc.) should be able to write the program code from the pseudocode
design.
Pseudocode and programming constructs/structures
Algorithms and programs are expressed using 4 basic structures/constructs:
– Assignment construct (Le. assigning values to identifiers)
– Sequence construct (i.e. instructions which are executed one after the other)
– Selection construct (Le. if statements or select case statements}
– Iteration construct (i.e. loops)
Computer Science P-2 8 Unit-2: Algorithms
Example: Declare a variable Name with initial value Fawad and another variable Pie with
initial value 3. 142
Name " Fawad "
Pie 3.142
Class Activity 2:
Write a pseudocode that declares two variables (Mark1 and Mark2) that stores initial values
40 and 90 respectively.
2.1.2 Sequence construct
This means a two or more statements are going to be executed one after the other.
Example: Using pseudocode, write an algorithm that calculates the sum of two numbers
input by a user.
Input num1
Input num2
Print num1 + num2
Note: The pseudocode below is an example where instructions are not executed in sequence
1. Input num1
2. Goto line 4
3. Print num1 + num2
4. Input num2
5. Goto line 3
Class Activity 3:
Write the Pseudocode that will allow a user to calculate volume of a cylinder using formula.
V = pie * radius * radius * height.
2.1.3 Selection constructs
It is used when a decision or comparison has to be made in the form of a condition. If the
condition is true, it will execute certain instructions else if it is false, it will execute another set
of instructions.
Testing a condition
In selection or condition statements comparisons can be tested using the following operators:
– = (Equals)
– > (Greater than)
Computer Science P-2 9 Unit-2: Algorithms
IF construct
CASE construct
(i) IF construct
There are different types or variations of IF construct/structures including:
Nested IF structure
1. IF structure without 'ELSE' clause
IF <condition> THEN
<statement>
ENDIF
Example: Using pseudocode, write an algorithm that takes a mark from a user and output
pass if the mark is above 60.
Input mark
If mark> 60 Then
Print "Pass"
End IF
Class Activity 4:
Write the pseudocode that take a mark from a user and output fail if the mark is below
40
Class Activity 5:
(i) Write the pseudocode to input an examination mark and output the grade awarded.
The maximum mark is 100. The table below shows the grades that correspond to the marks
gained.
Computer Science P-2 10 Unit-2: Algorithms
Mark Grade
0-59 U
60-69 E
70-74 D
75-79 C
80-84 B
85-89 A
90-100 A*
Example: Using pseudocode, write an algorithm that takes a mark from a user and output
pass if the mark is above 59 and fail if it is below 59
Input mark
If mark> 59 Then
Print "Pass"
Else
Print "fail"
End IF
Class Activity 6:
Write the pseudocode that take a temperature from a user and output high if the temperature
is above 40, else output low.
3. Nested IF structure
IF <condition> THEN
<Statement>
IF <condition> THEN
<Statement>
ELSE
< Statement >
END IF
< Statement >
ELSE
< Statement >
END IF
Computer Science P-2 11 Unit-2: Algorithms
This is called a nested IF structure. It has one IF statement nested (sitting inside) another.
Note: Nested selection
A nested structure (such as an IF statement) is contained inside another similar structure (e.g.
IF statement). This is emphasised by indenting the code to show how the statements are
nested.
Class Activity 7:
Which algorithm is the more efficient (If structure v/s nested IF structure)?
Example: Using pseudocode, write an algorithm take an age from a user, first check if the
age is valid (must be between 0 and 100), if so, print young if the age is below 40 or print
old if the age is above 39.
Input age
If age> 0 and age <=100
If age < 40 Then
Print "young"
Else
Print "old"
End If
End If
Computer Science P-2 12 Unit-2: Algorithms
Class Activity 8:
Write pseudocode for a program that takes a mark from a user. Firstly, check that the mark is
above 39 and if so, check if the mark is above 90 and below 101, then print "Excellent Mark",
else print "Pass Mark". If the mark is below 60, print "fail mark".
(ii) Case construct
Case construct is used when there are multiple options from which to choose.
There are different types or variations of Case structures including:
Example: Using pseudocode, write an algorithm (using case .. structure) that takes a
temperature, print "room temperature" if the temperature is from 1 to 37, prints "extreme
temperatures" if the temperature is from 38 to 100 or print "freezing temperatures" if
temperature is from -15 to o degrees Celsius.
Input temperature
Case of temperature
1 to 37: print "room temperature"
38 to 100: print "extreme temperatures"
-15 to 0: print "freezing temperatures"
End Case
Class Activity 9:
Write an algorithm that displays Grade E when student marks are 0 to 20, Grade D when
student marks are 21 to 35, Grade C when student marks are 36 to 45, Grade B when student
marks are 46 to 60, Grade A when student marks are 60+.
2. Case structure with 'otherwise'
CASE OF <identifier>
<value 1>: <statement>
<value 2>: <Statement>
OTHERWISE
<statement>
ENDCASE
Computer Science P-2 13 Unit-2: Algorithms
Example: -Using pseudocode, write an algorithm that displays Grade E when student marks
are 0 to 20, Grade D when student marks are 21 to 35, Grade C when student marks are 36 to
45, Grade B when student marks are 46 to 60, Grade A when student marks are 60+. 1f students
mark are not within the range, print Grade F (use case of ... otherwise structure)
Input grade
Case of grade
0 to 20: Display "Grade E"
21 to 35: Display "Grade D"
36 to 45: Display "Grade C"
46 to 60: Display "Grade "8"
60 +: Display "Grade A"
Otherwise:
Display "Grade F"
End Case
Pig - Oink
Cow- Moo
Bear - Grr
Sheep - Baa
Tiger - Grr
everything else – Meow
2.1.4 Iteration constructs
A loop is used when a block of program instructions is executed a finite number of times
according to some condition. Because the instructions inside the loop are repeated, these
loops are called iteration, or repetition, constructs.
There are 3 main types of iteration constructs/structures:
– For ... Next loop structure
– Repeat. .. Untilloop structure
– While ... End While structure
(i) For ... Next loop structure
This loop is used when you want to perform the sequence of instructions in a loop a number
of times. This construct take the following form:
FOR <identifier> <start value> TO <end value>
<statements>
NEXT<identifier>
ENDFOR
Computer Science P-2 14 Unit-2: Algorithms
Note: The for loop is a count controlled loop since we know the number of iterations initially
(e.g. for count = 1 to 10). The identifier is the variable that will increase or decrease
each time and it is known as the loop counter.
Example: Using pseudocode, write an algorithm (use for ...loop structure) that takes 10 numbers
from a user and output their total.
Total 0
For N = 1 to 10
Read number
Total Total + number
End For
Print Total
Example: Write an algorithm using repeat ... until structure that adds up a series of numbers
until the total exceeds 100.
Total 0
Repeat
Read number
Total Total + number
Until Total >100
Print Total
Computer Science P-2 15 Unit-2: Algorithms
Example: Using pseudocode, write an algorithm using while ...Ioop structure that will allow
a user to calculate area of a circle as long as the radius being entered is positive.
Pie 3.142
Input radius
While radius > 0
Area Pie * radius * radius
Output area
Input radius
End While
The process repeats for a number of students and terminates when input of the student name
END. The list of marks for each student is terminated by a negative mark. The program outputs
the average mark for the student.
Computer Science P-2 16 Unit-2: Algorithms
(b) Modify the algorithm so that whenever a value less than or equal to 0 is
entered for radius, the computer displays a message saying that "Error! Radius
must be greater than 0".
(8) Write an algorithm using pseudocode that accepts 10 temperatures reading in
Celsius, calculate and output the temperatures then in Fahrenheit. (Fahrenheit
= 32 + 9/5 * Celcius).
(9) Write an algorithm using pseudocode that outputs the smallest and largest
number when 10 numbers are input.
(10) Using Pseudocode, write an algorithm to print the even and odd numbers from
0 until 26.
Decision/quary box:
No Yes
The process box: x=x+1 Is X >
5?
Solution:
Tot C M Output
0 0
2 1 2
6 2 4
12 3 6
20 4 8
21 5 1
24 6 3
29 7 5
36 8 7
45 9 9
55 10 10 55
Note: (1) Notice that the test data 11 is not taken into consideration since the flowchart cater
for 10 values only.
(2) This flowchart makes use of a counter-controlled loop as shown by the arrow
which goes upwards. Counter-controlled loops are used when a flowchart need to
take input and processed a fixed number of items.
Class Activity 15:
(i) Design a flowchart to calculate area of a circle while radius is positive
(ii) Draw a trace table with test data: r = 1, 2, –3, 5, 6
2.2.5 Condition-controlled loop
In the flowchart below, we do not know in advance the number of times we wish the loop to
repeat. The flow of data in this case is interrupted by using a rogue value/data terminator. For
example here, the loop will repeat until the special data item (a rogue value is that is less 0) is
met and thus terminates the loop.
Computer Science P-2 20 Unit-2: Algorithms
Remarks:
Note: Please give your details if you want to be added in our Loyalty discount schemes
Name Facebook Id:
Address
City Country
Address