Tuesday, September
16, 2025
ALGORITHM DESIGN
AND
PROBLEM-SOLVING
DEFINITION OF ALGORITHM
PURPOSE OF ALGORITHM
FLOW CHARTS
Algorithm-Definition
• Algorithm- refers to a finite (limited) number of
instructions designed to give a solution to a
problem. For example, steps to find a common
denominator.
• Algorithms have a definite beginning and definite
end.
• An Algorithm will produce the same output
information given the same input information.
• Algorithms can be illustrated using Flowcharts,
Purpose of Algorithms
• Algorithm is a blue print of a program which gives
all the details and functionality involved in finding
the solution to a problem. It is important as we
can build a program on any platform with the help
of an algorithm.
• Algorithms are useful as they assist programmers
[Link] find the shortest path from one point to
another.
[Link] implement efficient, reliable and fast code in a
shorter amount of time
[Link] optimise the written program.
Flow Charts
• It is a graphical representation of an algorithm,
workflow or process, showing the steps as boxes various
kinds, and their order connected by arrows.
• It gives details on how programs and procedures are
executed.
ADVANTAGES OF FLOW CHARTS
• Communication: Flowcharts are a better way of
communicating the logic of a system to all concerned.
• Effective analysis: With the help of a flowchart, problem
can be analysed in a more effective way.
• Proper documentation: Program flowcharts
serve as a good program documentation, which
is needed for various purposes.
• Efficient Coding: The flowcharts act as a guide
or blueprint during the systems analysis and
program development phase.
• Proper Debugging: The flowchart helps in
debugging process.
• Efficient Program Maintenance: The
maintenance of operating program becomes
easy with the help of flowchart. It helps the
programmer to put efforts more efficiently on
LIMITATIONS OF USING FLOWCHARTS
• Complex logic: Sometimes, the program logic is quite
complicated. In that case, flowchart becomes complex and clumsy.
• Alterations and Modifications: If alterations are required, the
flowchart may require re-drawing completely.
• Reproduction: As the flowchart symbols cannot be typed,
reproduction of flowchart becomes a problem.
• The essentials of what is done can easily be lost in the technical
details of how it is done.
SYMBOLS OF
FLOWCHARTS
SYMBOL DESCRIPTION
This works as either a terminator or a starting point.
Therefore it is written either Start/Begin/Stop/End.
Denotes the flow of information from one location or
process symbol to the next symbol.
Parallelogram in shape. Represent input or output of
data.
Shows the decision box where action is taken after the
condition is taken after the condition is tested.
It is a process symbol that represents task execution.
Circular in shape. Denotes the start and end of a
subroutine. Nothing should be written inside it.
Indicates a module/subprogram/procedure inside another
program
Pseudo codes
• These are English-like statements, closer to
programming language that indicate steps followed in
performing a specific task.
• They are however independent of any programming
language. An example is as follows:
Start
Enter centigrade temperature, C
If C = 0, then stop.
Set F to 32 + (9C/5)
Print C and F
End
• A variable is a memory location that can store a value that can change during
program execution.
• A Constant is a memory location that can store a value that does not change
during program execution.
• Naming variables and Constants: Each programming language has its own
way of naming variables. However, the following conventions are common:
• - a variable should not be a reserved word. A reserved word is a word with a
specific meaning / function in that programming language, e.g. Print, else, are
reserved words in BASIC
• - Variables must start with an alphabetic character, not with digit.
• - It is wise to name a variable using the data it stores, e.g. surname (to store
surnames), DOB (to store a date of birth), etc. Thus it must be meaningful to
avoid confusion
• - Must not be too long
• - Must be one word
Control Structures
• A number of control structures are used in designing
Algorithms.
• These includes: simple sequence, selection and iteration
(looping/repetition).
Simple sequence:
• This is whereby instructions are executed in the order they
appear in a program without jumping any one of them up to the
end of the program.
• Statements are executed one after another in the order they
are.
• It is simple and avoids confusion. Example:
PSEUDO CODE
START
START
Enter first Number, A
Enter first number, A
Enter second number, B
Enter 2nd Number, B
C=A+B
C =A+ B Print C
Stop
Print C
STOP
TASK:
Using a Pseudo Code or a flow chart
1. Write a program that will accept the name and
surname of the user and display it.
2. Write a program that will accept three marks. The
program should calculate the average mark and
display it.
Selection Structure:
• This allows one to choose the route to follow in order to
accomplish a specific task.
• Selection is written using the IF ....THEN...ELSE
statement or the CASE statement.
IF...THEN ...ELSE statement: A programming structure
that allows the user to choose one from at least two routes
of solving a problem. The following pseudo codes and flow
chart compares two numbers entered through the
keyboard and determines the bigger one.
CASE STATEMENT: This is an alternative to the
IF...THEN...ELSE statement and is shorter. Example
START
Enter Mark
Case Mark OF
80 to 100
grade = “Distinction”
60 to 79
grade = “Credit”
50 to 59
grade = “Pass”
0 to 49
grade = “Fail”
End Case
STOP
With the IF… THEN …ELSE statement, the program is
coded in conditional execution of two groups of
instructions. The Select Case allow the program to take
any number of branches.
General Syntax
Select Case <expression>
Case <expression> to <expression>
Statements
Or
Case Is <relational operator>
Statements
End Select Case
The expression (in the Select Case) is evaluated, and
instructions are provided for each Case (Value Range).
Assignment:
1. Write a pseudo code that will accept age of a
person. Use a Select case to determine the
comment to be given for each instance. The
comment will be as follows:
Age equal to 10 or less : “You are mighty cute”
Age from 11 up to 20: “You are going to school”
Age from 21 up to 30: “You are working hard”
Age above 30: “You are over the hill”
[20]
2. Draw a program flow chart that will accept two
numbers. The program will add the numbers only
if they are equal, else they will be multiplied.
Tuesday, September
16, 2025
Repetition/ Iteration/
Looping Structure:
FOR… LOOP
REPEAT … UNTIL
WHILE … WEND WHILE
Repetition/Iteration/Looping Structure:
• A control structure that repeatedly executes part of a
program or the whole program until a certain condition
is satisfied.
• Iteration is in the following forms: FOR...NEXT loop,
REPEAT... UNTIL Loop and the WHILE...END WHILE
Loop.
• For...Next Loop: A looping structure that repeatedly
executes the loop body for a specified number of times.
The syntax of the For...Next loop is as follows:
FOR {variable} loop= {starting value} to {ending value} DO
Statement 1 body
Statement 2
................
START
FOR … LOOP
Sum, Average = 0
Start
Sum, Average = 0
i = 0 To 5
FOR I = 1 to 5 DO
Enter Number
Sum = Sum + number
NO Is
NEXT I i <=5?
Average = Sum/5 Average = Sum/5
YES i = i +1
Display Sum, Average
End Display Sum, Average
Enter number
Sum = Sum + number
STOP
Assignment:
1. Write a pseudo code that will allow the user to
enter Marks for Computer Studies test of Ten
learners. The program should also calculate the
average mark for the test. Finally the program
should display the total marks entered and
average. [15]
2. Draw a program flow chart that will represent
the algorithm in question 1.
[15]
Repeat...Until Structure:
A looping structure that repeatedly executes the loop body
for a number of times determined by rogue variable. The
syntax of the Repeat...Until loop is as follows:
Repeat …. Until
Y
While ... Do Statement: A looping structure in which the
loop body is repeatedly executed when the condition set is
TRUE until it becomes FALSE. It is used when the number
of repetitions is not known in advance. The condition set is
tested first before execution of the loop body. Therefore
the loop body may not be executed at all if the condition
set is FALSE from start. The syntax of the WHILE…END
WHILE structure is as follows:
WHILE …. WEND
N
TESTING AND INTERPRETING ALGORITHMS
Dry Running
• Algorithms are designed to solve certain problem,
meaning that every algorithm have a purpose.
• If an algorithm was designed by someone else, the
programmer should try to work out the purpose of the
algorithm.
• How does the programmer work out the purpose of the
algorithm? The programmer need to supply the
algorithm with some data (which is normally referred to
as Test Data) and watch out for the output. This can be
done on the programmer’s mind and it will be called Dry
run.
• Given the following algorithm
Start
Enter Number, A
IF A MOD 2 = 0 THEN
Display “The Entered Number is an Even Number”
ELSE
Display “The Entered Number is an Odd Number”
END IF
Stop
• Say the programmer wants to work out the purpose of
this algorithm.
• First things first, the programmer must read the
algorithm and check the number and type of inputs and
identify the process and the output as most algorithms
follow the pattern input-process-output.
• On the example above the algorithm has one input in the
integer type. So the programmer is supposed to supply it
with a test data item.
• The programmer can run more than one time the
algorithm so as to ascertain the results. So let’s use 5 on
the first run and 8 on the second run.
• After testing the algorithm, the programmer must make
conclusions on the purpose of the algorithm. As in the
Trace tables
• Some algorithms will be very large that dry running
them in the programmer’s mind will be an impossibility
especially those which include repetitions.
• This will give birth to use of trace tables. These are
rows and columns where variables within an algorithm
are recorded.
• Each variable will be assigned its own column.
• If the value remains the same on the successive rows it
will not be repeated. It will only be recorded when the
value is different from the previous one.
• Say A is a variable and its first value is 3, the second
value is 3, the third value is 3 and the fourth value is 7 as
the algorithm is run.
• So the recordings for the column of A will be as follows:
A
3
• This means that even without explanation, the variable A was 3 on
the second and third values.
• Benefits of trace tables are that the programmer:
1. Is less likely to make mistake;
2. Has evidence of the dry run;
3. Can check back to find any mistake in the dry run.
Start
Min=100
Max =0 Produce a trace table of the
Count = 1
Ave = 0
above algorithm using the data
Sum = 0 items:
REPEAT
Enter 56,45,78,87,34,67,90,54,50,80.
Mark
IF Mark >Hence or otherwise outline the
Max THEN
purpose this algorithm.
Max =Mark
END IF
IF Mark < Min THEN
Min =Mark
END IF
Sum = Sum + Mark
Count = Count + 1
UNTIL Count > 10
Ave = Sum/10
PRINT Max, Min, Ave