Professional Documents
Culture Documents
Paper 2 Notes
Paper 2 Notes
Paper 2 Notes
“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.
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.
Overview Of Pseudocode Constructs
Introduction to Pseudocode
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.
Sir
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.
Sir My
Page # 4
0332 3033505
Pseudocode
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.
Page # 5
Pseudocode
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]
Page # 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.
Page # 7
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
Page # 8
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:
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 Checks condition at the end
of loop of 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
CC+1
Output percentage
Q3
Q4
Q 5 Solve the Following Problem Using ‘Repeat Until’ Loop.
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
Format 3 (Condition Based)
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
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 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
There are four errors in this code.
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 1000 numbers into an array.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
…………………………………………………………………………………………….[2]
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
……………………………………………………………………………………………[4]
Q4 (M
Read this section of program code that
smallest number input.
/J
AYinputs UNE
10 numbers and then outputs the
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………………………………………………………………………………………
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. ………………………………………………………………………………………………
…………………… ………………………………………………………………………
2. ………………………………………………………………………………………………
…………………… ………………………………………………………………………
3. ………………………………………………………………………………………………
……………………………………………………………………………………………[3]
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
......................................................................................................................................
..................................................................................................................................[3]
Q7 A programmer writes a program to store a patient’s temperature every hour for a day.
State the data structure that would be most suitable to use and give the reason for your
choice.
Reason…………………………………………………………………………………………
……………………………………………………………………………………………… [2]
Q8 Identify two different selection statements that you can use when writing
pseudocode.
1. ………………………………………………………………………………………………
…………………… ………………………………………………………………………
2. ………………………………………………………………………………………………
…………………… ……………………………………………………………………[2]
Q9 (O /Nov 2016)
CTcode that inputs positive numbers discards any
Read this section of program
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 ………………………………………………………………………………
…………………………………………………………………………………………...
Error 2 …………………………………………………………………………………
Correction ………………………………………………………………………………
…………………………………………………………………………………………..
Error 3 …………………………………………………………………………………
Correction ………………………………………………………………………………
…………………………………………………………………………………………..
Error 4 …………………………………………………………………………………
Correction ………………………………………………………………………………
………………………………………………………………………………………. [8]
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 ……………………………………………………………………………
…………………………………………………………………………………………E
rror 3 …………………………………………………………………………………
Correction ……………………………………………………………………………
…………………………………………………………………………………………..
Error 4 …………………………………………………………………………………
Correction……………………………………………………………………………
………………………………………………………………………………………. [4]
b. Decide, with reasons, whether the numbers 10 and 20 are within or outside the
range.
Within Outside
Number
range range Reason
✓ ✓
……………………………………………………...………
10
………………………………………………….................
……………………………………………………………
20
…………...………………………………………………..
[4]
…………………………………………………………………………………………
………………………………………………………………………………………..[6]
(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’,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]
Q 15 Write an algorithm to input three different numbers, and then output the largest number. Use
eitherpseudocode or a flowchart.
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………………[4]
Q 16 An algorithm has been written in pseudocode to input 100 numbers and print out the
sum. A REPEAT…UNTIL loop has been used.
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 and suggest a correction.
Error…………………………………………………………………………………..
Correction……………………………………………………………………………
……..………………………………………………………………………………. [2]
b. Rewrite the correct algorithm using a more suitable loop structure.
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
……………………………..................................................................................[3]
Q 17
(MAY/JUNE
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………....[6]
Q 18
Q 19
Q 20
Q 21