Algorithm Design and Pseudocode
Algorithm
An algorithm is a precise, step-by-step set of instructions for solving a task. An
algorithm does not solve a task; it gives you a series of steps that, if executed
correctly, will result in a solution to a task.
Algorithm is written in two ways:
Pseudocode
Flowchart
Pseudocode:
Pseudocode is a simple way of writing programming code in English. Pseudocode is
not actual programming language. It uses short phrases to write code for
programs before you actually create it in a specific language.
Flowchart:
A flowchart shows diagrammatically the steps required for a task (sub-system)
and the order that they are to be performed. These steps together with the
order are called an ALGORITHM. Flowcharts are an effective way to communicate
the algorithm that shows how a system or sub-system works.
OR
JAGRUT AMIN [ 9898 206804 ] Page 1
A block diagrammatic representation of a solution or flow of program is called a
flowchart.
Examples of pseudocode assignments
ASSIGNMENT OF VALUE IN DESCRIPTION
VARIABLE
Cost ← 10 Cost has the value 10
Price ← Cost * 2 Price has the value 20
Tax ← Price * 0.12 Tax has the value 2.4
SellingPrice ← Price + Tax SellingPrice has the value 22.4
Gender ←"M" Gender has the value M
Chosen ← False Chosen has the value False
Phrases/words used in Pseudocode:
WORD/PHRASE DESCRIPTION
SET To set/store values/variables –
initialization/declaration
READ/INPUT To input/get data from
user/keyboard
PRINT/OUPUT To output/return data/value on
screen
JAGRUT AMIN [ 9898 206804 ] Page 2
END To show the end of algorithm
IF … THEN … ELSE … ENDIF Selection – to check some
condition
CASE … OF … OTHERWISE … Selection – to match multiple
ENDCASE values/variables/conditions
FOR….TO….NEXT Loop – (Unconditional) to repeat
some task(s) a specific number of
times.
WHILE…DO…ENDWHILE Loop – (Conditional) to repeat
some task(s) till a condition
remains TRUE
REPEAT…UNTIL Loop – (Conditional) to repeat
some task(s) till a condition
remains FALSE
Selection is the second technique of Algorithm.
Selection is written using two structures:
IF … THEN … ELSE … ENDIF
CASE … OF … OTHERWISE … ENDCASE
JAGRUT AMIN [ 9898 206804 ] Page 3
IF … THEN … ELSE … ENDIF
It is used to check only one condition and act accordingly.
Syntax
IF <condition> THEN
<statement if condition is TRUE>
ELSE
<statement if condition is FALSE>
ENDIF
Example: Algorithm to check whether the input value is greater than100 and
output the message accordingly.
SET number to 0
INPUT number
IF number > 100 THEN
OUTPUT “The entered number is greater than 100”
ELSE
OUTPUT “The entered number is smaller than 100”
ENDIF END
JAGRUT AMIN [ 9898 206804 ] Page 4
IF…THEN…ELSEIF…ELSE…ENDIF
This is used to check multiple conditions.
Each ELSEIF works like IF, but must be used within the main structure of
IF…ENDIF.
There is no limit of ELSEIFs
If any of the ELSEIF’s condition is met, the rest of the ELSEIFs are not
checked
Syntax
IF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSE
JAGRUT AMIN [ 9898 206804 ] Page 5
<statement if condition is FALSE>
ENDIF
Example: Algorithm to generate a marks sheet and check the grading scheme
based on the following criteria:
Grades Percentage range
A+ 90 +
A 80 – 89.99
B 70 – 79.99
C 60 – 69.99
D 50 – 59.99
U < 50 (less than 50)
SET eng, comp, isl, math, phy, chem, tot, per to 0
SET gr to “ ”
INPUT eng, comp, isl, math, phy, chem
Tot = eng + comp + isl + math + phy + chem
Per = (Tot/375)* 100
IF per >= 90THEN
Gr = “A+”
ELSEIF per >= 80 AND per < 90 THEN
JAGRUT AMIN [ 9898 206804 ] Page 6
Gr = “A”
ELSEIF per >= 70 AND per < 80 THEN
Gr = “B”
ELSEIF per >= 60 AND per < 70 THEN
Gr = “C”
ELSEIF per >= 50 AND per < 60 THEN
Gr = “D”
ELSE
Gr = “U”
ENDIF
OUTPUT tot, per, gr
END
CASE … OF … OTHERWISE … ENDCASE
CASE is used to match multiple options in one condition.
Syntax
CASE <identifier (variable) OF
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
JAGRUT AMIN [ 9898 206804 ] Page 7
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
OTHERWISE
<constant> : <statement if identifier/variable is not matched>
ENDCASE
EXAMPLE (NOV 2003 – Q11): The following algorithm inputs air speeds (which
must be in multiples of 100) and outputs a suitable message.
1. input a speed
2. whole = speed/100
3. case whole of
4. 0,1,2 : result = slow
5. 3, 4, 5, 6 : result = normal
6. 7, 8, 9 : result = high
7. otherwise whole = -1
8. endcase
9. if whole = -1 then
10. output “abnormal reading”
11. else output result, “speed”
JAGRUT AMIN [ 9898 206804 ] Page 8
LOOP
Loop: Repetition of task is called loop/iteration.
There are two basic types of loops:
Unconditional Loop (FOR – TO – NEXT)
Conditional Loop (WHILE – DO – ENDWHILE) and (REPEAT – UNTIL)
Unconditional Loop: Unconditional loop repeats the tasks unconditionally the
specified number of times.
FOR … TO … NEXT
Syntax
FOR <identifier(variable> = <number> TO <number>
<statement>
<statement>
<statement>
NEXT
JAGRUT AMIN [ 9898 206804 ] Page 9
Algorithm to output 1 to 10 no.
SET a to 0
FOR a = 1 TO 10
OUTPUT a
NEXT
END
Example 2: Algorithm to output sum of first 10 natural numbers.
SET num, sum to 0
FOR num = 1 To 10
sum = sum + num
NEXT
OUTPUT sum
END
Example 3: (November 2010, Paper 12 – Q9)
Task: The following algorithm inputs 20 numbers and outputs how many numbers
were positive (> 0) and how many numbers were negative (< 0).
Algorithm (Corrected):
JAGRUT AMIN [ 9898 206804 ] Page 10
1 negative = 0
2 positive = 0
3 FOR count = 1 TO 20
4 input number
5 if number < 0 then negative = negative + 1
6 if number > 0 then positive = positive + 1
8 NEXT count
9 print negative, positive
WHILE … DO … ENDWHILE
It is a conditional loop.
Loop continues till the condition remains TRUE.
Condition is check first and the process comes next to it.
WHILE <condition> DO
<statement>
<statement>
<statement>
<identifier> = <identifier> + <number>
JAGRUT AMIN [ 9898 206804 ] Page 11
ENDWHILE
Algorithm to output numbers from 1- 10.
SET a to 0
WHILE a < 10 DO
a=a+1
Output a
ENDWHILE
END
Example: June 2015, Paper 22 – Q2
Program code that inputs 30 positive numbers and then output the largest number
input.
Algorithm (Corrected):
1 Large = 0
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
JAGRUT AMIN [ 9898 206804 ] Page 12
REPEAT… UNTIL
It is a conditional loop.
Loop continues till the condition remains FALSE.
The process comes first, condition is checked next.
REPEAT
<statement>
<statement>
<statement>
<identifier> = <identifier> + <number>
UNTIL <condition>
Algorithm to output numbers from 1- 10.
SET a to 0
REPAT
a=a+1
Output a
UNTIL a = 10
END
INPUT and OUTPUT (e.g. READ and PRINT)
JAGRUT AMIN [ 9898 206804 ] Page 13
INPUT/READ
This command is used in pseudocode to accept/collect/get
data value from user/keyboard entry.
Algorithm: To take two numbers from user and output the sum.
SET num1, num2, tot to 0
INPUT num1
INPUT num2
tot = num1 + num2
OUTPUT tot
END
TOTALING
To add up a series numbers the following type of statement
should be used:
total = total + number
This literally means (new) total = (old) total + value of
number
Example: Algorithm to count total number of positive and
negative entries in 100 inputs.
SET num, pos, neg to 0
JAGRUT AMIN [ 9898 206804 ] Page 14
FOR a = 1 TO 100
INPUT num
IF num >= 0 THEN
pos = pos + 1
ELSE
neg = neg + 1
ENDIF
OUPUT pos, neg
END
(1) The following section of pseudocode inputs 1000 numbers
and then outputs how many were negative, how many were
positive and how many were zero.
Locate the 3 errors and suggest a corrected piece of code.
1 negative = 1: positive = 1
2 for x = 0 to 1000
3 input number
4 if number < 0 then negative = negative + 1
5 if number > 0 then positive = positive + 1
6 endif
7 endif
8 next
JAGRUT AMIN [ 9898 206804 ] Page 15
9 print negative, positive
(2) The following section of pseudocode inputs rainfall (in cm)
for 500 days and outputs the average rainfall and the highest
rainfall input.
Locate the 3 errors and suggest a corrected piece of code.
1 highest = 1000
2 days = 1
3 while days > 0
4 input rainfall
5 if rainfall > highest then highest = rainfall
6 endif
7 total = total + rainfall
8 days = days + 1
9 average = total/500
10 endwhile
11 print average, highest
(3) The following section of pseudocode inputs a number, n,
multiplies together 1 x 2 x 3 x ……. x n, calculates input
number/sum and outputs result of the calculation.
JAGRUT AMIN [ 9898 206804 ] Page 16
Locate the 3 errors and suggest a corrected piece of code.
1 input n
2 for mult = 1 to n
3 sum = 0
4 sum = sum * mult
5 result = n/sum
6 next
7 print result
JAGRUT AMIN [ 9898 206804 ] Page 17