Algorithms and Flowcharts
Algorithms and Flowcharts
INTRODUCTION
1
Content
• The Concept of an Algorithm
• Algorithm Representation
• Variables & Data types
• Flowcharts
• Pseudo code
2
1.Definition
3
Definition
• An algorithm is a finite sequence of step by step,
discrete, instructions for solving a particular problem
• An algorithm needs input data, and produces
output after processing the data.
• Processing is done by instructions or commands
that are ordered logically.
4
Before developing Algorithm
Understand the problem before solving it
“If you understand the problem, you are halfway there!”
• Identify & name each Input
• Understand the process
• Identify & name each Output
5
Algorithm for a vending machine
• Class activity
• Consider the vending
Machine in the lobby.
• Write an algorithm
for buying coffee
• Class discussion
6
Example
Consider developing an algorithm for finding the largest
integer in a list. The algorithm should be general and not
depend on the number of integers.
8.9
21
Definition: Flowchart
• A flowchart is a diagram, that represents an
algorithm or process, showing the steps as
boxes of various shapes, and the order of the
flow by connecting them with arrows.
• Flowcharts are used in analyzing, designing,
documenting or managing a process or
program in various fields.
• Modern techniques such as UML activity
diagrams can be considered to be extensions
of the flowchart.
22
Flowchart Sybols
23
Flow chart symbols
24
Example 1
25
Algorithm: Adding Three Start
SUM3
Numbers
Get N1
Get N2
Get N3
NAME: SUM3
GIVENS: N1, N2, N3
RESULTS: Total
DEFINITION: Let Total = N1 + N2 + N3
Give Total
26
Finish
SUM3
Start
Get X
Get Y
NAME: Division
GIVENS: X, Y
RESULTS: Quotient
Let Quotient = X/Y
DEFINITION:
Quotient := Division(X, Y)
Give
Quotient
27
Finish
DIVISION
Algorithm: Average of three numbers Start
AVG3
Get Num1
GIVENS:Num1, Num2,
Num3 Let Sum = Num1 + Num2 + Num3
DEFINITION:
Sum & Average := Give Suml
Give Average
AVG3(Num1,
Num2, Num3)
Finish
28
AVG3
Summation
One commonly used algorithm in computer science is
summation. We can add two or three integers very
easily, but how can we add many integers? The solution
is simple: we use the add operator in a loop
8.35
Pseudocode Example 3
Write an algorithm to find the largest of a set of integers. We do
not know the number of integers.
8.36
Pseudocode Example 4
Write an algorithm to find the largest of the first 1000 integers in
a set of integers.