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.

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

More on Algorithms and Problem Solving
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Introduction to Flowcharting
Introduction to Flowcharting
Selection (decision) control structure Learning objective
Understanding the Three Basic Structures
An Object-Oriented Approach to Programming Logic and Design
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
CS0004: Introduction to Programming Repetition – Do Loops.
ITEC113 Algorithms and Programming Techniques
Chapter 2: Algorithm Discovery and Design
Chapter 3 Planning Your Solution
The Program Design Phases
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Fundamentals of Python: From First Programs Through Data Structures
CSC103: Introduction to Computer and Programming
Fundamentals of Python: First Programs
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Problem Analysis and Program Design
Simple Program Design Third Edition A Step-by-Step Approach
Flowchart IDT. What is a flowchart? Visual representation of a flow of data Outline of process or a solution to a problem Outline the basic logic behind.
PROGRAMMING LANGUAGES Prof. Lani Cantonjos. PROGRAM - set of step-by-step instructions that tells or directs the computer what to do. PROGRAMMING LANGUAGE.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
1 IPC144 Session 4 Flowcharts. 2 Objectives: By the end of this session, the student will be able to: List the three elements of the Structure Theorem.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
CPS120: Introduction to Computer Science Decision Making in Programs.
Introduction to Programming with RAPTOR
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
ALGORITHM List of instructions for carrying out some process step by step. A sequence of instructions which has a clear meaning and can performed with.
ITEC113 Algorithms and Programming Techniques
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering Problem Solving and Logic.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
FLOWCHARTING AND ALGORITHMS
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
ICS124 Session 9 Flowcharting 1. By the end of this section the student will be able to:  Name the three structures of the Structure Theorem  Identify.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
ALGORITHMS AND FLOWCHARTS
Understand Problem Solving Tools to Design Programming Solutions
Understand Problem Solving Tools to Design Programming Solutions
Introduction To Flowcharting
Programming Fundamentals
Lecture 2: Logical Problems with Choices
Unit# 9: Computer Program Development
Structured Program
Understanding the Three Basic Structures
Faculty of Computer Science & Information System
Basic Concepts of Algorithm
REPETITION Why Repetition?
Presentation transcript:

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 the symbols used in flowcharts Recognize the flowchart symbols and their meaning Construct the elements of the Structure Theorem using Flowcharts 4

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

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

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

Simple Sequence 8

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

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

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

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

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

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

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

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

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)‏

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

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

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

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

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”

Selection 23

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

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

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 Default varC x > 5 Default INVALID

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

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

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

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

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 in14 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)‏ 31

Flowcharting - Selection, continued Example 32 Start Read stuName, bookName signoutDate, colourCode daysBetween(signoutDate, n)‏ A

Flowcharting - Selection, continued Example 33 Stop A colourCode fine = ovrDue * 0.05 W B G O Display stuName, bookName ovrDue, fine ovrDue = n - 13 ovrDue = n - 6 ovrDue = n ovrDue = n - 27

Flowcharting - Selection, continued Alternate Example 34 Stop A colourCode ='W' ovrDue = n - 13 ovrDue = n - 6 ovrDue = n ovrDue = n - 27 Display stuName, bookName ovrDue, fine colourCode ='B' colourCode ='G' B B fine = ovrDue * 0.05 T T T F F F

Iteration 35

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

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

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

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

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

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

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

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