0% found this document useful (0 votes)
11 views41 pages

Algorithms

An algorithm is a structured series of steps designed to solve a programming problem, requiring a defined start and end point, and each step must be executable within finite resources and time. Common representations of algorithms include flowcharts and pseudocode, which aid in visualizing and understanding the logic. The document also discusses various programming constructs, examples of algorithms, and the concept of top-down design for problem-solving.

Uploaded by

selesterat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views41 pages

Algorithms

An algorithm is a structured series of steps designed to solve a programming problem, requiring a defined start and end point, and each step must be executable within finite resources and time. Common representations of algorithms include flowcharts and pseudocode, which aid in visualizing and understanding the logic. The document also discusses various programming constructs, examples of algorithms, and the concept of top-down design for problem-solving.

Uploaded by

selesterat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

1

ALGORITHMS …
 An algorithm is the series of steps that are used to
solve a programming problem.
 It may also be considered as a plan for solving a
programming problem that specifies the sequence
of steps used in the solution.
 The following are mandatory features that an
algorithm must have:
(I) An algorithm must have a start and an end
point.
(II)very step must be doable or performable:-
(A) using a finite amount of resources
(B) in a finite amount of time.
2
ALGORITHMS…
 In summary an algorithm will consist of a series
of steps solving a problem.
 There must be a starting point and an ending
point and each step is something the computer
can do and finish.
 So the format of an algorithm is: start, step1,
step2, … , stepn, stop.
 Generally the steps are numbered or labeled
from start to stop.
 Formulation of algorithms is a major step in
writing computer programs.

3
ALGORITHMS :Examples
 To get the mean of a set of numbers
 To get the standard deviation of a set of numbers
 To buy food in the mess
 To open an email account in yahoo

4
ALGORITHMS …
 The most common means of representing
algorithms is by the use of
flowcharts
pseudo code.
 We will examine each of these.

5
ALGORITHMS …

Flow Charts
 They are visual representation of algorithms
 Typically show a program’s logic.
 Can be instantiated using any programming
language we want i.e. flow charts are language
independent
 Flow charts are also easy for non-programmers to
understand.

6
ALGORITHMS …

Flow Chart Symbols


 Each symbol contains information on what must be
done at that particular point.
 The arrow shows the order in which the instructions
must be executed

Input/ Output Start/Stop

Read Write Start Stop

7
ALGORITHMS …

Flow Chart Symbols …

Process box
 Use not more than two statements in each box.

Sum:= Sum + 1;
Count:= Count + 1;

8
ALGORITHMS …
Flow Chart Symbols …
Decision/selection box
 Based on the value of the decision
parameter, follow one of the two
available paths

9
ALGORITHMS …

Flow Chart Symbols …


Direction line (next step)
 Natural left to right (no arrow indicator)

 Natural top to down (no arrow indicator)

 Other directions are as indicated:

10
ALGORITHMS …
Flow Chart Symbols …

Preparation box Connection circle

11
ALGORITHMS …

PSEUDO CODE
 Pseudo codes are mixture of English statements
and terms that are used in programming. The
programming terms may include:
START, BEGIN, END, STOP, IF, THEN, WHILE,
DO WHILE, END WHILE, REPEAT, FOR, UNTIL,
DISPLAY, WRITE, READ, etc.

12
ALGORITHMS …
STRUCTURED PROGRAMMING CONSTRUCTS
 Programming constructs are the building blocks of
a program. There are only three programming
constructs.
 These are sequence, repetition, selection. These
are considered below:-
1. Sequence
Start:
process A;
process B;
Process C;
Stop: 13
ALGORITHMS …
2. REPETITION
 These are steps that must be performed more than
once.
1) WHILE

WHILE CONDITION
Process;
WHILE exit
action adjustment;
END WHILE

14
ALGORITHMS …

2. REPETITION …
2) REPEAT UNTIL

REPEAT:
Process
Until condition
adjustment
UNTIL condition holds

15
ALGORITHMS …
DECISION/ SELECTION

IF condition holds THEN


perform Process
ELSE
do something else
(including nothing)

16
ALGORITHMS …
EXAMPLES OF ALGORITHMS
 Algorithm that reads in, displays and exchanges integer
values of two variables.
Hint: use temporary.
Algorithm Description
1. Start
2. Set up variables
3. Initialize variables
4. Read in value of Variable_A
5. Read in value of Variable_B
6. Display value of Variable_A
7. Display value of Variable_B 17
ALGORITHMS …

8. Copy value of Variable_A into Temporary


9. Copy value of Variable_B into
Variable_A
10.Copy value of Temporary into Variable_B
11.Display value of Variable_A
12.Display value of Variable_B
13.Stop

18
ALGORITHMS …

 Algorithm that reads in, displays and exchanges integer


values of two variables.

Check if the algorithm works

19
ALGORITHMS …
 Algorithm that reads in, displays and exchanges integer
values of two variables.
Pseudocode
START
VARIABLES Variable_A, Temporary, Variable_B
Variable_A:=0; Temporary:=0; Variable_B:=0;
READ Variable_A;
READ Variable_B;
WRITE Variable_A;
WRITE Variable_B;
Temporary:= Variable_A;
Variable_A:=Variable_B;
Variable_B:=Temporary;
WRITE Variable_A;
WRITE Variable_B;
STOP. 20
Algorithms… Start

 Algorithm That reads


in, displays and
Variables: Variable_A:=0
exchanges integer Variable_A Variable_B:=0
values of two
Variable_B
Temporary

variables.
Temporary:=0
Flow chart Read
Variable_A
Read
Variable_B

Display
Variable_A
Display Temporary := Varible_A :=
Variable_B Varibale_A Varible_B;

Display
Variable_A Varibale_B :=
Display Temporary
Variable_B

Stop 21
ALGORITHMS …
 An Algorithm That Reads & Displays A Set Of 6
Numbers
Planning
 Track the number of times reading is done by setting up
a counter.
 When 6 numbers have been read, stop.
Algorithm Description
1. Start
2. Setup variables: Number, Count
3. Initialize variables: Number:=0; Count:=0;
4. Read a Number
5. Display the Number
6. Increase Count by 1
7. If Count<6 Repeat from 4
8. Stop 22
ALGORITHMS …

 An Algorithm That Reads & Displays A Set Of 6 Numbers


(Using REPEAT UNTIL repetition construct)
Pseudocode
START
Variables: Number, Count
Number:=0;
Count:=0;
REPEAT
READ Number;
DISPLAY Number;
Count:=Count+1; //increase count by 1
UNTIL Count=6;
STOP
23
ALGORITHMS …
An Algorithm That Reads & Displays A Set Of 6
Numbers using DO WHILE repetition construct
START
Variables: Number, Count
Number:=0;
Count:=0;
DO WHILE Count<6
READ Number;
DISPLAY Number;
Count := Count + 1;
END WHILE
STOP
24
Algorithms …
 An Algorithm That Reads &
Displays A Set Of 6 Numbers
Flow Chart

25
ALGORITHMS …
An Algorithm To Compute The Sum Of N Numbers
 Let the numbers be 5,3,4,5,10,30 but they could be any
numbers
Algorithm Description
1. Start
2. Set up variables: Sum, Count, Number,
Number_of_items
3. Initialize variables ( Sum:=0; Count:=0;
Number:=0)
4. Read Number_of_items
5. Read a Number
6. Add 1 to count
7. Add Number to Sum
8. If Count < Number_of_items then repeat from 5
9. Display Sum
26
10.Stop
ALGORITHMS …
 An Algorithm To Compute The Sum Of N Numbers

27
Algorithms
 An Algorithm To
Compute The Sum Of N
Numbers
Flowchart

28
ALGORITHMS …
 An Algorithm To Compute The Sum Of N Numbers
Pseudocode
START
Variables: Sum, Count, Number, Number_of_items
Sum:=0;
Count:=0;
READ Number_of_items;
REPEAT
READ Number;
Count:=Count+1;
Sum:=Sum+Number;
UNTIL Count=Number_of_items;
DISPLAY Sum;
29
STOP
ALGORITHMS …

Algorithm For Computing The Factorial Of A Number


Less Than 12
Planning:
0!=1
1!=1
2!=1x2
3!=1x2x3
4!=1x2x3x4
10!=1x…x9x10
n!=1x…x(n-1)xn
30
ALGORITHMS …
 Algorithm For Computing The Factorial Of A Number
Less Than 12
Algorithm Description
1. Start
2. Setup variables: Count, Number, Factorial
3. Initialize variables:
Count:=0;Number:=0;Factorial:=1;
4. Read a Number
5. Check exceptions: Number<0(advice and ask
again); Number>12(advice and ask again),
Number:=0 or 1 (Factorial:=1);
6. If Number<2 then proceed to 10
7. Add 1 to Count
8. Factorial := Factorial * Count
9. If Count<Number Repeat from 7
10.Display Factorial
31
11.Stop
Algorithms
 Algorithm For Computing The Factorial Of A
Number Less Than 12

32
ALGORITHMS …
 Algorithm For Computing The Factorial Of A
Number Less Than 12
flowchart

33
ALGORITHMS …
 Algorithm For Computing The Factorial Of A Number
Less Than 12
Pseudocode
START
Variables: Count, Number, Factorial
Count:=0;
Number:=-10;
Factorial:=1;
DO WHILE (Number <0 or Number>12)
PROMPT ‘Enter a number between 0 and 12’
READ Number
If Number <0 or Number>12
WRITE ‘Number should be 0..12’
ENDWHILE 34
ALGORITHMS …

DO WHILE (Number>1 and Count<Number)


Count := Count + 1;
Factorial := Factorial * Count;
ENDWHILE
WRITE Factorial;
STOP

35
Top-down design (stepwise refinement)
 If we look at a problem as a whole, it may seem
impossible to solve because it is so complex. Examples:
- Writing a tax computation program
- Writing a word processor
Definitions of Stepwise Refinement
1. It is a divide and conquer approach to programming, in
which a complex problem is recursively divided into
smaller, more manageable, sub-problems.
2. The development of a program by breaking the original
problem into smaller sub-problems and then applying the
same process to each sub-problem.
3. The process of repeatedly subdividing a problem into
smaller sub-problems until a detailed algorithm for
solving the problem emerges. 36
Top-down design (stepwise refinement)
1. Start with what you want
2. Cut it up in parts
3. If the parts are trivial to solve, then solve them
4. If the parts are non trivial, apply top-down again
 You will cut up the problem in smaller and smaller
sub-problems until they can be solved trivially.

37
Top-down design (stepwise refinement)
Advantages of stepwise refinement
1. There is a separation of what is supposed to happen
and what is next
2. Modular program production where each module is
developed independently
3. The maintenance of your program is made easy
4. Incremental progress
5. The design is easy to modify and change

38
Top-down design (stepwise refinement)
Example
Getting the surface area of a closed cylinder
• Get the area of the bottom and the top sides
• Get the area of the curved surface

1.1 Get the radius of the bottom side


1.2 square the radius and multiply by PI to
get the area of the bottom side
1.3 Multiply the area of the bottom side by
two to get the area of the top and bottom
side
39
Top-down design (stepwise refinement)
Example
Getting the surface area of a closed cylinder
• Get the dimensions of the cylinder
• Get the area of the bottom and the top sides
• Get the area of the curved surface
• Get the total surface area

1.1 Get the radius of the cylinder


1.2 Get the height of the cylinder

2.1 square the radius and multiply by PI to


get the area of the bottom side
40
Top-down design (stepwise refinement)
Example
Getting the surface area of a closed cylinder
2.2 Multiply the area of the bottom side by
two to get the area of both the top and
bottom sides
3.1 Get the circumference of the cylinder by
multiplying the radius by two then
multiplying the product by PI
3.2 Multiply the circumference of the
cylinder with the height to get the area
of the curved surface
4.1 Add the area of the top and bottom side to
the area of the curved surface to get the
total area 41

You might also like