Upload
scott-moore
View
213
Download
1
Tags:
Embed Size (px)
Citation preview
Flowcharts and Algorithms
Review of Terms
A computer is a machine that can represent and manipulate data
– Ultimately the data and the instructions for manipulating it will be stored in binary
A program is an algorithm, expressed in a language the computer can understand
For example Pascal, Java, Visual Basic
The Two Phases of ProgrammingThe Two Phases of Programming
The task of programming can be divided into
two distinct phases:
– The Problem Solving Phase
• When we think about the problem and design an algorithm to help us solve it
–The Implementation (Coding) Phase
• When we translate the algorithm into a programming language
Algorithm Design Tool: PseudocodeAlgorithm Design Tool: Pseudocode
We have used pseudocode to design algorithms
Pseudocode:
– Is a generic way of describing an algorithm, without use of any specific programming language
– Helps programmers to plan an algorithm
– Not an actual programming language, but uses programming constructs available in all popular programming languages
Writing Pseudocode AlgorithmsWriting Pseudocode Algorithms
We need to be certain that the problem we are
solving is fully understood
Questions to ask:
– What data is known before the program runs?
– What data must be input by the user?
– What computations will be performed on the data?
– What data will be output to the user?
ExampleExample
Real World Problem:
– Calculate your two childrens’ pocket money, based upon one euro per year old.
Known Values
– Rate = one euro per year Inputs
– Ages of children Calculations
– Pocket_Money = Age x Rate Outputs
– Pocket_Money for each child
Pseudocode AlgorithmPseudocode Algorithm
Obtain Age of Child1
Obtain Age of Child2
Pocket_Money for Child1 = Age of Child1 x Rate
Pocket_Money for Child2 = Age of Child2 x Rate
Display Pocket_Money for Child1 and Child2
Algorithm including SelectionAlgorithm including Selection
Real World Problem:– Calculate one child’s Pocket Money, based upon one euro per
year old if s/he is under 10, and two euros per year if 10 or over.
Known Values– YoungRate = one euro per year– OlderRate = two euros per year– BreakAge = 10
Inputs– Age of child
Calculations– Pocket_Money = Age x Rate
Outputs– Pocket_Money
Pseudocode AlgorithmPseudocode Algorithm
Obtain Childs_Age
IF Childs_Age less than 10
THEN Pocket_Money = Age x YoungRate
ELSE Pocket_Money = Age x OlderRate
Display Pocket_Money
Algorithm including a LoopAlgorithm including a Loop Real World Problem:
– Calculate the total Pocket Money paid to each of three children at one euro per year old.
Known Values– Rate = one euro per year– NumChildren = 3
Inputs– Ages of children
Calculations– Pocket_Money = Age x Rate
Outputs– Total Pocket Money paid
Pseudocode AlgorithmPseudocode Algorithm
Set NumOfChildrenPaid to 0
Set TotalPocketMoneyOwed to 0
WHILE NumOfChildrenPaid < 3 DO
Obtain Childs_Age
Pocket_Money = Age x Rate
ADD Pocket_Money to TotalPocketMoneyOwed
ADD 1 to NumOfChildrenPaid
ENDWHILE
DISPLAY Total
AbstractionAbstraction
We use procedures (or mini-algorithms) to break the solution into meaningful “chunks”
If NumOfChildren > 1
ThenCalculatePocketMoneyforManyChildren
ElseCalculatePoketMoneyforOneChild
Endif
Each procedure is then described in pseudocode
Flowcharts as an Alternative to Flowcharts as an Alternative to PseudocodePseudocode
Perhaps a picture is worth a thousand words Sometimes it is easier to follow an algorithm if it is
represented in a diagram Program flowcharts are widely used for this
purpose
Example FlowchartExample Flowchart
Flowchart SymbolsFlowchart Symbols
Rectangle is used for actions
Diamond is used for decisions
Symbols are connected by
arrows to represent the
order of the actions
Flowcharting Control StructuresFlowcharting Control Structures
IF Statements
IF statements are used to cause certain actions to occur
within a program when specific conditions are met.
The simplest is the If/Then statement:
If Balance < 0 Then Display "You are overdrawn“
Here, if the Balance is less than zero, the message “You
are overdrawn” is shown.
IF exampleIF example
“You are overdrawn”Balance<0yesP
rog
ram flo
w
If/Then/EndIfIf/Then/EndIf
You can allow multiple statements by using If/Then/End If
Example
If Balance < 0 Then
Display "You are overdrawn“
Display "Authorities have been notified“
End If
In this case, if Balance is less than zero, the two lines of
information are shown (one at a time!)
If/Then/EndIf exampleIf/Then/EndIf example
“You are overdrawn”Balance<0yes
“Authorities have been notified”
End If
Pro
gram
flow
If/Then/Else/EndIfIf/Then/Else/EndIf
When more than one result needs to be catered for we can use-
If/Then/Else/EndIf blocks: If Balance < 0 Then
Display "You are overdrawn"
Display "Authorities have been notified"
Else
Display " Your balance is " , Balance
EndIf
Here, the same two lines are printed if you are overdrawn (Balance < 0), but, if you are not overdrawn (Else), a message indicating your Balance is displayed.
If/Then/Else/EndIf exampleIf/Then/Else/EndIf example
“You are overdrawn”Balance<0
yes
“Authorities have been notified”
End If
“Your balance is £x”
no
End If
ElseIfElseIf
Or, we can add the ElseIf statement:If Balance < 0 Then
Display "You are overdrawn"
Display "Authorities have been notified"
ElseIf Balance = 0 Then
Display "You have no money left"
Else
Display (" Your balance is “, Balance)
End If
Now, one more condition is added.
If your Balance = 0, a different message appears.
If/Then/Else/EndIf exampleIf/Then/Else/EndIf example
“You are overdrawn”Balance<0
yes
“Authorities have been notified”
“Your balance is £x”
no
End If
Balance=0
no
“You have no money left”
yes
End If End If
Flowchart for Pocket Money ExampleFlowchart for Pocket Money Example
Previous Pseudocode Algorithm:
PROMPT for Age
READ Age
IF Age less than 10
THEN Pocket_Money =
Age x YoungRate
ELSE Pocket_Money =
Age x OlderRate
ENDIF
DISPLAY Pocket_Money
PM = Age x YoungRate
start
Print PM
stop
prompt Age
PM = Age x OlderRate
Age < 10 FALSETRUE
input Age
NoteNote
In using branching statements, make sure you consider all
viable possibilities in the If/Else/End If structure.
Also, be aware that each If and ElseIf in a block is tested
sequentially.
The first time an If test is met, the code associated with that
condition is executed and the If block is exited.
If a later condition is also True, it will never be considered.
And /OrAnd /Or
You may want to test conditions that are combined in different ways.
AND
For example
You want to print “Accepted” if the amount in an account is greater than £100 and the number of bad debts is zero.
This would be written like this:If Balance > 100 AND BadDebtsNo = 0 then Print “Accepted”
Only if both conditions are met will the message be printed.
And /OrAnd /Or
OR
The OR statement is more tolerant and will allow a condition to be met if either of two conditions are met.
For example
A person can have a discount of 20% in a store if they are over 64 years of age or have a club card.
This would be written:
If Age > 64 OR Card = True then Discount = 0.20
These conditions can be incorporated into IF/Then/Else/EndIf and If/Then/ElseIf/Else/EndIf statements
Nested StructuresNested Structures
Control structures can be nested; that is one can be placed within another.
Consider the flow diagram below.
Enter Name
Type of drink
Cost = £0.75
Tea
Type of coffee
Cost = £1.75
Latte
Cost = £2.15
Espresso
Coffee
Looping StructuresLooping Structures
Times = 0
Times = Times + 1
Do this
Do that
Does Times = 5
End
Yes
No
A looping structure to repeat operations five times
Different Types of LoopDifferent Types of Loop
While conditions do begin : statements / commands : end;
Repeat : statements / commands :
Until condition;
Previous Pseudocode Algorithm:
Set KidsPaid to 0
Set Total to 0
WHILE KidsPaid < 3 DO
PROMPT for Age
READ Age
PocketMoney = Age x Rate
ADD PocketMoney to Total
ADD 1 TO KidsPaid
ENDWHILE
DISPLAY Total
start
Display Total
stop
Read Age
Calc PM
KidsPaid < 3FALSETRUE
KidsPaid = 0
Total = 0
Add PM to Total
Increment KidsPaid
Prompt Age
Flowchart for Pocket Flowchart for Pocket Money ExampleMoney Example
ExerciseExercise
Take another look at the problems in the pseudocode tutorial – develop flowcharts to solve the various problems we considered there
Any advantage over pseudocode – do you think the graphical notation pushes you towards developing better structured algorithms?
Review QuestionsReview Questions
What are the three major components of a flowchart? How many arrows can emanate from a process box in
a flowchart? How many arrows can emanate from a decision box? Draw a flowchart to describe the process of finding a
name in a telephone directory by opening the directory in the middle, then moving to the middle of the half where the name will be found and so on until either the name is found or it is established that the name is not in the directory
How do flowcharts compare with pseudocode as a means of specifying program logic?
– Would you have a personal preference?