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

Algorithms

The document discusses algorithms and different ways to represent them. It begins by defining an algorithm as a step-by-step process to solve a problem, usually expressed through a flowchart, pseudocode, or natural language. It then covers the basic components of algorithms like inputs, outputs, sequence, decisions, and recursion. The remainder discusses designing algorithms using pseudocode, natural language, and flowcharts, including examples.

Uploaded by

Vine Vapour
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Algorithms

The document discusses algorithms and different ways to represent them. It begins by defining an algorithm as a step-by-step process to solve a problem, usually expressed through a flowchart, pseudocode, or natural language. It then covers the basic components of algorithms like inputs, outputs, sequence, decisions, and recursion. The remainder discusses designing algorithms using pseudocode, natural language, and flowcharts, including examples.

Uploaded by

Vine Vapour
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

1

OBJECTIVES
Understand the concept of an algorithm.

Define and use the three constructs for developing


algorithms: sequence, decision, and repetition.

Understand and use three tools to represent algorithms:


flowchart, pseudocode, natural language.

Design an algorithm and convert it to a program


(creating a program from an algorithm)

2
Introduction to Algorithms
An algorithm is a plan, a logical step-by-step
process for solving a problem normally
expressed using flowchart or pseudocode or
Natural Languages.
The key to any problem solving task is to guide
your thought process.
The most useful thing to do is keep asking ‘What
if we did it this way?’ Exploring different ways of
solving a problem can help come up with more3
Introduction to Algorithms cont
Therefore, when designing an algorithm,
consider if there is more than one way of
solving the same problem.
Understanding the problem is very important:
Before designing an algorithm, it is important to
check and completely understood the problem
you are solving.

4
Basic things to help in
problem understanding
The inputs: What will be the inputs into the
problem?
The outputs: What will be the outputs of the
problem?
Operation: In what order do instructions need to
be carried out?
Decision: What decisions need to be made in the
problem?
Recursion: Are any areas of the problem 5
Designing an algorithm
The algorithm of a program can be designed
using pseudo code or a flowchart

6
Designing an algorithm using
pseudo code.
Pseudo code is a way of expressing an
algorithm in structured English that resembles
computer programming language.
It does not enforce strict syntax and therefore,

natural language like English and programming


language syntax can be mixed together provided
steps are clear and consistent.

7
Designing an algorithm using
pseudo code cont...
Pseudo code uses commands, keywords and
structures similar to those found in programming
languages.
Cannot be understood by computers, but is used
to develop the logic of a program without
needing to worry about the syntax.
A solution in pseudo code can then be
converted into a high-level language.
Uses English-like phrases to outline the task.
8
Designing an algorithm using
Natural language.
Natural language comprises of set of steps
written in a language that can be understood by
human. Such languages are English.
The steps are listed and explained in a
sequential order.

9
Designing an algorithm using
Natural language.
Natural language comprises of set of steps
written in a language that can be understood by
human. Such languages are English.
The steps are listed and explained in a
sequential order.

10
Designing an algorithm using
Flowcharts.
Flowcharts are diagrammatic/schematic
representations of processes taken to solve a problem.
Flowcharts uses notation to represent the start point,
end points, inputs, outputs, possible paths and
the decisions that lead to these possible paths.
Flow-charts can be created by hand or manually in
most office software, but lately specialized diagram
drawing software has emerged that can also be used
for the purpose.

11
Flowcharts
• Graphically depict the logical steps to
carry out a task and show how the steps
relate to each other.

12
Flowchart Symbols (Mostly Used)

• Start / End symbol


• Arrows

• Processing steps

• Input/Output

• Conditional (Decision)

11/01/19 Chapter 1 - VB 2008 by Schneider


Flowchart symbols

14
Flowchart symbols continued

15
Flowchart
example

16
Statement structures
• Sequence – follow instructions from one line to the
next without skipping over any lines
• Decision - if the answer to a question is “Yes” then
one group of instructions is executed. If the answer
is “No,” then another is executed
• Looping – a series of instructions are executed over
and over

17
Divide-and-conquer method
• Used in problem solving – take a large
problem and break it into smaller
problems solving the small ones first
• Breaks a problem down into modules

18
Sequence
flow chart

19
Direction of Numbered Nairobi
Streets Algorithm
• Problem: Given a street number of a one-way
street in Nairobi City, decide the direction of
the street, either eastbound or westbound
• Discussion: In Nairobi City even numbered
streets are Eastbound, odd numbered streets
are Westbound

20
Flowchart

21
Pseudocode

An Algorithm in Pseudo code: Example 1

Written Description Flowchart Pseudocode


SEND ‘Enter first number’ TO
Start
DISPLAY
RECEIVE num1 FROM (INTEGER)
Enter num1 KEYBOARD
SEND ‘Enter second number’ TO
Enter first number
Enter num2 DISPLAY
Enter second number
RECEIVE num2 FROM (INTEGER)
Add together to get total = KEYBOARD
the total num1 +
Output the total num2
SET total TO num1 + num2
SEND total TO DISPLAY
Output total

End
END
An Algorithm in Pseudocode: Example 2

Written Description Flowchart Pseudocode

Start
SEND ‘Enter age’ TO DISPLAY
Enter age RECEIVE age FROM (INTEGER)
Enter your age KEYBOARD
If the age is larger NO
type =
than 18 set the type If age > 17
child IF age > 17 THEN
to adult Output SET type TO adult
YES
Else if the age is less ‘child’ ELSE
type = adult
than or equal to 18 set End SET type TO child
the type to child Output ‘adult’
END IF
Display the type
End SEND type TO DISPLAY
END

You might also like