0% found this document useful (0 votes)
10 views

Algorithms

An algorithm is a set of step-by-step instructions to solve a problem. Algorithms are used in programming to determine how to solve a problem before actual coding. There are several ways to express algorithms, including structured English, flowcharts, and pseudocode. Structured English describes the steps in plain language. Flowcharts use standard symbols to show the flow. Pseudocode is a mix of regular language and code-like syntax. All provide a way to plan the logic before programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Algorithms

An algorithm is a set of step-by-step instructions to solve a problem. Algorithms are used in programming to determine how to solve a problem before actual coding. There are several ways to express algorithms, including structured English, flowcharts, and pseudocode. Structured English describes the steps in plain language. Flowcharts use standard symbols to show the flow. Pseudocode is a mix of regular language and code-like syntax. All provide a way to plan the logic before programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Algorithms

- An algorithm is a set of step-by-step instructions to solve a problem or accomplish a


task.
- Algorithms are used in programming to determine how a problem can be solved
before the actual programming takes place.
- Programming is the process of writing programs using specific programming
languages such as Python, Java, Visual Basic, C etc.
- Programming is also known as coding and the person who writes programs is called a
programmer.
- Both programming and algorithms are used to solve problems.

Decomposing a problem
- Any problem to be solved by a computer system should be broken down
(decomposed) into its component parts.
- These parts are:
1) INPUTS – the data to be entered such as marks.
2) PROCESSES – tasks to be performed such as adding.
3) OUTPUTS – information that is displayed or printed for the user.
4) STORAGE – data stored for later use.
- All algorithms should make use of some or all of the parts.

Characteristic of an algorithm
i. It should be able to solve the given problem.
ii. It must be simple.
iii. It must be clear and unambiguous (not having two or more possible meanings or
interpretations).
iv. It must be finite (it must have an ending).
v. It should be independent of a programming language.
vi. It should be space-efficient (does not use more memory than necessary).
vii. It should be time-efficient (solve a problem within the shortest possible time).

Building blocks of Algorithms


An algorithm is made up of 3 building blocks which are:
1. Sequence
- This is the order in which instructions are carried out.
- The sequence of instructions is very important because carrying out
instructions in the wrong order results in a program/algorithm not
performing correctly.

2. Selection
- This is the ability to make a decision based on certain conditions.
- Selection works by testing a condition.
- If the result is TRUE, the program follows one path but if it is false, it follows
another path.
- An example is if a student scores 50% or more then they should receive a
pass grade but if not, they should receive a fail grade.

3. Iteration
- This is repeating the same set of steps until a condition has been met.
- It is also referred to as repetition or looping.
- Instead of writing out the same statements again and again, they can be
written once and carried out many times as desired.

Methods of expressing algorithms solving problems


- An algorithm can be expressed in many ways such as:
1. Structured English
2. Flowchart
3. Pseudocode

Structured English
- Structured English is a way of describing an algorithm using simple English language.
- The steps to solving a problem are described in a simple way as one would for
instance write a recipe.
Example 1
Write an algorithm using Structured English to add two marks for Science.
Enter the first Science mark
Enter the second Science mark
Add the first Science mark to the second Science mark
Display the result of the total

Example 2
Write an algorithm using Structured English to enter two numbers and find out which one is
the largest.
Enter the first number
Enter the second number
Compare the first number to the second number
If the first number is greater than the second number
Print that the first number is the largest
If the first number is smaller than the second number
Print that the second number is the largest
Else print that the numbers are equal
Advantages of Structured English
1. It is easy for a user who knows nothing about computer programming to be able to
understand the steps involved.
2. The descriptions make it easy for a programmer to convert to a programming
language.
3. It requires less time to write.
4. There is no need to worry about using correct syntax (rules of programming)

Disadvantages of Structured English


1. It can not be used by non-English speakers.
2. Structured English can be hard for a person to learn if English is their second
language.
3. There is a temptation to use too many words.

Flowchart
- A flowchart is a diagram used to illustrate the steps of an algorithm.
- Flowcharts are made up of symbols,
- Each symbol contains a single step of the algorithm.

NAME SYMBOL MEANING


1. Terminator Marks the beginning or end
of the flowchart
(Start/Stop)
(Begin/End)

2. Flow line Shows the flow from one


step to the next.
Flow is from top to bottom
or from left to right.
3. Input/Output Represents data entered by
the user or information
displayed to the user

4. Process action to be performed e.g. a


calculation such as adding

5. Decision Test of a condition that


allows the flowchart to
branch into one of two
paths, yes or no.
Example 1
Write an algorithm using a flowchart to add two marks for Science.

BEGIN

Input Mark1, Mark2

TotalMark = Mark1 + Mark2

Display TotalMark

END
Example 2
Write an algorithm using a Flowchart to enter two numbers and find out which one is the
largest.

BEGIN

Input Number1, Number2

Number 1
NO > YES
Number 2

Display Number 1
‘ is the largest’
Number 1
NO YES
<
Number 2

Display ‘The Display Number 2


numbers are equal’ ‘ is the largest’

END
Advantages of Flowcharts
1. It makes it easy to understand how a program flows.
2. Flowcharts follow an international standard so it is easy for anyone to pick up a
flowchart and understand it.
3. Mistakes can be easily identified and corrected.

Disadvantages of Flowcharts
1. In a large program the diagram can become huge and therefore difficult to follow.
2. It is time-consuming to draw them.
3. It is difficult for people who do not know flowchart symbols to understand

Pseudocode

You might also like