Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPC144 Session 4 Flowcharts 1. Objectives: By the end of this session, the student will be able to: List the three elements of the Structure Theorem Identify.

Similar presentations


Presentation on theme: "IPC144 Session 4 Flowcharts 1. Objectives: By the end of this session, the student will be able to: List the three elements of the Structure Theorem Identify."— Presentation transcript:

1 IPC144 Session 4 Flowcharts 1

2 Objectives: By the end of this session, the student will be able to: List the three elements of the Structure Theorem Identify the symbols used in flowcharts Recognize the flowchart symbols and their meaning Construct the elements of the Structure Theorem using Flowcharts 4

3 Flowcharts Background The third phase of the PDC is Develop Solution into an Algorithm. The points that were made are: Computers cannot devise a plan or decide to act Computers can only do what they are told, exactly the way they are told Computers do what you tell them to do, not what you want them to do An algorithm is a set of precise instructions which describe the task to be performed and the order in which they are to be carried out Flowcharting is a tool that helps develop a solution into an algorithm:  A picture is worth a thousand words  Uses only a limited number of symbols to define an algorithm of any complexity 5

4 Flowcharts This is where the program starts to take shape. The Structure Theorem states that it is possible to write any computer program by using only THREE basic control structures: simple sequence selection iteration 6

5 Flowcharts Simple sequence The Simple Sequence represents the computer's ability to execute instructions in a step- by-step, sequential manner. Selection The Selection represents the computer's ability to make a decision. By evaluating a condition, alternate execution paths are followed. Iteration Iteration represents the computer's ability to repeat a series of instructions. 7

6 Simple Sequence 8

7 Flowcharting - Simple Sequence Flow lines Shows the direction of logic flow within the flowchart Unless otherwise stated the symbols of a flowchart have one flow line entering it, and one flow line exiting it. 9

8 Flowcharting - Simple Sequence, continued Terminator or Terminal Interrupt Used to show the beginning or end of an algorithm There must be these two symbols (only one each)‏ 10 Start Stop

9 Flowcharting - Simple Sequence, continued Terminator or Terminal Interrupt, continued A simple flowchart that does nothing: 11 Start Stop

10 Flowcharting - Simple Sequence, continued Process Box Used to show a processing operation The operation is described inside the box (i.e. a calculation) Does not show a decision, or the input or output of data 12 Perform a process

11 Flowcharting - Simple Sequence, continued An Example Prepare for an exam: 13 Start Stop Review texts Review Lectures Review Tests Review Labs

12 Flowcharting - Simple Sequence, continued Off-page / On-page Connector What happens when you run out of room on the page? A unique identifier is written inside the symbol so that they can be paired up when reading the flowchart Like the terminator, in that there is either one flow line entering it, OR one flow indicator exiting it. 14 A Off-page A On-page

13 Flowcharting - Simple Sequence, continued A three page example 15 Start UVYZ Stop A B WX A B

14 Flowcharting - Simple Sequence, continued Input / Output Operation Used to show the input of data, or the output of data The operation is described inside the box Does not show a decision, or any processing steps 16 Read valueA

15 Flowcharting - Simple Sequence, continued Module Call Box Used to call or invoke another flowchart. Any variables in the current program that are required by the module are listed in parenthesis, each separated by a comma. The new flowchart is executed, and when it completes (hits the Stop Terminator) the current flowchart resumes execution. This is where the modularity of programming is represented 17 getData(a, b)‏

16 Flowcharting - Simple Sequence, continued Flowcharting the Module All of the rules that are being discussed for flowcharts applies to the modules. Modules are flowcharts that are composed of Simple Sequence, Decisions and Iteration. The START and STOP terminal interrupts have new meaning in this case. The START terminal interrupt shows the name of the of the module and the parameters to be used (some or all may be modified by the module). The STOP terminal interrupt shows the module is complete and execution control is being returned to the flowchart that invoked the module. 18

17 Flowcharting - Simple Sequence, continued Parameters Much more time will be spent working with parameters when we actually start programming in C. Parameters are a means to pass values from a flowchart to a module. The names of the parameter variables in the module need not be the same name, however, they are positionally dependent. 19 myModule(a, varB, x, name)‏ myModule(a, b, c, d)‏ a varBb xc named

18 Flowcharting - Simple Sequence, continued Parameters, continue 20 myModule(a, b, c, d)‏ myModule(a, varB, x, name)‏ Stop Processing is passed to this flowchart Control of execution is returned to the originating flowchart

19 Flowcharting - Simple Sequence, continued Example Develop a program to accept three numbers, add them together, then print their total and average. Write the flowchart 21

20 Flowcharting - Simple Sequence, continued Example Develop a program to accept three numbers, add them together, then print their total and average. Write the flowchart 22 Start DISPLAY “Enter 1 st Value” total= val1 + val2 + val3 A C average= total / 3 PRINT total, average Stop GET val1 DISPLAY “Enter 1st Value” A DISPLAY “Enter 1 st Value” B GET val2 DISPLAY “Enter 2nd Value” B DISPLAY “Enter 1 st Value” C GET val3 DISPLAY “Enter 3rd Value”

21 Selection 23

22 Flowcharting - Selection Selection There will be references for the remainder of flowcharting to "conditions" A condition is a logical expression that is either TRUE or it is FALSE The condition is expressed as some form of a mathematical expression Expressions: A > B SIN(theta) = COS(theta)‏ recordValue = "end of records" 24

23 Flowcharting - Selection, continued Simple selection box Used to show a change in the path of logic in the program The condition being tested is written inside the diamond in a mathematical form using information available in the flowchart. Has 2 flow indicators exiting it These flow lines MUST be labelled with the result of the decision (e.g. YES, NO, TRUE, FALSE...) 25 A > B + 5 True False

24 Flowcharting - Selection, continued Multiway selection (Case Statement)‏ Used to show a change in the path of logic in the program Has more than 2 flow lines exiting it Used for EQUALITY decisions (each flow line is labelled with the value)‏ You may not place a range of values on the flow lines You may not place conditions on the flow lines One branch may be labelled 'Default' to represent the path to be taken when none of the conditions are met 26 varC 1 2 3 4 Default varC 1 - 6 7 - 9 10 x > 5 Default INVALID

25 Flowcharting - Selection, continued Collector Used to collect the flow lines that were created from a decision box, and restore the logic of the program back to a single stream THE PATHS FROM A DECISION MUST REJOIN IN A COLLECTOR To be used with simpleTo be used with a multiwaydecision ONLY 27

26 Flowcharting - Selection, continued Example - Simple Decision 28 B amount < 0 DB = DB + amount CR = CR + amount C True False

27 Flowcharting - Selection, continued Example - Multiway Decision 29 B userOption res = x * y res = x - y C res = x / y res = x + y A SM D

28 Flowcharting - Selection, continued Example Write a program to help a local Elementary School's library with the overdue fines on books. The librarians don't wish to always charge students for overdue books, however sometimes the number of books that are overdue is too much. When this happens the librarians enter into the program the student's name, the name of the overdue book they borrowed, and the date it was signed out (the librarian reads these fields from the sign- out card). The colour of the card is also entered, as this indicates what the normal sign- out period is for that book. White cards are due back in 14 days, Blue cards in 7 days, Green cards the next day, and Orange cards in 28 days. The colour code of the card is entered as a single character into the program (W, B, G and O). When this information has been entered, the program determines by how much the book is overdue (based on the date the program is run) and the overdue charge, ($0.05 per day). A list is produced that will show the student's name, the name of the book, the number of days the book is overdue and the fine that is payable. Create the IPO Chart and Flowchart (a module daysBetween(date, n) exists where the date parameter is the date to be compared to and the n parameter is the number of days between date and today that is calculated. For example if today is February 5 th and the date parameter is February 16 th, then the value of n would be set to 11 by the function. 30

29 Iteration 35

30 Flowcharting - Iteration Iteration Iteration is also referred to as "looping" The goal of iteration is to repeat a sequence of steps until a certain condition has been met The iteration is a special case of the decision structure already discussed The sequence of steps in the loop may consist of simple sequences, decisions, loops There are two types of loops: Pre-test Loop Post-test Loop 36

31 Flowcharting - Iteration Pre-Test Loop The condition that terminates the loop is tested before the loop is entered It is therefore possible to not execute any of the statements within the loop at all 37 Repeated statement(s)‏ False True Conditional Expression Update Statement

32 Flowcharting - Iteration Post-Test Loop The condition that terminates the loop is tested after the loop is entered It is therefore necessary to execute the sequence of statements at least once in order to reach the decision that terminates the loop. 38 Repeated statement(s)‏ True Conditional Expression Update Statement

33 Flowcharting, continued Integrity of a Flowchart In this course will represent ALL logic necessary to represent a program – every last tedious step. There are no 'magic' shortcuts to writing the algorithm. A flowchart is not just a collection of symbols and lines. As a rule of thumb – lines in a flowchart do NOT cross each other Think of a flowchart as a story in print:  Symbols (rectangles, parallelograms, circles, diamonds, lines...) -> letters of the alphabet. Individually they have no meaning, they are used to make words.  Simple Sequence / Decision / Iteration -> Words of the story. Just as you don’t mix letters arbitrarily into a word, you don’t throw symbols into a flowchart.  The flowchart is the story you are composing from START to STOP. 39

34 Flowcharting, continued Nesting Structures When combining structures, they must be nested. Each structure (Simple Sequence, Simple Decision and Iteration) each have ONE entry point and ONE exit point. This is a rule that cannot be broken: 40

35 Flowcharting, continued Nesting Structures, continued Embedding a loop within a decision 41 Loop needed Becomes...

36 Flowcharting, continued Nesting Structures, continued Embedding a decision within a loop 42 Decision needed Becomes...

37 Flowcharting, continued Nesting Structures, continued Think of the nesting of your program, as a series of self-contained objects. One inside the other. There is no connection / interaction across layers. For example, this is illegal: 43


Download ppt "IPC144 Session 4 Flowcharts 1. Objectives: By the end of this session, the student will be able to: List the three elements of the Structure Theorem Identify."

Similar presentations


Ads by Google