Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

ALGORITHMS –

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.

To solve this problem, we need an intuitive approach.


First use a small number of integers (for example, five), then
extend the solution to any number of integers.

Next slide shows one way to solve this problem. We


call the algorithm Find-Largest. Each algorithm has a name to
distinguish it from other algorithms. The input for the
algorithm is a list of five integers. The output is the largest
integer.
Finding the largest integer among five integers
Defining actions
Slide 7 does not show what should be done in each
step. We can modify the figure to show more details.

8.9

Defining actions in FindLargest algorithm


Three constructs in developing
algorithms
Computer scientists have defined three constructs for a
structured program or algorithm.
The idea is that a program must be made of a
combination of only these three constructs:
• sequence,
• decision (selection)
• repetition
It has been proven there is no need for any other
constructs. Using only these constructs makes a
program or an algorithm easy to understand, debug or
change.
Three constructs
Sequence: The first construct is called the sequence. An
algorithm, and eventually a program, is a sequence of
instructions, which can be a simple instruction or either
of the other two constructs.
Some problems cannot be solved with only a sequence
of simple instructions. Sometimes we need to test a
condition. If the result of testing is true, we follow a
sequence of instructions: if it is false, we follow a
different sequence of instructions. This is called the
decision (selection) construct.
2. Algorithmic
Representation
13
Algorithmic Representation
There are two maim methods to represents (to construct)
and algorithm:

1- Flowcharts: a pictorial (graphical) representation of a


logical sequence of steps that represent a process. Steps are
represented as boxes of various kinds, and their order by
connecting them with arrows. Flowcharts are sometimes
called UML (unified Modeling Language)

2- Pseudo code: The use of human language to describe a


step by step algorithm. Pseudo code typically omits details
that are not essential for human understanding of the
algorithm, such as variable declarations, system-specific 14
code and some subroutines
Figure 8.7 UML for three constructs
Pseudo-code for three constructs
3. Variables &
data types
17
Variables
• Observe the use of names for the data items in our
Givens and Results
• Num1, Num2, Num3, Sum, Average
• Each name refers to a unique location in computer
memory (one or more adjacent bytes) that contains
a value
• Since that value can change as the instructions in
our algorithm are executed, we call each data item
a variable
• In our algorithm, when we use a variable name, we
18
are referring to the value stored in memory for that
data item
Data Types
• The data type indicates the
• Kind of data that can be stored in the
variable
• byte
• integer numbers
• floating point numbers
• characters
• Size of memory required to store the
variable 19
Specific Data Types
• byte (1 byte) -character
One byte is one memory location that could hold a value
between 0..255
• integer (2 bytes)
• Whole numbers (no decimals)
• -32,768 … 32,767
• long integer (4 bytes)
• Whole numbers (integers)
• -2 trillion … 2 trillion
• float and double (8 bytes)
• Real numbers with floating points
• ± 10^300 with 15 significant digits of accuracy
20
4-Flowcharts

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

Draw a flowchart to find


the sum of first 50
natural numbers

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

Total := SUM3(N1, N2, N3)

Give Total

26

Finish
SUM3
Start

Algorithm: Dividing two numbers DIVISION

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

NAME:AVG3 Get Num2


Get Num3

GIVENS:Num1, Num2,
Num3 Let Sum = Num1 + Num2 + Num3

RESULTS:Sum , Average Let Average = Sum/3

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

A summation algorithm has three logical parts:

1. Initialization of the sum at the beginning.


2. The loop, which in each iteration adds a new integer
to the sum.
3. Return of the result after exiting from the loop.
Summation algorithm
Product
Another common algorithm is finding the product of a
list of integers. The solution is simple: use the
multiplication operator in a loop.

A product algorithm has three logical parts:


1. Initialization of the product at the beginning.
2. The loop, which in each iteration multiplies a new
integer with the product.
3. Return of the result after exiting from the loop.
8.32
Product algorithm
5-Pseudo code
33
Pseudocode Example 1
Write an algorithm in pseudocode that finds the sum of two
integers.
Pseudocode Example 2
Write an algorithm to change a numeric grade (integer) to a letter
grade.

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.

You might also like