Flowcharts and Pseudo Code

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

PROBLEM SOLVING TECHNIQUES
ITEC113 Algorithms and Programming Techniques
Review Algorithm Analysis Problem Solving Space Complexity
ALGORITHMS AND FLOWCHARTS
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Chapter 2 - Algorithms and Design
By the end of this session you should be able to...
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Flowcharts.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
1 Program Planning and Design Important stages before actual program is written.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
 Problem Analysis  Coding  Debugging  Testing.
Programming revision Revision tip: Focus on the things you find difficult first.
Program design Program Design Process has 2 phases:
Learning outcomes 5 Developing Code – Using Flowcharts
3.1 Fundamentals of algorithms
A451 Theory – 7 Programming 7A, B - Algorithms.
Programming Languages
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Chapter 2: Input, Processing, and Output
ALGORITHMS AND FLOWCHARTS
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Algorithms and Flowcharts
CS1371 Introduction to Computing for Engineers
The Selection Structure
Chapter 5: Control Structure
Introduction To Flowcharting
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
While Loops in Python.
Algorithm and Ambiguity
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
ALGORITHMS AND FLOWCHARTS
Print slides for students reference
Unit# 9: Computer Program Development
Program Design Introduction to Computer Programming By:
Introduction to pseudocode
Algorithms & Pseudocode
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
ALGORITHMS AND FLOWCHARTS
Introduction to Algorithms and Programming
` Structured Programming & Flowchart
Algorithm and Ambiguity
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
COMPUTATIONAL THINKING COMPUTATIONAL THINKING IN PROGRAMMING
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Chapter 2: Input, Processing, and Output
Basic Concepts of Algorithm
Chapter 2 - Algorithms and Design
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
WJEC GCSE Computer Science
While Loops in Python.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Flowcharts and Pseudo Code

Overview We will explore simple algorithms using flowcharts and pseudo code We will look at: Sequencing – putting things in the right order Conditions – doing different things depending on some rules Iteration (Looping) – repeating something Flowcharts and Pseudo Code

What is Pseudo code? Pseudo code is an informal, high-level, natural-language description of an algorithm. E.g. Start get value result = value * 100 display result Stop Flowcharts and Pseudo Code

What is a Flowchart? A flowchart is a graphical representation of an algorithm It uses standard shapes including: Tools: MS Visio, Word etc. Terminator Input or output Flow line Process Flowcharts and Pseudo Code

Why use Pseudo Code or Flowcharts? They are more concise and clear than a paragraph of writing They show the logical branching with indentation or flow lines They are not specific to a particular programming language They can be used to plan a solution before coding it They can be checked by or discussed with someone who cannot program Computers and Programming

Pseudo Code Pseudo code is informal but we will use this basic structure: Start get value result = value * 2 display result Stop The indentation is important… To begin To ask the user for some data (input) To do some operation To show something on the screen (output) To finish Flowcharts and Pseudo Code

Flowchart To begin To ask the user for some data (input) To do some operation To show something on the screen (output) To finish Flowcharts and Pseudo Code

Simple Sequencing Problem: calculate the average of three input numbers. How do we do this? Start get n1 get n2 get n3 average = (n1 + n2 + n3)/3 display average Stop n1 represents the first number entered average will contain the result of the sum: (n1 + n2 + n3) / 3 Flowcharts and Pseudo Code 8 of 33

Simple Sequencing Here is the flowchart for the same problem Remember to use the right shapes for input or output for a process or calculation Flowcharts and Pseudo Code

Conditions Conditions are the way we make a choice between two or more alternatives. Doing something different depending on a condition is called altering the flow of control. Start get mark if mark >= 50 result = “Pass” else result = “Fail” end-if display result Stop 1. Evaluate the condition getting True or False 2. If the result is True then perform these operation(s) 3. If it is False then perform these operation(s) Flowcharts and Pseudo Code

A Note on Meta-Symbols Note that the < and > characters are called meta-symbols What lies between then indicates the kind of thing which is allowed in its place. if <condition> then <operations for then-part> else <operations for else-part> end-if <variable> = <expression> A condition must go here. E.g. number < 10 A variable must go here. E.g. salary An expression must go here. E.g. basic + bonus 120 + 10 Flowcharts and Pseudo Code 11 of 33

Variable? What’s That? Variables are used to represent some data that might change, e.g. name = “Fred” age = 16 Constants are similar but their values don’t change, e.g. PI = 3.14 FILE_NAME = “mydata.txt” Variables start with a lower case letter Constants are always in UPPERCASE 12 of 33

Conditions Problem: Work out whether the student has passed or failed Start get mark if mark >= 50 result = “Pass” else result = “Fail” end-if display result Stop Flowcharts and Pseudo Code 13 of 33

Conditions There can be more than one expression in the <then> or <else> parts. E.g. Start Get mark if mark >= 50 then result = “Pass” message = “Well done!” else result = “Fail” message = “See me!” end-if display result display message Stop Flowcharts and Pseudo Code 14 of 33

Conditions It is also possible to have multiple conditions. Start get mark if mark < 50 then result = “Fail” else if mark < 60 then result = “Pass” else if mark < 70 then result = “Merit” else result = “Distinction” end-if display result Stop Flowcharts and Pseudo Code 15 of 33

Condition Flowchart 16 of 33

What does this do? What value is printed if: What is this doing? a=5, b=7, c=2 a=8, b=2, c=4 a=3, b=5, c=8 What is this doing? b a c Flowcharts and Pseudo Code 17 of 33

Your Turn Write some pseudo code and sketch a flow chart for the following cinema ticketing system: If customer is aged under 12 then the ticket £5 Otherwise, if they are over 65 the ticket is £7 Otherwise the ticket is £10 Flowcharts and Pseudo Code 18 of 33

Pseudo Code Start get age if age < 12: price = 5 else if age > 65: price = 7 else price = 10 end-if display price Stop Flowcharts and Pseudo Code 19 of 33

Cinema Ticket Flow Chart 20 of 33

Iteration If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display 2 etc. Stop Wouldn’t it be better to say something like this? number = 1 while the number <= 1000… display the number increase the number Flowcharts and Pseudo Code 21 of 33

Iteration: Display Numbers up to 1000 We can write this in pseudo code: Start number = 1 while number <= 1000 display number number = number + 1 end-while Stop Why do we need to do this? Flowcharts and Pseudo Code 22 of 33

Iteration: Display Numbers up to 1000 What is going on here? Flowcharts and Pseudo Code 23 of 33

Iteration: Get Five Numbers Problem: Allow the user to enter 5 numbers How do we know when we have 5 numbers? Start count = 0 while count < 5 get number count = count + 1 end-while Stop Why less than? Why do we need to do this? Flowcharts and Pseudo Code 24 of 33

Iteration: Get Five Numbers 25 of 33

Iteration: Total Five Numbers Now let’s also display the total of those numbers Start count = 0 total = 0 while count < 5 get number count = count + 1 total = total + number end-while display total Stop Here is the pseudo code to ask the user for five numbers We need a total, initially set to zero We need to add the number to that total At the end, we need to display the final total Flowcharts and Pseudo Code 26 of 33

Iteration: Total Five Numbers Flowcharts and Pseudo Code 27 of 33

Your Turn Write some pseudo code and sketch a flow chart to: Ask the user for ten numbers Display the total and average of these numbers Flowcharts and Pseudo Code 28 of 33

Average Ten Numbers Pseudo Code Start count = 0 total = 0 while count < 10 get number count = count + 1 total = total + number end-while average = total / count display total display average Stop Flowcharts and Pseudo Code 29 of 33

Average Ten Numbers Flow Chart 30 of 33

Iteration Types There are different ways of expressing iteration using pseudo code Which you use depends on what you are trying to do The programming language you intend to use is also likely to affect your choice We will use the first one because it translates most directly to Python while <condition> <operation(s) end-while do <operation(s) while <condition> repeat <operation(s)> until <condition> Flowcharts and Pseudo Code Flowcharts and Pseudo Code 31 of 33

Tracing a Flowchart Use a table of possible values and expected outcomes mark Expected result Actual result 49 Fail Fail  50 Pass Pass  59 60 Merit Merit  69 70 Distinction Distinction  You must carefully consider the test values you need to validate your algorithm Flowcharts and Pseudo Code

Summary We have looked at why pseudo code and flowcharts are useful We have looked at pseudo code and flowcharts for sequencing, conditions and iteration On the VLE (Pseudocode and Flowcharts): FlowchartSymbols.pdf gives a summary of shapes PseudocodeNotes.pdf gives a summary of keywords Homework: Hodder’s OCR Computing for GCSE, page 156, questions 1 – 4 (due 17th October) Flowcharts and Pseudo Code 33 of 33