Algorithms, Flowcharts & Program Design

advertisement
Algorithms, Flowcharts &
Program Design
ComPro
Definition
• Algorithm:
o sequence of steps to be performed in order to solve a
problem by the computer.
• Flowchart:
o graphical or symbolic representation of an algorithm
(diagrammatic representation of the step-by-step
solution to a given problem).
• Program Design:
o steps a programmer should do before they start
coding the program in a specific language.
o Proper program design helps other programmers to
maintain the program in the future.
Example
• The call-me algorithm:
o When your train arrives, call my mobile
phone.
o Meet me outside the railway station.
• There are often many different algorithms
to accomplish any given task.
• Each algorithm has advantages and
disadvantages in different situations.
Reason Using Algorithm
• Efficiency: time, cost
• Abstraction: complicated problems can be
distilled into simpler ones for which
wellknown algorithms.
• Reusability: Algorithms are often reusable
in many different situations.
Expressing Algorithms
• many different notations: natural
languages, pseudocode, flowcharts.
o Natural language: tend to be verbose and
ambiguous (rarely used for complex
algorithm).
o Pseudocode and flowcharts: are structured
ways, avoid many ambiguities common in
natural language statements.
Sometimes it is helpful in the description of an algorithm to supplement small
flowcharts with natural language and/or arithmetic expressions written inside
block diagrams to summarize what the flowcharts are accomplishing.
Example
• Algorithm using natural language statements:
o Assume the first item is largest.
o Look at each of the remaining items in the list and if it is
larger than the largest item so far, make a note of it.
o The last noted item is the largest item in the list when the
process is complete.
• Algorithm using pseudocode:
largest = La
for each item in the list (Length (L)  1), do
if the item  largest, then
largest = the item
return largest
Benefits of Using Algorithms
• Reduces the task into a series of smaller
steps of more manageable size.
• Problems can be approached as a series
of small, solvable sub-problems.
• Efficient.
Flowcharts
• Flowchart is a type of diagram (graphical or
symbolic) that represents an algorithm or
process.
• Each step in the process is represented by a
different symbol and contains a short
description of the process step.
• The flow chart symbols are linked together
with arrows showing the process flow
direction.
• A flowchart typically shows the flow of data in
a process.
Why Flowcharts
• Flowcharts are used in analyzing,
designing, documenting or managing a
process or program in various fields.
• Flowcharts are generally drawn in the
early stages of formulating computer
solutions.
• Flowcharts often facilitate communication
between programmers and business
people.
Example
• Read X, Y, Z
• Compute Sum (S) as
X+Y+Z
• Compute Average (A)
as S / 3
• Compute Product (P)
as X x Y x Z
Advantages of Using Flowcharts
• Communication: a better way of logical
communicating.
• Effective analysis: problem can be analysed in
more effective way.
• Proper documentation.
• Efficient Coding: a guide or blueprint during the
systems analysis and program development
phase.
• Proper Debugging: helps in debugging process.
• Efficient Program Maintenance: easy with the help
of flowchart
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 redrawing completely.
• Reproduction: As the flowchart symbols
cannot be typed, reproduction of flowchart
becomes a problem.
Flowchart Symbols
•
•
•
•
•
•
•
Terminator: “Start” or “End”
Process
Decision: Yes/No question or True/False
Connector: jump in the process flow
Data: data input or output (I/O)
Delay
Arrow: flow of control in a process.
Basic Symbol
Name
Symbol
Use in Flowchart
Oval
Denotes the beginning or end of the program
Parallelogram
Denotes an input operation
Rectangle
Denotes a process to be carried out
e.g. addition, subtraction, division etc.
Diamond
Denotes a decision (or branch) to be made.
The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)
Hybrid
Denotes an output operation
Flow line
Denotes the direction of logic flow in the program
Example
•
•
•
•
•
•
•
Terminator
Process
Decision
Connector
Data
Delay
Arrow
Example
•
•
•
•
•
•
•
Terminator
Process
Decision
Connector
Data
Delay
Arrow
Ex. : convert the length in feet to centimeter
Algorithm:
Flowchart
o Step 1: Input Lft
o Step 2: Lcm  Lft x 30
o Step 3: Print Lcm
Pseudocode:
o Input the length in feet (Lft)
o Calculate the length in cm (Lcm) by
multiplying LFT with 30
o Print length in cm (LCM)
START
Input
Lft
Lcm  Lft x 30
Print
Lcm
STOP
Ex. : read the two sides of a rectangle
and calculate its area
START
Algorithm
o Step 1: Input W,L
o Step 2: A  L x W
o Step 3: Print A
Pseudocode
Input
W, L
ALxW
Print
A
STOP
o Input the width (W) and Length (L) of a rectangle
o Calculate the area (A) by multiplying L with W
o Print A
Ex. : calculate the roots of a quadratic equation
ax2  bx  c  0
• Equation:
• Calculate: d = sqrt (b2  4ac), and
• Roots:
o x1 = (–b + d)/2a and
o x2 = (–b – d)/2a
Ex. Roots of Quadratic Eq.
• Algorithm:
o
o
o
o
o
Step 1: Input a, b, c
Step 2: d=sqrt (bb  4  a  c)
Step 3: x1=(–b + d) / (2 x a)
Step 4: x2=(–b – d) / (2 x a)
Step 5: Print x1, x2
• Pseudocode:
o
o
o
o
o
Input the coefficients (a, b, c)
Calculate d
Calculate x1
Calculate x2
Print x1 and x2
START
Input
a, b, c
d  sqrt(b x b – 4 x a x c)
x1 (–b + d) / (2 x a)
X2  (–b – d) / (2 x a)
Print
x1 ,x2
STOP
Ex.
START
• Algorithm:
Input
VALUE1,VALUE2
o?
• Pseudocode:
o?
Y
N
is
VALUE1>VALUE2
MAX  VALUE1
MAX  VALUE2
Print
“The largest value is”, MAX
STOP
Program Design
• The phase of program development:
o Determination of hardware and software
resources needed by the program are
identified and the logic to be used by the
program.
• Program Design consists of the steps a
programmer should do before they start
coding the program in a specific language.
Activities in Program Design
• Understanding the Program (user need) –
System Analyst
o variety of documentation items (include
screen layouts, narrative descriptions,
documentation showing, the processing
steps, etc.
o Understanding the purpose of a program
(inputs, processing, outputs)
• Using Design Tools to Create a Model
• Develop Test Data
Test Data
• Test data consists of the user providing
some input values and predicting the
outputs.
• This can be quite easy for a simple
program and the test data can be used to
check the model to see if it produces the
correct results.
Components of Program Design
• Problem definition, leading to a program
specification,
• Modular program design, which refines the
specification,
• Module composition, which translates
specification into executable program,
• Module/program evaluation and testing,
during which you refine the program and find
errors,
• Program documentation, which pervades all
other phases.
Approach
• The two most common ones are
“procedural programming” and “objectoriented programming”.
• Procedures may use objects, and objects
usually use procedures, called methods.
• The object-oriented code takes more
planning and is significantly larger, but it is
generally accepted to be easier.
Approach
Object-Oriented Formulations
• The process of creating an “objectoriented” (OO) formulation in program
design involves at least three stages:
o Object-Oriented Analysis (OOA),
o Object-Oriented Design (OOD), and
o Object-Oriented Programming (OOP).
Object-Oriented Analysis
• Find objects and classes
o Create an abstraction of the problem domain.
o Give attributes behaviours, classes, and objects
meaningful names.
o Identify structures pertinent to the system’s complexity and
responsibilities.
o Observe information needed to interact with the system, as
well as information to be stored.
o Look for information re-use; are there multiple structures;
can sub-systems be inherited?
• Define the attributes
• Define the behavior
• Diagram the system
Object-Oriented Analysis
• Find objects and classes
• Define the attributes
o
o
o
o
o
o
o
o
Select meaningful names.
Describe the attribute and any constraints.
What knowledge does it possess or communicate?
Put it in the type or class that best describes it.
Select accessibility as public or private.
Identify the default, lower and upper bounds.
Identify the different states it may hold.
Note items that can either be stored or re-computed.
• Define the behavior
• Diagram the system
Object-Oriented Analysis
• Find objects and classes
• Define the attributes
• Define the behavior
o
o
o
o
o
o
o
o
Give the behaviours meaningful names.
What questions should each be able to answer?
What services should it provide?
Which attribute components should it access?
Define its accessibility (public or private).
Define its interface prototype.
Define any input/output interfaces.
Identify a constructor with error checking to supplement the
intrinsic constructor.
o Identify a default constructor.
• Diagram the system
Object-Oriented Analysis
•
•
•
•
Find objects and classes
Define the attributes
Define the behavior
Diagram the system
o Employ an OO graphical representation.
Object-Oriented Design
• Improve and add to the OOA results during OOD.
• Divide the member functions into constructors, accessors, agents
and servers.
• Design the human interaction components.
• Design the task management components.
• Design the data management components.
• Identify operators to be overloaded.
• Identify operators to be defined.
• Design the interface prototypes for member functions and for
operators.
• Design code for re-use through “kind of” and “part of” hierarchies.
• Identify base classes from which other classes are derived.
• Establish the exception handling procedures for all possible errors.
Object-Orientedness
• Object-based modular structure:
o Systems are modularized on the basis of their data structure.
• Data Abstraction:
o Objects should be described as implementations of abstract data types.
• Automatic memory management:
o Unused objects should be de-allocated by the language system.
• Classes:
o Every non-simple type is a module, and every high-level module is a
type.
• Inheritance
o A class may be defined as an extension or restriction of another.
• Polymorphism and dynamic binding:
o Entities are permitted to refer to objects of more than one class and
operations can have different realizations in different classes.
• Multiple inheritances:
o Can declare a class as heir to more than one class.
Summary
• Algorithm is the sequence of steps to be
performed in order to solve a problem by
the computer.
• Three reasons for using algorithms are
efficiency, abstraction and reusability.
• Algorithms can be expressed in many
different notations, including natural
languages, pseudocode, flowcharts and
programming languages.
Summary
• Analysis of algorithms is the theoretical
study of computer program performance
and resource usage, and is often practised
abstractly without the use of specific
programming language or implementation.
• The practical goal of algorithm analysis is
to predict the performance of different
algorithms in order to guide program
design decisions.
Summary
• Most algorithms do not perform the same
in all cases; normally an algorithm’s
performance varies with the data passed
to it.
• Typically, three cases are recognized: the
best case, average case and worst case.
• Worst case analysis of algorithms is
considered to be crucial to applications
such as games, finance and robotics.
Summary
• Flowchart : diagrammatic representation of
the step-bystep solution to a given problem.
• Flowcharts are used in analyzing, designing,
documenting or managing a process or
program in various fields.
• Benefits of using flowcharts include ease of
communication, effective and efficient
analysis and coding, proper documentation
and maintenance.
• Limitations of using flowcharts include
complex logic and multiple modifications.
Summary
• Program Design consists of the steps a programmer
should do before they start coding the program in a
specific language.
• Program design must be extremely structured, having
the ultimate intentions of performing a specific
calculation efficiently with attractive, understandable,
efficient programs.
• Three broad areas of activities in program design: (1)
Understanding the Program, (2) Using Design Tools to
Create a Model and (3) Develop Test Data
• The process of creating an OO formulation in program
design involves at three stages: 1) OOA, 2) OOD, and
3) OOP.
Download