[go: up one dir, main page]

0% found this document useful (0 votes)
18 views32 pages

Paper 2 Notes

Uploaded by

areebhamid28
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)
18 views32 pages

Paper 2 Notes

Uploaded by

areebhamid28
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/ 32

Pseudocode

2.1Problem solving

The computer is created to solve problems, primarily mathematical problems. It is developed to


mimic the working of human brain. Human brain is amazing as it can perform lot of different tasks
quickly as well as simultaneously like controlling your heart rate, breathing etc. A computer being an
electronic device can work faster than a human brain however it cannot mimic the simultaneous
working nature of it. To let the computer solve tasks like a human brain, it first needs to be divided
into smaller steps and that’s what an algorithm is.

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

Within your sandwich algorithm you would need to specify:

 inputs - ingredients and quantities


 the process - recipe or method
 output - what the finished sandwich will be like

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:

Algorithms are used in all areas of computing. Examples include:

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

Algorithms are a great way of automating computer decisions.

Sir Bandeshah Sir Mym


Page # 1
0333-2076121 0332 3033505
Pseudocode

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:

1. Remove lid from butter tub and place to one side.


2. Place 5 grams of butter on the tip of the knife.
3. Position the tip of the knife on the upwards side of the slice of bread with the butter
between the knife and the bread.
4. Move the knife backwards and forwards in a sweeping motion across the bread to spread it
at an even thickness.
5. Repeat steps 2 to 4 until one side of the slice of bread is evenly coated with butter.
6. Remove any excess butter from the tip of the knife.

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.

Sir Bandeshah Sir Mym


Page # 2
0333-2076121 0332 3033505
Pseudocode

Overview Of Pseudocode Constructs

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.

Programs and Data

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 Bandeshah Sir Mym


Page # 3
0333-2076121 0332 3033505
Pseudocode

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: 72.84 , 0.014 , -12.85


Use: Mainly used for money, weight, height, temperature etc.

3. CHAR: Data type to hold a “single” alphanumeric character.

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.

Example: Google , CIE2016 , R@N#


Use: Mainly used for names, address etc.

Sir Bandeshah Sir Mym


Page # 4
0333-2076121 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.

VARIABLES: Declare variable_name AS data_type

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

CONSTANT: CONST constant_name 


Value AS datatype

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:

 CONST pi  3.141 AS Float


 CONST color  “Red” AS String

Note: Constant’s value is assigned directly in the declaration. Its value can be read but cannot be
changed later on.

Sir Bandeshah Sir Mym


Page # 5
0333-2076121 0332 3033505
Pseudocode

Arrays:

Declare array_name [1:max] AS datatype


Declare array_name [max] AS datatype

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

 Declare marks [1:10] AS Integer


 Declare father names [50] AS String

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:

Set Swapped to False

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]

Sir Bandeshah Sir Mym


Page # 6
0333-2076121 0332 3033505
Pseudocode

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

Decision making or branching is a way to direct the flow of


pseudocode depending upon a particular condition. We can
use these constructs to check for a condition and depending
upon the result, can either do something or not.
There are 2 constructs for decision making in pseudocode;
CASE-OF and IF-THEN.
IF-THEN have 2 variations; one which checks single condition
and the other which can check against a series of conditions.
Both have slight differences in their syntax and are explained
below.

IF statements

IF statements may or may not have an ELSE IF clause. IF


statements without an ELSE IF clause is written as follows:

IF<condition>THEN
<statements if true>
ENDIF

IF statements with an else clause is written as follows:

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.

Sir Bandeshah Sir Mym


Page # 7
0333-2076121 0332 3033505
Pseudocode

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

An OTHERWISE clause can be the last case:

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

Sir Bandeshah Sir Mym


Page # 8
0333-2076121 0332 3033505
Pseudocode

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.

Computer programs can use different types of loops:

 count-controlled loops - repeat a set amount of times


 condition-controlled loops – while/repeat until something happens

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

Sir Bandeshah Sir Mym


Page # 9
0333-2076121 0332 3033505
Pseudocode

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.

For example, to stop the robot from driving off the


edge of a table, you might write a WHILE loop like this:

Move forward WHILE I am not touching the table edge

Example:

WHILE weight > 100


DO
PRINT “You are overweight”
END WHILE

Differences between For-Next, While & Repeat-Until

FOR-NEXT WHILE REPEAT-UNTIL


Repeat statements fixed Repeats statements when Repeats statements when
number of times condition is true condition if false

No condition checking Checks condition at the start of Checks condition at the end of
loop loop

Sir Bandeshah Sir Mym


Page # 10
0333-2076121 0332 3033505
Pseudocode

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

Comparison operators allows us to compare different values.

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.

Sir Bandeshah Sir Mym


Page # 11
0333-2076121 0332 3033505
Pseudocode

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

Sir Bandeshah Sir Mym


0333-2076121 0332 3033505
Page # 12
Pseudocode

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

Sir Bandeshah Sir Mym


0333-2076121 0332 3033505
Page # 13
Pseudocode

Solving Questions Having Fixed Number of Inputs Using Repeat Until Looping Structure.

Format 3

Declaration

Initialization ( C 
1)

Repeat

Input

Totaling (total 
total + . . . . . . .)

Counting with decision (count count +1)


Output with Decision
Extreme values

C 
C+1

Until (C = N) Average 
total/N

Percentage
(count/N)*100 Output average

Output percentage

Q3

Q4

Sir Bandeshah Sir Mym


0333-2076121 0332 3033505
Page # 14
Pseudocode

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)

Format 2 (Condition Based)

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

Sir Bandeshah Sir Mym


0333-2076121 0332 3033505
Page # 15
Pseudocode

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

Q 7 Write an algorithm, using pseudo code or a flowchart, which

• inputs a set of positive numbers (which end with -1)


• outputs the average (mean) value of the input numbers outputs the value of the largest
• (highest) number input

Sir Bandeshah Sir Mym


0333-2076121 0332 3033505
Page # 16
Pseudocode

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

There are four errors in this code.

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

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 17
Pseudocode
There
e 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
put 1000 numbers into an array.

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………
……………………………………………………………………………………

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

b. Rewrite your algorithm using another loop structure.


………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………
………………………………………………………………………………………………

………………………………………………………………………………………………

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 18
Pseudocode
………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

……………………………………………………………………………………………[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]
……………………………………………………………………………………………

b. Rewrite the program code with your changes.


………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………
………………………………………………………………………………………………

………………………………………………………………………………………………

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 19
Pseudocode
………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

………………………………………………………………………………………………

……………………………………………………………………………………………[3]
……………………………………………………………………………………………

Q5 REPEAT…UNTIL is one type of loop structure.


Identify and describe two other types of loop structure that you could use when
writing pseudocode.

Loop structure 1………………………………………………………………………………

Description………………………………………………………………………………………

….…………………………………………………………………………
…………………………………………………………………………………
……………………

Loop structure 2 …………………………………………………………………….........


……………………………………………………………………..............

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.

a. Suggest three improvements that could be made.

1. ………………………………………………………………………………………………

………………………………………………………………………………………………
…………………………………………………………………………

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 20
Pseudocode
2. ………………………………………………………………………………………………

………………………………………………………………………………………………
…………………………………………………………………………

3. ………………………………………………………………………………………………

……………………………………………………………………………
……………………………………………………………………………………………[3]

b. Rewrite the program code with your improvements.


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

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

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

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

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

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

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

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

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

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

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

Data structure ………………………………………………………………………………

Reason…………………………………………………………………………………
…………………………………………………………………………………………

……………………………………………………………………………………………
……………………………………………………………………………………………… [2]

Q8 Identify two different selection statements


statements that you can use when writing
pseudocode.

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 21
Pseudocode
1. ………………………………………………………………………………………………

………………………………………………………………………………………………
…………………………………………………………………………

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

There are four errors in this code.

Locate these errors and suggest a correction to remove each error.

Error 1 …………………………………………………………………………………
Correction
n ………………………………………………………………………………
………………………………………………………………………………
…………………………………………………………………………………………...
……………………………………………………………………………...
Error 2 …………………………………………………………………………………
Correction ………………………………………………………………………………
…………………………………………………………………………………………..
…………………………………………………………………………………..
Error 3 …………………………………………………………………………………
Correction ………………………………………………………………………………
…………………………………………………………………………………………..
…………………………………………………………………………………..
Error 4 …………………………………………………………………………………

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 22
Pseudocode
Correction ………………………………………………………………………………
………………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………. [8]

Q 10 IF…THEN…ELSE…ENDIF and CASE…OF…OTHERWISE…ENDCASE are two


different conditional statements that you can use when writing pseudocode.
Explain, using examples, why you would choose to use each conditional
statement.

Example 1……………………………………………………………………………
……………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
……………………………………………................................................................
……………………………………………................................................................
.............................................................................................................................
.............................................................................................................................
Reason off choice..…………………………..…………………………………………
choice..…………………………..…………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………..............................
....................................................................................................
..........................................................................
.............................................................................................................................
.............................................................................................................................
.............................................................................................................................
.........................................................................................................................
.........................................................................................................................[6]

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 23
Pseudocode
Q 11 Read this section of program code that:
 Input 10 numbers
 Checks whether each number is within a specified range.
 Totals the numbers within 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
OutRange=OutRange
7. Count=Count+1
8. NEXT X
9. PRINT InRange, OutRange

a. There are four errors in this code.


Locate these errors and suggest a correction to remove each error.

Error 1 …………………………………………………………………………………
Correction
on …………………………………………………………………………
……………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………
Error 2 …………………………………………………………………………………
Correction
n ……………………………………………………………………………
………………………………………………………………………………………
…………………………………………………………………………………
……………………………………………………………………………………E
rror 3 …………………………………………………………………………………
Correction
n ……………………………………………………………………………
…………………………………………………………………………
…………………………………………………………………………………………..
Error 4 …………………………………………………………………………………
Correction……………………………………………………………………………
……………………………………………………………………………
……………………………………………………………………………………….
………………………………………………………………………………. [4]

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 24
Pseudocode
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]

Q 12 REPEAT …UNTIL and WHILE…Do…ENDWHILE are two different loop


structures you can use when writing pseudocode. (Oct/Nov2016
016)
Explain, using examples, why you would become choose to use each type of
loop.
Example 1………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………...
Reason for choice ……………………………………………………………….........
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………...
Example 2 ……………………………………………………………………...........
…………………………………………………………………………………………
……………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
Reason for choice …………………………………………………………………...
…………………………………………………………………………………………

…………………………………………………………………………………………

………………………………………………………………………………………..[6]

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 25
Pseudocode

(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

a. There are four errors in the code.


State the line number for each error and write the 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]

b. Describe the purpose of each statement in the algorithm.


FOR I  1 TO 300
INPUT Name[I]
NEXT I
…………………………………………………………………………………………
…………………………………………………………………………………………

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 26
Pseudocode
…………………………………………………………………………………………
………………………………………………………………………………………..[2]
c. Identify, using pseudocode, another loop structure that the algorithm in part
acould have used.

…………………………………………………………………………………………
…………………………………………………… ……………………………….. [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.

…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 27
Pseudocode
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………………[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
pseud and suggest a correction.

Error…………………………………………………………………………………..
Correction………………………………………………………………………
n………………………………………………………………………
n……………………………………………………………………………
……..……………………………………………………………………………….
………………………………………………………………………………. [2]
b. Rewrite the correct algorithm using a more suitable loop structure.
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 28
Pseudocode
…………………………………………………………………………………………
……………………………................................................................
……………………………..................................................................................[3]

(May/Jun 2018)
(May/June
Q 17

…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………………
…………………………………………………………………………………....[
…………………………………………………………………………………....[6]

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 29
Pseudocode
Q 18

Q 19

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 30
Pseudocode
Q 20

Q 21

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 31
Pseudocode
Q 22

Sir Bandeshah Sir Mym


0333-2076121 0332-3033505
3033505 Page # 32

You might also like