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

New 05-Algorithm Design and Problem Solving

Uploaded by

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

New 05-Algorithm Design and Problem Solving

Uploaded by

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

ALGORITHM DESIGN

AND PROBLEM SOLVING


LECTURE 5

McGraw-Hill ©The McGraw-Hill Companies, Inc., 2000


Objectives

Define an algorithm and relate it to problem solving.

❑ Define the three constructs—sequence, selection, and


repetition—and describe their use in algorithms.


Describe pseudocode and how it can be used to represent
algorithms.


Describe flowcharts and how they can be used to represent
algorithms.
• Please refer to your textbook
• Chapter 8 Algorithms
PROBLEM SOLVING

• Problem solving is the process of


• identifying a problem,
• developing possible solution paths, and
• taking the appropriate course of action.

1.4
PROBLEM SOLVING
• Problem solving is a process.
• Most strategies provide steps that help you identify the
problem and choose the best solution.
• Problem Solving steps
• Identify the problem
• Define the context of the problem
• Explore possible strategies
• Act on best solution
• Look back and learn

1.5
PROBLEM SOLVING

Top Down Design


• Top down design is essentially the process of breaking down
a computer system into lots of sub-systems. Each of these
sub-systems can then be broken down into more sub-
systems with the process continuing as necessary.
• This is an effective way of designing and developing a
computer system as it breaks a large problem down into
smaller more manageable problems that can be tackled one
at a time.

1.6
PROBLEM SOLVING

Top Down Design


• There are a few benefits of designing a program this way,
these include:
• Breaking the problem into parts helps clarify what needs to be done.
• At each step of refinement, the new parts become less complicated
and easier to figure out.
• Parts of the solution may become reusable.
• Developers can code and test each sub-system separately
• Multiple developers can be tasked with coding different sub-systems
at the same time which should result in the program being created
more efficiently.

1.7
PROBLEM SOLVING

1.8

Top Down Design Example Scenario


PROBLEM SOLVING
• There are two basic problem solving strategies:
algorithmic and heuristic.
• Algorithmic provides you with step-by-step instructions used to
achieve a desired outcome. You can think of an algorithm as a
recipe with highly detailed instructions that produce the same
result every time they are performed.

• Heuristic is an algorithm that is able to produce an acceptable


solution to a problem in many practical scenarios, in the
fashion of a general heuristic, but for which there is no formal
proof of its correctness. Heuristics is more generally and
technically used in AI.

1.9
You will study algorithmic problem solving in this course
ALGORITHM
• The word algorithm comes from the name of the 9th century
Persian and Muslim mathematician Abu Abdullah
Muhammad ibn Musa Al-Khwarizmi.
• A computer program makes use of algorithms.
• An algorithm is a step-by-step method for solving a problem
or doing a task.
• A more formal definition: An algorithm is an ordered set of
unambiguous steps that produces a result and terminates in
a finite time.

1.10
ALGORITHM

Informal definition of an algorithm used in a


computer
ALGORITHM

Finding the largest integer among 5 integers


ALGORITHM

Please refer to your textbook


Behrouz, A. Forouzan and De Anza College,
“Foundations of Computer Science”, 4th Edition,
Cengage Learning

section 8.1.2 page 214 for more elaboration on


the previous example

1.13
ALGORITHM
• An algorithm can be represented using pseudocodes (or)
flowcharts.
• Pseudocodes is a method of describing computer algorithms
using a combination of natural language and programming
language. It is essentially an intermittent step towards the
development of the actual code. It allows the programmer
to formulate their thoughts on the organization and
sequence of a computer algorithm without the need for
actually following the exact coding syntax.
• Flowcharts are written with program flow from the top of a
page to the bottom. Each command is placed in a box of the
appropriate shape, and arrows are used to direct program
1.14
flow.
ALGORITHM

• Three Constructs of 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), and 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.
ALGORITHM

1.16

Three Constructs
PSEUDOCODES

• Pseudocode is an English-language-like representation


of an algorithm.
• There is no standard for pseudocode- some people use
a lot of detail, other use less.
• Some use a code that is close to English, while other
syntax like the Pascal programming language.

1.17
PSEUDOCODES

Pseudocode: Calculating the sum of two integers


1.18
PSEUDOCODES

Pseudocode: Assigning pass/no grade 1.19


PSEUDOCODES

Pseudocode: Assigning a letter grade 1.20


PSEUDOCODES

Pseudocode: Finding the largest integer 1.21


PSEUDOCODES

Pseudocode: Finding the smallest integer among 1.22


1000 integers
PSEUDOCODES

Pseudocode: Iteration solution to factorial 1.23


FLOWCHARTS

• Program flowcharts use geometric symbols and familiar


relational operators to graphically portray the sequence
of steps involved in a program.

1.24
Flowchart symbols Relational operators
FLOWCHARTS
Standard Symbols Used

Print
Start
Results A
On-Page
Input/Output Connector
Start Or Stop Process

Calculate Yes
Variables ? A1

Processing Rectangle No Off-page


Connector
Condition
McGraw-Hill ©The McGraw-Hill Companies, Inc., 2000
FLOWCHARTS

1.26
A simple flowchart to read two
numbers and print their sum
FLOWCHARTS

Basic Flowchart Operations


• All computer instructions are based on four basic
processing patterns:
1. Simple Sequence
2. Selection Pattern
3. Loop Pattern
4. Branch Pattern
FLOWCHARTS
1.Simple Sequence

• Logic involves executing instructions one


statement after another, in the order presented
by the program.
• This is the simplest and most-used pattern.
• The computer assumes that all instructions are
to be executed in this order unless the program
presents other instructions.
FLOWCHARTS

An example of simple sequence


FLOWCHARTS
2. Selection Pattern

•Requires that the computer make a choice


among two or more items.

• Each choice is based on one of two


comparisons a computer can make: true
or false (Yes or No).
FLOWCHARTS

An example of selection pattern


FLOWCHARTS
3. Loop Pattern

• Causes an interruption in the normal sequence


of processing and directs the computer to loop
back to a previous statement in the program,
repeating the same sequence over again,
usually with new data.
• Bylooping, the programmer avoids having to
repeat the same set of instructions over and
over
FLOWCHARTS

An example of loop pattern


FLOWCHARTS
4. Branch Pattern

• Is often used in combination with selection or


looping.
• The branch pattern allows the computer to skip
statements in a program.
• The branch encourages undisciplined jumping
around among program statements, a
characteristic that is frowned on by most
programming experts.
• Branching is difficult to follow and is an
inefficient use of computer power.
FLOWCHARTS

Honour No
1
Student?

Yes

Read Area of Concentration

Read Courses
Taken
1

Compare with Criteria

An example of a branch pattern


FLOWCHARTS

• Uses of flowcharts
▪ Flowcharts play a vital role in the programming of a
problem and are quite helpful in understanding the
logic of complicated and lengthy problems.
▪ Once the flowchart is drawn, it makes it easy to write
the program in any high level language.
▪ Often we see how flowcharts are helpful in explaining
the program to others.

Hence, it is correct to say that a flowchart is a


must for the better documentation of a complex
program.
FLOWCHARTS

• Uses of flowcharts
▪ Flow Charts document processes and interrelationships
of process steps
▪ FlowCharts identify actual and ideal paths where any
product or process flows
▪ Flow Charts are used to identify problems and potential
improvements
▪ FlowCharts can be completed on entire processes
assemblies with all components, one person or
component through a process, combinations of people
and machines, transactions following forms or other
documents, etc.
FLOWCHARTS
• Advantages of flowcharts
▪ Communication: Flowcharts are better way of communicating
the logic of a system to all concerned.
▪ Effective Analysis: With the help of flowchart, problem can be
analysed in more effective way.
▪ Proper Documentation: Program flowcharts serve as a good
program documentation, which is needed for various
purposes
▪ Efficient Coding: The flowcharts act as a guide or blueprint
during the systems analysis and program development phase.
▪ Proper Debugging: The flowchart helps in debugging process.
▪ Efficient Program Maintenance: The maintenance of
operating program becomes easy with the help of flowchart.
It helps the programmer to put efforts more efficiently on
that part.
FLOWCHARTS

• Disadvantages of flowcharts
▪ Complex Logic: Sometimes, the program logic is
quite complicated. In that case, flowchart becomes
complex and clumsy.
▪ Alterationsand 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.
The Essentials of what Is Done can easily be lost in
the technical details of how it is done.
Putting it all together

1.40
SUMMARY AND DISCUSSION

1.41

You might also like