Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.

Similar presentations


Presentation on theme: "Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions."— Presentation transcript:

1 Flowcharts and Algorithms

2 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

3 The 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

4 Algorithm 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

5 Writing 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?

6 Example  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

7 Pseudocode 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

8 Algorithm 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

9 Pseudocode 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

10 Algorithm 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

11 Pseudocode 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

12 Abstraction  We use procedures (or mini-algorithms) to break the solution into meaningful “chunks” If NumOfChildren > 1 Then CalculatePocketMoneyforManyChildren Else CalculatePoketMoneyforOneChild Endif  Each procedure is then described in pseudocode

13 Flowcharts as an Alternative to Pseudocode  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

14 Example Flowchart

15 Flowchart Symbols Rectangle is used for actions Diamond is used for decisions Symbols are connected by arrows to represent the order of the actions

16 Flowcharting 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.

17 IF example “You are overdrawn” Balance<0 yes Program flow

18 If/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!)

19 If/Then/EndIf example “You are overdrawn” Balance<0 yes “Authorities have been notified” End If Program flow

20 If/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.

21 If/Then/Else/EndIf example “You are overdrawn” Balance<0 yes “Authorities have been notified” End If “Your balance is £x” no End If

22 ElseIf 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.

23 If/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

24 Flowchart 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

25 Note 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.

26 And /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.

27 And /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

28 Nested 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

29 Looping Structures Times = 0 Times = Times + 1 Do this Do that Does Times = 5 End Yes No A looping structure to repeat operations five times

30 Different Types of Loop While conditions do begin : statements / commands : end; Repeat : statements / commands : Until condition;

31 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 < 3 FALSETRUE KidsPaid = 0 Total = 0 Add PM to Total Increment KidsPaid Prompt Age Flowchart for Pocket Money Example

32 Exercise  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?

33 Review 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?


Download ppt "Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions."

Similar presentations


Ads by Google