Paper 2 Notes
Paper 2 Notes
2.1Problem solving
“Algorithms are sets of step-by-step instructions for the computer to follow. They are at the heart of
all computer programs.”
Let’s take an example of a real-life problem and see how we can break it into a series of simple
steps:
You can think of an algorithm as similar to a food recipe. If you make a sandwich, you follow a set of
steps to put the different ingredients together. You bring ingredients together, assemble them as
you like, and produce a final product - the sandwich. If you were asked to write down instructions to
make a sandwich, you could create a written algorithm.
A problem can have many possible solutions and each will be considered correct until you are
getting the right result i.e. in the above example; A single problem can have many possible solutions
i.e. . Algorithms.
Use of Algorithms:
PageRank - Google's search engine uses a highly effective algorithm called PageRank to find
the best matches for search terms.
Weather forecasting - the Met Office uses weather forecasting algorithms to model weather
patterns and make predictions.
Designing an Algorithm
When designing an algorithm, you need to assess how complex it could be. With a food recipe, a
simple command like 'spread butter on bread' could be made much more detailed. For example:
Algorithms have values that can be constant or variable. These values are stored in a memory
location and can be changed, depending on the output that is needed. For example, the main
content for a sandwich could be cheese or jam. In this case 'main ingredient' would be a value which
is variable. You could require one sandwich or 57 sandwiches. This would be another variable.
Introduction to Pseudocode
No specific programming language is referred to; development of algorithms using pseudocode uses
generic descriptions of looping, branching, data manipulation, input/output, totaling and counting
techniques.
The main purpose of computer programs is to collect and process data. A computer can change what
it does depending on the data that it has to work with. The data that is used in a program is referred
to as the data values.
Data Types
Following the common data types that are used in pseudocode when declaring variables, constants
and arrays. Some data types can be referred with more than one name; alternative names are also
listed.
1. INTEGER: Data type to hold whole numbers i.e. numbers without decimal value. It contains both
positive and negative numbers
Example: 56 , 27 , -25
Use: Mainly used for counting, whole number addition/subtraction etc.
2. FLOAT / REAL: Data type to hold floating point numbers i.e. numbers with decimal value. It
contains both positive and negative decimal numbers
Example: R , K , s
Use: Mainly used for codes like T or F (True/False), M or F(male/female) etc.
4. STRING: Data type to hold alphanumeric characters. It includes all printable characters including
alphabets, numbers, special symbols and punctuation marks.
Declaration
Declaration is a way to tell the computer that you are going to use a particular variable so it should
reserve specific memory to it as well as label it with the name that you give in the declaration. It is
required for variables, constants and arrays and is mostly done at the start of pseudocode, however
it is not compulsory and we can declare them anywhere in our pseudocode but beware that
declaration should always be before the use of variable, constant or array.
Where:
variable_name is the name of variable. It can be anything but should be meaningful and signify the
purpose. Variable names should start with an alphabet. 4num is not a valid variable name since it
starts with a number.
data_type is the data type of variable. It could be integer, float, char or string.
Example
Declare student name AS String
Declare height AS Float
Where:
constant_name is the name of constant. It can be anything but should be meaningful and signify the
purpose. Constant names should start with an alphabet. 4num is not a valid variable name since it
starts with a number.
data_type is the data type of variable. It could be integer, float, char or string. Value is the fixed
value assigned to variable which will not be changed afterwards in pseudocode or flowchart
Example:
Note: Constant’s value is assigned directly in the declaration. Its value can be read but cannot be
changed later on.
Arrays:
Where:
array_name is the name of array. It can be anything but should be meaningful and signify the
purpose. Array names should start with an alphabet. 4num is not a valid array name since it starts
with a number.
datatype is the data type of array. It could be integer, float, char or string. max is the size of an array.
It is the number of elements in an array.
Example
Note: An array’s index number cannot be a floating point value nor it can be negative value. It can
only be a positive integer value.
ASSIGNMENT:
Assignment is the process of writing a value into a variable (a named memory location).
For example, Count ← 1 can be read as “Count is assigned the value 1‟, “Count is made equal to 1‟
or “Count becomes 1‟. Another way of indicating assignment is a pseudocode statement such as:
Initialization:
If an algorithm needs to read the value of a variable before it assigns input data or a calculated value
to the variable, the algorithm should assign an appropriate initial value to the variable, known as
Initialization.
INPUT/ OUTPUT
We indicate input by words such as INPUT, READ or ENTER, followed by the name of a variable to
which we wish to assign the input value.
Example:
INPUT name
INPUT name, father_name, age
Input class [6]
We indicate output by words such as OUTPUT, WRITE or PRINT, followed by a comma separated list
of expressions.
Example:
PRINT name
PRINT class [6]
PRINT name, father_name, age
DECISION MAKING
IF statements
IF<condition>THEN
<statements if true>
ENDIF
IF <condition>THEN
<statements if true>
ELSE IF
<statements if false>
ENDIF
Example:
IF Number>Largest THEN
Largest
Number
ENDIF
Note: THEN and ELSE clauses are only indented by two spaces. (They are, in a sense, a continuation
of the IF statement rather than separate statements). When IF statements are nested, the nesting
should continue the indentation of two spaces. In particular, run-on THENIF and ELSE IF lines should
be avoided.
CASE Statements:
CASE statements allow one out of several branches of code to be executed, depending on the value
of a variable. CASE statements are written as follows:
CASE OF<identifier>
<value 1> : <statement>
<value 2> : <statement>
...
ENDCASE
CASE OF <identifier>
<value 1> : <statement>
<value 2> : <statement>
...
OTHERWISE<statement>
ENDCASE
Example:
INPUT ItemType
CASE OF ItemType
CD CD + 1
DVD DVD + 1
Video Video + 1
Book Book + 1
OTHERWISE : Beep
ENDCASE
LOOPING/ ITERATION
Looping or iteration is the process of repeating sections of a program depending upon a particular
condition (condition based) to achieve a particular target or goal.
Count-Controlled Loop
Count-controlled loops are used to make a computer do the same thing a specific number of times.
The count-controlled loop can be described as a FOR loop. The program repeats the action FOR a
number of times.
Example:
FOR counting 1 to 75
PRINT “This will be printed 75 times”
NEXT counting
Condition-Controlled Loops
A program could be made more intelligent by programming it to avoid hazards. For example, if the
robot vehicle is 3 cm from the edge of the table and you tell it to move forwards 5 cm, it will drive
off the edge of the table. To stop this from happening, you might write a condition-controlled loop
like this:
move forward
repeat until
(touching table
edge)
Condition-
controlled loops
can be used to add
a high degree of
intelligence to a
computer system.
Example:
REPEAT
PRINT “You are
overweight”
UNTIL weight < 120
While Loop
Condition-controlled loops are also called WHILE loops or WHILE-ENDWHILE statements. A WHILE
loop code is repeated based on a certain condition. The condition could be 'true' or 'false'. The
WHILE loop executes while a condition is true. Whether the condition is met or not is checked at the
beginning of the loop. If the condition is 'true' it repeats, if not then the code is not executed.
Example:
No condition checking Checks condition at the start of Checks condition at the end of
loop loop
Operators
Following are the basic arithmetic operators that allow us to perform different types of
mathematical calculations in our flowchart/pseudocode.
Arithmetic operators
OPERATOR MEANING
+ Addition
- Subtraction
* Multiplication
/ Division
mod Remainder
← Assignment operator. Used to assign values to
variables/constant/array.
Care should be taken with the division operation: the resulting value should be of data type REAL,
even if the operands are integers.
The integer division operators MOD and DIV can be used. However, their use should be explained
explicitly and not assumed. INT function is also used in algorithm.
Logical Operators
OPERATOR MEANING
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
The only logic operators (also called relational operators) used are AND, OR and NOT. The operands
and results of these operations are always of data type BOOLEAN.
FORMAT 1 To Solve Problems Having Fixed Iterations (loops) using FOR NEXT Loop.
Hi Guys, the whole idea about learning ‘The Formats’ is that so students can look into problem
solving questions and correlate it with the correct format. Students can then have a clear picture
that where to INPUT, where to close the loop (Put NEXT, End While or Until) and where to calculate
Average, OUTPUT extreme values variable etc.
Format 1
Declaration
Initialization
For count = 1 to N
Input
Counting with Decision (count2 = count2 +1)
Output with Decision
Extreme values
Totaling (total = total + . . . . . . .)
Next
Average = total/N
Percentage = (count2/N)*100
Output average
Output percentage
>>>>>>>>>>>>>>>>>>>>>>>>>>>
Q 1 Write an algorithm, using pseudo code, which inputs the speed for 500 cars, and then outputs:
• the final speed for ALL 500 cars
• the slowest (lowest) final speed
• the fastest (highest) final speed
• the average final speed for all the cars
A school is doing a check on the heights and weights of all its students. The school has 1000 students. Write an
algorithm, using pseudo code, which
• inputs the height and weight of all 1000 students
• outputs the average (mean) height and weight
Q2
(a) Write an algorithm, using pseudo code, which:
• inputs 50 numbers
• outputs how many of the numbers were > 100
(b) Write an algorithm, using pseudo code, which:
• inputs 100 numbers
• finds the average of the input numbers Outputs the average
We already know that the Format 1 is for the problems which need to be iterated (looped) for some
definite number of times, and we can simply figure that out by looking at the problem. Now, what if
we are told to do the same problems with other two remaining kind of loops (‘WHILE ENDWHILE’ &
‘REPEAT UNTILL’) instead of using (‘FOR NEXT’) loop. Firstly, as we have done in Format 1. We’ll
revise ‘Format 2’ and ‘Format 3’.
Solving Problems having fix number of inputs but using While – EndWhile Looping Structure.
Format 2
Declaration
Initialization (C 1)
While ( C <= N )
Input
Totaling (total total + . . . . . . .)
Counting with decision (count count +1)
Output with Decision
Extreme values
C C + 1
Endwhile
Average total/N
Percentage (C1/C2)*100
Output average
Output percentage
Solving Questions Having Fixed Number of Inputs Using Repeat Until Looping Structure.
Format 3
Declaration
Initialization ( C
1)
Repeat
Input
Totaling (total
total + . . . . . . .)
C
C+1
Until (C = N) Average
total/N
Percentage
(count/N)*100 Output average
Output percentage
Q3
Q4
A small airport handles 400 flights per day from three airlines: FASTAIR (code FA)
SWIFTJET
(code SJ)
KNIGHTAIR
(code KA)
Each flight is identified by the airline code and 3 digits. For example FA 156.
Write an algorithm, using pseudocode or otherwise, which monitors the 400 flights into and out of
the airport each day. The following inputs, processing and outputs are all part of the monitoring
process:
• input flight identification
• calculate number of flights per day for each of the three airlines
• output the percentage of the total flights per day by each airline Any validation checks must be
included
Hint [input flight code and flight number separately, for example, (Input Flight_code, Input
Flight_Number), and then apply check on flight codes to count number of flights for each airlines.
For example ( if Flight_code = ‘FA’ then count_FA = count_FA + 1)
Declaration
Initialization
Input Var
While (Var <> Condition)
Totaling (total <- total + . . . . . . .)
Counting with decision (count count +1)
Output with Decision
Extreme values
C C + 1 ( counting the number of iterations as we don’t know the value of N here )
Input Var
EndWhile
Average <- total/C
Percentage <- (C1/C)*100
Output average
Output percentage
Declaration
Initialization
Repeat
Totaling (total <- total + . . . . . . .)
Counting with decision (count count +1)
Output with Decision
Extreme values
C C + 1 ( counting the number of iterations as we don’t know the value of N here )
Input Var
Until (Var = condition)
Average <- total/C
Percentage <- (C1/C)*100
Output average
Output percentage
Now, solve these Problems given below all by yourself with the Help of Format 2 (Condition Based).
Q6
PSEUDOCODE
(May/Jun 2015)
(May/June
Q1 Read this section of program code that should input 10 positive numbers and
then output the smallest number input.
1. Small = 0
2. Counter = 0
3. REPEAT
4. INPUT Num
5. IF Num < small THEN Num = small
6. Counter = Counter + 1
7. PRINT Small
8. UNTIL Counter < 10
Locate these errors and suggest a corrected piece of code for each error.
1. ………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
2. ………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
3. ………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
4. ………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………[4]
Q2 Read this section of program code that
that should input 30 positive numbers and
then output the largest number input.
1. Large = 9999
2. Counter = 0
3. WHILE Counter > 30
4. DO
5. INPUT Num
6. IF Num < Large THEN Large = Num
7. Counter = Counter - 1
8. ENDWHILE
9. PRINT Large
Locate these errors and suggest a correct piece of code for each error.
1. ………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
2. ………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
3. ………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
4. ………………………………………………………………………………………………
……………………………………………………………………………………………
…………………………………………………………………………………
…………………………………………………………………………………[4]
Q3
a. Write an algorithm, using pseudocode and a FOR … TO … NEXT loop
structure, to input
put 1000 numbers into an array.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………
…………………………………………………………………………………………….[2]
……………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………[4]
……………………………………………………………………………………………
(May/Jun 2016)
(May/June
Q4 Read this section of program code that inputs 10 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
a. Identify three changes you would need to make to find the largest number
input instead of the smallest number.
1. ………………………………………………………………………………………………
………………………………………………………………………………………………
2. ………………………………………………………………………………………………
……………………………………………………………………………………………
………………………………………………………………………
…………………………………………………………………………
3. ………………………………………………………………………………………………
………………………………………………………………………… …………………[3]
……………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………[3]
……………………………………………………………………………………………
Description………………………………………………………………………………………
….…………………………………………………………………………
…………………………………………………………………………………
……………………
Description………………………………………………………………………………………
…..………………………………………………………………………………………
……………………………………………………………………
………………………………………………………………………… [4]
Q6 Read this section of program code that inputs 10 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 = 10
This code works, but it is inefficient.
1. ………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………
3. ………………………………………………………………………………………………
……………………………………………………………………………
……………………………………………………………………………………………[3]
.............................................................................................................................
......................................................................................................................................
......................................................................................................................................
.....................................................................................................................
.............................................................................................................................
......................................................................................................................................
..................................................................................................................................
.............................................................................................................................
.................................................................................................................................
.............................................................................................................................
......................................................................................................................................
.............................................................................................................................
......................................................................................................................................
...................................................................................................................
......................................................................................................................................
......................................................................................................................................
................................................................................................................
......................................................................................................................................
......................................................................................................................................
..................................................................................................................................[3]
............................................................................................................
Reason…………………………………………………………………………………
…………………………………………………………………………………………
……………………………………………………………………………………………
……………………………………………………………………………………………… [2]
………………………………………………………………………………………………
…………………………………………………………………………
2. ………………………………………………………………………………………………
………………………
………………………………………………………………………
……………………………………………………………………[2]
(Oct/Nov
Oct/Nov 2016)
2016
Q9 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
Error 1 …………………………………………………………………………………
Correction
n ………………………………………………………………………………
………………………………………………………………………………
…………………………………………………………………………………………...
……………………………………………………………………………...
Error 2 …………………………………………………………………………………
Correction ………………………………………………………………………………
…………………………………………………………………………………………..
…………………………………………………………………………………..
Error 3 …………………………………………………………………………………
Correction ………………………………………………………………………………
…………………………………………………………………………………………..
…………………………………………………………………………………..
Error 4 …………………………………………………………………………………
Example 1……………………………………………………………………………
……………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
……………………………………………................................................................
……………………………………………................................................................
.............................................................................................................................
.............................................................................................................................
Reason off choice..…………………………..…………………………………………
choice..…………………………..…………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………..............................
....................................................................................................
..........................................................................
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
.........................................................................................................................
.........................................................................................................................[6]
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
OutRange=OutRange
7. Count=Count+1
8. NEXT X
9. PRINT InRange, OutRange
Error 1 …………………………………………………………………………………
Correction
on …………………………………………………………………………
……………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………
Error 2 …………………………………………………………………………………
Correction
n ……………………………………………………………………………
………………………………………………………………………………………
…………………………………………………………………………………
……………………………………………………………………………………E
rror 3 …………………………………………………………………………………
Correction
n ……………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………………………..
Error 4 …………………………………………………………………………………
Correction……………………………………………………………………………
……………………………………………………………………………
……………………………………………………………………………………….
………………………………………………………………………………. [4]
Within Outside
Number
range range Reason
……………………………………………………
……………………………………………………...………
10
………………………………………………….................
……………….................
………………………………………………………
……………………………………………………………
20
…………...………………………………………
………………………………………………..
[4]
…………………………………………………………………………………………
………………………………………………………………………………………..[6]
(May/Jun 2017)
(May/June
Q 13 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 = 0
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
is’,Average
…………………………………………………………………………………………
…………………………………………………… ……………………………….. [1]
……………………………………………………...………………………………..
Q 14 Write an algorithm, using pseudocode, to input a number between 0 to 100
inclusive. The algorithm should prompt for the input and output error message
if the number is outside this range.
…………………………………………………………………………………………
……………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………………[3]
………………………………………………………………………………………[3]
Q 15 Write an algorithm to input three different numbers, and then output the largest
number. Use eitherpseudocode
pseudocode or a flowchart.
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
1. Count 0
2. Sum 0
3. REPEAT
4. INPUT Number
5. Sum Sum + Number
6. Count Count + 1
7. UNTIL Count > 100
8. PRINT sum
a. Find the error in the pseudocode
pseud and suggest a correction.
Error…………………………………………………………………………………..
Correction………………………………………………………………………
n………………………………………………………………………
n……………………………………………………………………………
……..……………………………………………………………………………….
………………………………………………………………………………. [2]
b. Rewrite the correct algorithm using a more suitable loop structure.
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
(May/Jun 2018)
(May/June
Q 17
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………....[
…………………………………………………………………………………....[6]
Q 19
Q 21