Algorithm Design and Pseudocode
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
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
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
OR
flowchart.
WORD/PHRASE DESCRIPTION
initialization/declaration
user/keyboard
screen
condition
ENDCASE values/variables/conditions
times.
remains TRUE
remains FALSE
Syntax
IF <condition> THEN
ELSE
ENDIF
Example: Algorithm to check whether the input value is greater than100 and
SET number to 0
INPUT number
ELSE
ENDIF END
JAGRUT AMIN [ 9898 206804 ] Page 4
IF…THEN…ELSEIF…ELSE…ENDIF
Each ELSEIF works like IF, but must be used within the main structure of
IF…ENDIF.
If any of the ELSEIF’s condition is met, the rest of the ELSEIFs are not
checked
Syntax
IF <condition> THEN
ELSE
ENDIF
Example: Algorithm to generate a marks sheet and check the grading scheme
A+ 90 +
A 80 – 89.99
B 70 – 79.99
C 60 – 69.99
D 50 – 59.99
SET gr to “ ”
Gr = “A+”
Gr = “B”
Gr = “C”
Gr = “D”
ELSE
Gr = “U”
ENDIF
END
Syntax
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
JAGRUT AMIN [ 9898 206804 ] Page 7
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
OTHERWISE
ENDCASE
EXAMPLE (NOV 2003 – Q11): The following algorithm inputs air speeds (which
1. input a speed
2. whole = speed/100
3. case whole of
5. 3, 4, 5, 6 : result = normal
6. 7, 8, 9 : result = high
7. otherwise whole = -1
8. endcase
9. if whole = -1 then
FOR … TO … NEXT
Syntax
<statement>
<statement>
<statement>
NEXT
SET a to 0
FOR a = 1 TO 10
OUTPUT a
NEXT
END
FOR num = 1 To 10
NEXT
OUTPUT sum
END
Task: The following algorithm inputs 20 numbers and outputs how many numbers
were positive (> 0) and how many numbers were negative (< 0).
Algorithm (Corrected):
2 positive = 0
3 FOR count = 1 TO 20
4 input number
8 NEXT count
WHILE … DO … ENDWHILE
It is a conditional loop.
WHILE <condition> DO
<statement>
<statement>
<statement>
SET a to 0
WHILE a < 10 DO
a=a+1
Output a
ENDWHILE
END
Program code that inputs 30 positive numbers and then output the largest number
input.
Algorithm (Corrected):
1 Large = 0
2 Counter = 0
4 DO
5 INPUT Num
7 Counter = Counter + 1
8 ENDWHILE
9 PRINT Large
REPEAT
<statement>
<statement>
<statement>
UNTIL <condition>
SET a to 0
REPAT
a=a+1
Output a
UNTIL a = 10
END
Algorithm: To take two numbers from user and output the sum.
INPUT num1
INPUT num2
OUTPUT tot
END
TOTALING
To add up a series numbers the following type of statement
should be used:
number
INPUT num
pos = pos + 1
ELSE
neg = neg + 1
ENDIF
END
and then outputs how many were negative, how many were
1 negative = 1: positive = 1
2 for x = 0 to 1000
3 input number
6 endif
7 endif
8 next
for 500 days and outputs the average rainfall and the highest
rainfall input.
1 highest = 1000
2 days = 1
4 input rainfall
6 endif
8 days = days + 1
9 average = total/500
10 endwhile
1 input n
2 for mult = 1 to n
3 sum = 0
5 result = n/sum
6 next
7 print result