You are on page 1of 60

Algorithms

CONCEPT
Figure 8-1
Informal definition of an algorithm
used in a computer
Figure 8-2
Finding the largest integer
among five integers
Figure 8-3
Defining actions in FindLargest algorithm
Figure 8-4
FindLargest refined
Figure 8-5
Generalization of FindLargest
8.2

THREE CONSTRUCTS
Figure 8-6
Three constructs
8.3

ALGORITHM
REPRESENTATION
Figure 8-7
Flowcharts for three constructs
Figure 8-8
Pseudocode for three constructs
Example 1

Write an algorithm in pseudocode that finds


the average of two numbers

Solution
See Algorithm 8.1 on the next slide.
Algorithm 8.1: Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Example 4

Write an algorithm to find the largest of a set


of numbers. You do not know the number of
numbers.
Solution
See Algorithm 8.4 on the next slide.
Algorithm 8.4: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 if (the integer is greater than Largest)
then
2.1.1 Set largest to the value of the
integer
End if
End while
3. Return Largest
End
Example 5

Write an algorithm to find the largest of


1000 numbers.

Solution
See Algorithm 8.5 on the next slide.
Algorithm 8.5:Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
8.5

SUBALGORITHMS
Figure 8-9
Concept of a subalgorithm
Algorithm 8.6: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 FindLarger
End while
3. Return Largest
End
Subalgorithm: Find larger
FindLarger
Input: Largest and current integer
1. if (the integer is greater than Largest)
then
1.1 Set Largest to the value of the integer
End if
End
8.6

BASIC
ALGORITHMS
Figure 8-10
Summation
Figure 8-11
Product
Figure 8-12
Selection sort
Figure 8-13: part I
Example of selection sort
Figure 8-13: part II
Example of selection sort
Figure 8-14
Selection sort
algorithm
Flowchart:

What is a Flowchart?
 The flowchart is a means of visually presenting the flow
of control through an information processing systems,
the operations performed within the system and the
sequence in which they are performed.
 It is a graphic representation of how a process works,
showing, at a minimum, the sequence of steps.
 Flowcharts are generally drawn in the early stages of
formulating computer solutions.
Flowchart (Contd…):
Guideline for drawing a flowchart:
Flowcharts are usually drawn using some standard
symbols; Some standard symbols, which are frequently
required for flowcharting many computer programs are
shown below
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.

Process symbol - shows an instruction other than


input, output or selection.

Input-output symbol - shows an input or an output


operation.

Disk storage I/O symbol - indicates input from or output to


disk storage.

Printer output symbol - shows hardcopy printer


output.

Principles of Programming - NI
July 2005 32
Flowchart Symbols cont…
Selection symbol - shows a selection process
for two-way selection.

Off-page connector - provides continuation of a


logical path on another page.

On-page connector - provides continuation


of logical path at another point in the same
page.

Flow lines - indicate the logical sequence of


execution steps in the algorithm.

Principles of Programming - NI
July 2005 33
Flowchart (Contd…):
A set of useful standard Flowchart
symbols:
 Rounded box
use it to represent an event which occurs automatically.
 Rectangle or box
use it to represent an event which is controlled within
the process. Typically this will be a step or action which
is taken.
 Diamond
use it to represent a decision point in the process.
 Circle
use it to represent a point at which the flowchart
connects with another process.
ADVANTAGES OF USING
FLOWCHARTS:
 Communication: Flowcharts are better way of
communicating the logic of a system
 Effective analysis: Problem can be analyzed in more
effective way.
 Proper documentation: Flowcharts serve as a good
program documentation
 Efficient Coding: Flowcharts act as a guide or blueprint
during the systems analysis and program development
phase.
ADVANTAGES OF USING
FLOWCHARTS (Contd…):
 Proper Debugging: Flowchart helps in debugging
process.

 Efficient Program Maintenance: The maintenance of


operating program becomes easy with the help of
flowchart.
Flow chart of the while loop :
Flow chart of the for loop:
The flow chart of the if statement:
The flow chart of the if…else statement:
The flow chart of the switch statement:
Flowchart for finding the sum of first five natural numbers
( i.e. 1,2,3,4,5):
Flowchart (Example):
Flowchart to find the sum of first 50 natural numbers.
Flow Chart to find largest of two numbers:

Start

Read A, B

Yes No
Is A > B

Print A Print B

End
Flowchart to find the largest of
three numbers A,B, and C:

NO
LIMITATIONS OF USING
FLOWCHARTS:
 Complex logic: Sometimes, the program logic is quite
complicated. In that case, flowchart becomes complex
and clumsy.

 Alterations and Modifications: If alterations are


required the flowchart may require re-drawing
completely.

 Reproduction: As the flowchart symbols cannot be


typed, reproduction of flowchart becomes a problem.
Pseudocode
 Pseudocode is a shorthand notation for programming which uses a
combination of informal programming structures and verbal
descriptions of code.
 In general, pseudocode is used to outline a program before
translating it into proper syntax. This helps in the initial planning of a
program, by creating the logical framework and sequence of the code.
An additional benefit is that because pseudocode does not need to
use a specific syntax, it can be translated into different programming
languages and is therefore somewhat universal. It captures the logic
and flow of a solution without the bulk of strict syntax rules.
 Pseudocode is an artificial and informal language that helps
programmers develop algorithms. Pseudocode is very similar to
everyday English.
Pseudocode & Algorithm

 Example 1: Write an algorithm to determine


a student’s final grade and indicate whether it
is passing or failing. The final grade is
calculated as the average of four marks.
Pseudocode & Algorithm

Pseudocode:
 Input a set of 4 marks

 Calculate their average by summing and dividing


by 4
 if average is below 50

Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
 Detailed Algorithm
 Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
example
Design with
Structure Charts
Design Process

 Problem solving and design should be done


independent of programming.
 Code significantly clouds the design
process.. It is very difficult to see the design
and address design issues when coding
 Design needs to take place in a mode which
minimizes code influence
4 Steps
1 2

Create a structure chart Modify structure chart


based on the main based on the
item, programming
NOT programming requirements.
requirements Top-down design.

4 3

Write code to
Improve readabililty implement the
of the code by top-down design.
incorporating As you mature this
functions. will be skipped by
going to step 4.
National Parcel Example
Example 2 : Parcel Service Company.
Emphasizes IF notation for top down design.
National Parcel Service (NPS) specializes in nationwide delivery of small packages. NPS will not accept
any packages whose largest dimension is greater than 3 feet or whose weight exceeds 50 pounds.
The charge for shipping a parcel is $0.75 plus an amount based on package weight as follows:
Weight (lb.) Rate
-----------------------------
20 or less $0.08 per lb.
40 or less $0.10 per lb.
Over 40 $0.15 per lb.
-----------------------------
There is an additional $1.00 charge if the volume of the package exceeds 18 cubic feet. Write a program
that will read the dimensions of a parcel (in feet) and its weight (in pounds) and then compute and print
the postage due. If the package is rejected, an appropriate message should be printed.

Example Test Data Length Width Depth Weight


1.5 1.2 0.8 6.0
Parcel Delivery
National Parcel Service

Parcel *

Size Weight Shipping Status

Length Width Depth Acceptable Not Acceptable

Charge

Weight Charge Volume Charge 0.75

Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18


Change in Perspective

 Our natural view of the problem domain


may not match the requirements.
 There are multiple views of anything.
 Other views are not wrong, but do provide
insight into the structure we expect and
perceive.
 In this problem we need to adjust for a
single parcel.
Parcel Delivery
(adjust to one package)

Parcel

Size Weight Shipping Status

Length Width Depth Acceptable Not Acceptable

Charge

Weight Charge Volume Charge 0.75

Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18


Step 2

Adapt the structure chart


to accommodate the
program specifications:
DO A TOP-DOWN DESIGN
Parcel Delivery
Adjusting Structure Chart for Program (Top-Down) Design

Compute Postage

Input Package Info Determine Acceptability

Size Weight Acceptable Not Acceptable

Length Width Depth Output Rejection


Compute Charge Output Charge
Message

Compute Compute Compute


Weight Charge Volume Charge Total Charge

Charge=
Wt<=20 20<Wt<=40 Wt>40 Vol<=18 Vol>18 Wt_Chrg+
Vol_Chrg+
0.75
Wt_chrg= Wt_chrg= Wt_chrg= Vol_chrg= Vol_chrg=
Wt*0.08 Wt*0.1 Wt*0.15 0.0 1.0

You might also like