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

04 Algorithm PDF

The document discusses algorithms and flowcharts. It provides examples of algorithms to calculate the sum and average of a set of numbers and to determine the temperature based on altitude. The key steps in algorithms are presented using pseudocode and flowcharts. Common flowchart symbols are explained along with variable types and assignments that are used in algorithms.

Uploaded by

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

04 Algorithm PDF

The document discusses algorithms and flowcharts. It provides examples of algorithms to calculate the sum and average of a set of numbers and to determine the temperature based on altitude. The key steps in algorithms are presented using pseudocode and flowcharts. Common flowchart symbols are explained along with variable types and assignments that are used in algorithms.

Uploaded by

priya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Algorithm & Flowchart

Credit: Mr Ainullotfi
Common Flowchart Symbols

Start/Stop Decision

Connector
Process
Off-page
Connector
Input/Output
Comments
Refers to a
Preparation
separate
(for loops etc)
flowchart
Example Problem #1
Given a set of numbers, calculate their
sum and the average value (mean).
Formula: n
1
x = x i
n
i =1

n is the number of numbers in the set


Algorithm
1. Start
2. Get one number in the set
3. Count the numbers as it is obtained
4. If there are still numbers to be
obtained,
go back to step 2.
5. Sum the numbers in the set
6. Divide the sum by the number of numbers
in the set to get the average
7. Show the sum and the average
8. Stop
Flowchart
Start

Calculate mean
Get number

Show sum
Count number Calculate sum and mean

Yes Any more No Stop


number?
Detailed Flowchart
Start
sum 0
i0
Mean = sum/n
j0

ii+1
jj+1 Show sum
and mean
Get xi
sum sum + xj
Stop
Yes Any more
Yes
number? Is j < n?
No
ni No
Pseudocode
1. Start
2. i0
3. ii+1
4. Get xi
5. If there more numbers repeat from
3.
6. ni
7. sum  0
8. j0
Pseudocode
9. j  j + 1
10. sum  sum + xi
11. If j < n repeat from step 9
12. mean  sum / n
13. Show sum and mean
14. Stop
Variables
A variable is a location in the computer
memory which is given a specific name
and can hold a single value at a time
A variable can be compared to a box or
a container that is given a label and
the box can hold one content at a time
In the last example, i, j, n, sum, mean
and x1, x2, x3 etc are all variables
Variable Assignments
Variables are given values either directly by
the user through the input statements (e.g.
Get xii) or by assignments statements
i  0 is an assignment expression meaning
assign the value 0 to variable i
n  i means assign the value equivalent to
that in variable i to variable n (the value in
variable i is not changed)
j  j + 1 means add 1 to the value in j
Variable Types
Variables can be of several types
depending of the kind of data it stores
In general variables can be classified into:
(a) Numeric type
(b) String type
(c) Logical type
Assignment expressions would involve
similar type of variables only
Numeric Variables
Numeric variables store numerical data
which can be used in mathematical
calculations
Examples of numeric expressions are:
i0
jj+1
mean  sum / n
y  x*x
z  sin(x) + 3
String Variables
String variables store alphanumeric
data, symbols and control characters
Although strings may store numbers,
they are of the type not used for
calculations e.g. phone numbers,
addresses etc
String variables are useful for labels,
names and comments
name  lotfi is a string expression
Logical Variables
Logical variables store only either a
True or a False value
k  (3 > 4) is an example of a logical
expression in this case k has the value
False since it is not true that 3 is
greater than 4
Logical expressions are useful for tests
and decision making algorithms
Example Problem #2
Atmospheric temperature vary with altitude
according to the following tables
Alt h (m) Temp T (K)
0 288.15 Alt h (m) dT/dh (K/m)
11000 216.65 0-11000 -6.5 x 10-3
20000 216.65 11000-20000 0
32000 228.65 20000-32000 1 x 10-3
47000 270.65 32000-47000 2.8 x 10-3
51000 270.65 47000-51000 0
71000 214.65 51000-71000 -2.8 x 10-3
85000 186.946 71000-85000 -2.0 x 10-3
Standard Atmosphere
(Air Temperatures)
300

290
288.15
280
270.65 270.65
270

260

250
Troposphere
Tem perature (K)

240
Stratosphere Mesosphere
230
228.65
220
216.65 216.65 214.65
210

200

190
186.946
180

170

160

150
0 10000 20000 30000 40000 50000 60000 70000 80000
Altitude (m)
Example Problem #2
The Troposphere is the layer from sea
level up to 11000 m
The Stratosphere is between 11000 to
51000m
The Mesosphere is between 51000 to
71000m
Given an altitude, the temperature of
the atmosphere need to be calculated
Algorithm
1.
Start
2.
Get altitude
3.
Determine which altitude band it is in
4.
Calculate the temperature using the
equation associated with that band
5. Show the altitude and the temperature
6. Stop
Flowchart
Start

Get altitude

Determine Calculate Show sum


altitude band temperature and mean

Stop
Start Flowchart
Yes

Get
altitude h

Yes
h < 11000? T 288.15 6.5*h*10-3

No
Yes
h < 20000? T 216.65

No
Yes
h < 32000? T 216.65 + h*10-3

No
B
A
A Flowchart
h < 47000? T 228.65 + 2.8*h*10-3 A

h < 51000? T 270.65 Show h


and T

h < 71000? T 270.65 - 2.8*h*10-3 Stop

T 214.65 - 2*h*10-3
Pseudocode
1. Start
2. Get h
3. If h < 11000 then
4. T 288.15 6.5*h*10-3
-3

5. Else if h < 20000 then


6. T 216.15
7. Else if h < 32000 then
8. T 216.15 + *h*10-3
-3
Pseudocode
9. Else if h < 47000 then
10. T 228.65 + 2.8*h*10-3
-3

11. Else if h < 51000 then


12. T 270.65
13. Else if h < 71000 then
14. T 270.65 2.8*h*10-3-3

15. Else T 214.65 + 2*h*10-3


-3

16. Show h and T


17. Stop
Types of Algorithms
Sequential algorithm
Looping algorithm
Decision algorithm
Link algorithm

You might also like