ALGORITHMS AND PROBLEM SOLVING Lesson Note
Learning Objectives
This SS3 DIGITAL TECHNOLOGY lesson note covers ALGORITHMS AND PROBLEM SOLVING for First Term, Week 4.
Lesson Note
Learning Objectives
By the end of the lesson, students should be able to:
Define an algorithm.
Explain the importance of algorithms in problem solving.
Identify the characteristics of a good algorithm.
Explain the different ways of representing an algorithm.
Define a flowchart and identify its symbols.
Explain pseudocode and give examples.
Describe the stages involved in solving a problem.
Differentiate between an algorithm, pseudocode and a flowchart.
Apply algorithms to simple real-life and computing problems.
Explain the concepts of sequence, selection and iteration.
4. MEANING OF AN ALGORITHM
An algorithm is a finite, well-defined and ordered set of instructions or steps used to solve a problem or perform a particular task.
In simple terms, an algorithm is a step-by-step procedure for solving a problem.
Example
Suppose we want to calculate the area of a rectangle.
Algorithm:
Start.
Enter the length.
Enter the width.
Multiply length by width.
Display the area.
Stop.
If the length is 10 cm and the width is 5 cm:
Area = Length × Width
Area = 10 × 5 = 50 cm²
Algorithms are used in computer programs because a computer needs clear instructions to know what operations to perform.
B. IMPORTANCE ofOF ALGORITHMS
Algorithms are important in computing and everyday activities because they:
Provide clear instructions: They show exactly what should be done.
Help solve problems: Complex problems can be divided into smaller steps.
Reduce errors: Following a properly designed algorithm can reduce mistakes.
Make programming easier: Programmers use algorithms as a plan before writing programs.
Save time: A well-designed algorithm can provide an efficient solution.
Make problems easier to understand: The steps involved in solving a problem become clear.
Make testing easier: Each step can be checked to determine where an error occurs.
Allow repetition: Algorithms can contain instructions that are repeated when necessary.
Support automation: Computers can carry out algorithmic instructions automatically.
Make solutions reusable: An algorithm can be used again to solve similar problems.
C. CHARACTERISTICS OF A GOOD ALGORITHM
A good algorithm should have the following characteristics:
1. Finiteness
An algorithm must eventually come to an end. It should not continue forever.
2. Definiteness
Every instruction must be clear and unambiguous. Each step should have only one reasonable interpretation.
3. Input
An algorithm may require one or more inputs. For example, an algorithm for calculating an average may require students’ scores.
4. Output
An algorithm should produce a result or output.
5. Effectiveness
Each instruction should be simple enough to be carried out.
6. Correctness
The algorithm should produce the expected result when properly followed.
7. Efficiency
A good algorithm should use reasonable amounts of time and computer resources.
8. Generality
The algorithm should be applicable to a range of similar problems rather than only one specific example.
D. STEPS INVOLVED IN PROBLEM SOLVING
Problem solving involves finding a suitable solution to a problem.
The major stages include:
Step 1: Identify and Define the Problem
The first step is to understand what the problem is.
For example:
A teacher wants to calculate the average score of three students.
The problem is to determine the average.
Step 2: Analyse the Problem
Determine:
*What information is available?
*What information is required?
*What calculations or operations are necessary?
*What are the inputs?
*What should be produced as output?
For the example:
Inputs: Three scores
Process: Add the scores and divide by 3
Output: Average score
Step 3: Design the Solution
Develop a step-by-step procedure for solving the problem.
This can be done using:
*An algorithm
*Pseudocode
*A flowchart
Step 4: Implement the Solution
Convert the solution into a programming language or carry out the instructions.
Step 5: Test the Solution
Check whether the solution produces the correct results.
Different sets of data may be used during testing.
Step 6: Debug the Solution
If errors are discovered, they are identified and corrected.
Step 7: Document the Solution
The solution and important explanations are recorded so that it can be understood and maintained later.
E. WAYS OF REPRESENTING ALGORITHMS
Algorithms can be represented in different forms. The major forms include:
Natural language
Pseudocode
Flowcharts
Programming languages
Natural Language Representation
An algorithm can be written using ordinary language.
Example: Algorithm for making a cup of tea
Start.
Boil water.
Put tea in a cup.
Pour hot water into the cup.
Add milk and sugar if required.
Stir.
Serve the tea.
Stop.
Natural language is easy to understand, but it may sometimes be ambiguous.
F. PSEUDO CODE
Pseudocode is a simple, informal way of writing the steps of an algorithm using words and structures that resemble programming instructions.
It is not written according to the strict rules of a particular programming language.
Example: Pseudocode for adding two numbers
START
INPUT Number1
INPUT Number2
Sum = Number1 + Number2
OUTPUT Sum
STOP
Pseudocode allows a programmer to plan the logic of a program before writing actual code.
G. ADVANTAGES OF PSEUDO CODE
1. Easy to read.
2. Easy to understand.
3. Does not depend on a particular programming language.
4. Helps programmers plan programs.
5. Makes errors in logic easier to identify.
6. Can be converted into actual program code.
H. FLOWCHART
A flowchart is a graphical representation of an algorithm. It uses standard symbols connected by arrows to show the sequence of operations.
Flowcharts make it easier to visualize how a problem is solved.
Common Flowchart Symbols
Symbol Name Function
Oval Terminator Represents Start or Stop
Rectangle Process Represents an operation or calculation
Parallelogram Input/Output Represents data input or output
Diamond Decision Represents a condition or choice
Arrow Flow line Shows the direction of the process
Circle Connector Connects different parts of a flowchart
SIMPLE FLOWCHART STRUCTURE
START
↓
INPUT DATA
↓
PROCESS
↓
OUTPUT RESULT
↓
STOP
I. SEQUENCE, SELECTION AND ITERATION
Most algorithms are built from three fundamental control structures:
1. Sequence
A sequence means instructions are carried out one after another in the order in which they are written.
Example
Enter two numbers.
Add the numbers.
Display the answer
The instructions are performed in sequence.
2. Selection
Selection occurs when a decision must be made based on a condition.
A condition normally has two or more possible outcomes.
Example
IF score >= 50
Display “PASS”
ELSE
Display “FAIL”
ENDIF
If the score is 50 or above, the student passes. Otherwise, the student fails.
Common forms of selection
IF
IF…ELSE
IF…ELSE IF…ELSE
CASE/SWITCH
3. Iteration
Iteration means repeating a set of instructions until a particular condition is met.
Iteration is also called looping.
Example
FOR number = 1 TO 5
DISPLAY number
NEXT number
This instruction displays the numbers 1 to 5.
J. COMMON TYPES OF LOOPS
FOR loop
WHILE loop
DO…WHILE loop
1. Example of an Algorithm for Finding the Largest of Two Numbers
Problem
Write an algorithm to determine the larger of two numbers.
Algorithm
Start.
Enter Number A.
Enter Number B.
Compare A and B.
If A is greater than B, display A.
Otherwise, display B.
Stop.
Pseudocode
START
INPUT A
INPUT B
IF A > B THEN
OUTPUT A
ELSE
OUTPUT B
ENDIF
STOP
2. Example: Algorithm for Finding the Average of Three Numbers
Algorithm
Start.
Input the first number.
Input the second number.
Input the third number.
Add the three numbers.
Divide the sum by 3.
Display the average.
Stop.
Pseudocode
START
INPUT A
INPUT B
INPUT C
SUM = A + B + C
AVERAGE = SUM / 3
OUTPUT AVERAGE
STOP
K. ALGORITHM AND PROGRAMMING
An algorithm is not the same as a computer program.
An algorithm provides the logic or plan for solving a problem, while a program is the actual implementation of that solution using a programming language.
For example:
Problem → Algorithm → Pseudocode/Flowchart → Program → Testing → Result
A programmer should normally understand the problem and design the algorithm before writing the program.
L. ALGORITHM EFFICIENCY
Not all algorithms solve problems with the same level of efficiency. An efficient algorithm should generally:
1. Take less time to produce a result.
2. Use fewer computer resources.
3. Avoid unnecessary operations.
4. Work correctly with large amounts of data.
5. Be easy to maintain and understand.
For example, when searching through a large list, some searching methods can find information much faster than simply checking every item one after another.
M. ERRORS IN PROBLEM SOLVING
Errors can occur when designing or implementing a solution.
Common types include:
1. Logical Error:
The instructions are followed, but they produce an incorrect result because the reasoning is wrong.
2. Syntax Error:
An error in the rules or structure of a programming language.
3. Runtime Error:
An error that occurs while a program is running.
4. Data/Input Error:
Incorrect or inappropriate data is entered into the system.
Proper testing and debugging help identify and correct errors.
N. ALGORITHM VS PSEUDOCODE VS FLOWCHART
Algorithm Pseudocode Flowchart
*Step-by-step solution Text-based representation Graphical representation
to a problem of an algorithm of an algorithm
*Can be written in Uses programming-like Uses standard symbols
ordinary language statements
*Focuses on the procedure Focuses on program logic Shows the flow visually
*May be less formal more structured than Uses symbols and arrows
ordinary language
N. ADVANTAGES OF USING FLOWCHARTS
Flowcharts:
Make algorithms easier to understand.
Show the flow of a process visually.
Help programmers identify errors.
Make communication between programmers easier.
Help in program planning.
Make complex processes easier to study.
Serve as documentation for a system.
Help in modifying or improving a solution.
PRACTICAL EXAMPLE
Problem
Develop an algorithm to determine whether a number is positive, negative or zero.
Algorithm
Start.
Enter a number.
If the number is greater than zero, display “Positive”.
If the number is less than zero, display “Negative”.
Otherwise, display “Zero”.
Stop.
PSEUDOCODE
START
INPUT Number
IF Number > 0 THEN
OUTPUT “Positive”
ELSE IF Number < 0 THEN
OUTPUT “Negative”
ELSE
OUTPUT “Zero”
ENDIF
STOP
This example demonstrates selection, because the computer must make a decision based on the value entered.
SUMMARY
An algorithm is a clear, ordered and finite set of instructions for solving a problem. Algorithms are important because they help programmers plan solutions before implementing them in a programming language.
A good algorithm should be finite, definite, correct, effective and efficient. Algorithms can be represented using natural language, pseudocode, flowcharts or programming languages.
Problem solving involves identifying a problem, analysing it, designing a solution, implementing it, testing it, debugging errors and documenting the solution.
The three basic control structures used in algorithms are sequence, selection and iteration.
Evaluation
OBJECTIVE QUESTIONS
1. Which of the following best defines an algorithm?
A. A computer’s physical component
B. A step-by-step procedure for solving a problem
C. A type of computer virus
D. A storage device
2. Which characteristic requires an algorithm to eventually come to an end?
A. Input
B. Finiteness
C. Output
D. Selection
3. Which of the following is used to represent an algorithm graphically?
A. Spreadsheet
B. Flowchart
C. Database
D. Keyboard
4. Which flowchart symbol is normally used to represent a decision?
A. Rectangle
B. Oval
C. Diamond
D. Parallelogram
5. What is pseudocode?
A. A type of computer hardware
B. A programming language used only for games
C. An informal representation of program logic
D. A computer storage device
6. Which control structure involves carrying out instructions one after another?
A. Selection
B. Iteration
C. Sequence
D. Decision
7. Which of the following is an example of iteration?
A. Performing an instruction once
B. Repeating instructions while a condition is satisfied
C. Entering a username
D. Displaying a result
8. Which flowchart symbol represents input or output?
A. Diamond
B. Rectangle
C. Parallelogram
D. Oval
9. What is the main purpose of testing a solution?
A. To decorate the program
B. To check whether the solution works correctly
C. To increase the size of the computer
D. To remove the keyboard
10. Which of the following should normally be done before writing a computer program?
A. Design the solution or algorithm
B. Delete the operating system
C. Disconnect the computer
D. Remove the storage device
THEORY QUESTIONS
1a. Define an algorithm and explain five characteristics of a good algorithm.
1b. Describe the major stages involved in solving a computer-related problem.
2a. Explain the difference between an algorithm, pseudocode and a flowchart.
2b. Explain the three basic control structures used in algorithms, giving one example of each.
2c. Write an algorithm and pseudocode for determining whether a student’s score is a pass or fail, assuming a score of 50 or above is a pass.