Flowcharts and Pseudocode

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Advertisements

Understanding the Three Basic Structures
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Programming Logic and Design Fourth Edition, Introductory
ITEC113 Algorithms and Programming Techniques
Program Design and Development
1 Chapter 8 Designing Small Programs. 2 A ‘Procedure’ v A set of instructions which describe the steps to be followed in order to carry out an activity.
Chapter 1 Program Design
ALGORITHMS AND FLOWCHARTS
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Simple Program Design Third Edition A Step-by-Step Approach
CIS Computer Programming Logic
By the end of this session you should be able to...
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
ITEC113 Algorithms and Programming Techniques
Program Design BUILDING A HOUSE. Steps to Designing a Program 1. Define the Output 2. Develop the logic to get that output 3. Write the program.
The Software Development Process
Flowcharting & Algorithms. Quick. Close your Eyes and Listen You are still sitting in the classroom. However, you have been called to the counselor’s.
Chapter 7 Problem Solving with Loops
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Algorithms and Pseudocode
Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Algorithms and Flowcharts
Component 1.6.
ALGORITHMS AND FLOWCHARTS
Software Development.
Understand Problem Solving Tools to Design Programming Solutions
3.1 Fundamentals of algorithms
GC101 Introduction to computers and programs
INTRODUCTION TO PROBLEM SOLVING
FLOWCHARTS Part 1.
Basics of Computer Programming
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Programming Mehdi Bukhari.
Variables and Primative Types
Understand Problem Solving Tools to Design Programming Solutions
Lecture 2 Introduction to Programming
Introduction To Flowcharting
Basics of Computer Programming
Basics of Computer Programming
Basics of Computer Programming
Programming Fundamentals
Using a Stack Chapter 6 introduces the stack data type.
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
Using a Stack Chapter 6 introduces the stack data type.
Understanding the Three Basic Structures
ALGORITHMS AND FLOWCHARTS
Computational Thinking for KS3
Coding Concepts (Basics)
Computer Science Testing.
Algorithm and Ambiguity
Data and Flowcharts Session
Flowcharting & Algorithms
How Are Algorithms Developed?
Problem-Solving and Control Structures By Faith Brenner
Data and Flowcharts Session
Introduction to Programming
Tonga Institute of Higher Education IT 141: Information Systems
Data and Flowcharts Session
Tonga Institute of Higher Education IT 141: Information Systems
Basic Concepts of Algorithm
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

Flowcharts and Pseudocode Summative Activity Flowcharts and Pseudocode This lecture is about how you go about planning to write a computer program.

A ‘Procedure’ A set of instructions which describe the steps to be followed in order to carry out an activity. This can be for any activity: eg recipe, assembly instructions etc If it is written in a computer language it is called a program. A computer is simply a sequence of instructions that the computer can understand which it will obey sequentially one after the other. What is meant by sequentially?

To solve a problem by writing a computer program! Make sure you understand the problem! be clear in your mind what you are trying to achieve. Plan the solution! work out how the task should be done on the computer. Implement the solution! write the computer program. Test the solution! make sure the program does what it is supposed to do. This slide describes the total process of developing software. This lecture is really about the second point above. However, this slide shows you how planning fits into the overall creation of a computer program.

Planning the Solution The solution must be prepared before any programming starts. on paper using computerised tools in your head Divide problem into separate tasks if necessary. called ‘modules’ or ‘components’ These tasks may also be divided. Tasks must be precisely described. the ‘procedure’ must be described For very simple programs, like most of the ones in an elementary course you would plan the program in your head (possibly copying the style or structure from another program that you have already seen). By rights it should be done on paper using some of the techniques shown in this lecture. If you were doing a very big job, you can get software to help you do this planning. Bigger jobs can be divided into managable tasks. However, at some point we have to describe the steps that need to be followed for the computer to carry out a particular task. This is called: describing the procedure to be carried out by the computer.

Testing the Program Unit testing Integration testing System testing testing modules separately Integration testing do the modules work together? System testing testing on the computer it will be used on. User acceptance testing are they happy with the program? When you have written the program it should be tested before it can be released for people to use.

Describing Procedures Precisely Flowcharts Diagrams that include words which describe the individual tasks to be carried out and the decisions to be made in regard to carrying out tasks. Pseudocode Words that describe the individual tasks and special ‘keywords’ that describe the decisions to be made in regard to carrying out these tasks. Tis is the main part of this lecture. How do we make a plan for a computer program. Note down the definitions above, but the examples to follow will make them a bit more clear. Main point: One method is visual and the other uses special words.

Write address on envelope. Begin Flowcharts Write address on envelope. Is letter urgent? Yes No Flowchart describing the procedure involved in preparing a set of envelopes for posting. Stick first class stamp on envelope. Stick 2nd class stamp on envelope Fold letter Lick gum Imagine you have to give instructions to a robot to carry out the task. You break it down into small tasks that have to be done (rectangles) and any decisions that have to be made (diamonds) – did you notice that there is only one way into each diamond but more than one way out? Why do you think that is? Imagine that you are the robot and have a bunch of envelopes (already filled) and a list of names and address (some marked urgent) in front of you, and see if you can follow the instructions. Seal envelope Any more letters? Yes No End

Flowcharts Rectangle represents actions. can only have one entry point. can only have one exit point. Diamond represents a decision. phrased like a question. must have one exit point for each possible answer. Arrows show which action is executed after the previous one. Open door Yes Is door open? No Note that the entry point for a diamond must be in a corner. Note also that it is best to have simple yes/no or true/false decisions – then you just have to have 2 exit points from the diamond. Be sure that you know the difference between an action and a decision.

Flowchart Assignment Design flowcharts for the following tasks! Leaving the classroom. Knocking a set of nails into a block of wood. Making a sandwich (choice of ham or cheese) Getting dressed in the morning. Write our your flowcharts and hand them in. These are just some exercise for you to try. If you can get your hands on the out of print book by CS French in the library you will see solutions there. Here’s one without a solution: Try just doing a flowchart for a robot leaving the classroom. It should repeatedly take one step until it can reach the doorhandle. Every time it takes a step it needs to decide if it needs to take another one (This is what is called a loop). When it is finally close enough it should then decide whether to push or pull the door open, before going out. Watch the rules on the previous slide.

Top Down Programming with Stepwise Refinement Designing a computer program by specifying as little of the details of how the job will be done at the early stages, but gradually defining tasks in more detail at later stages. Leave details as long as possible! Divide a job into it’s major components. Take each component and specify the major tasks involved. Break each task down into further tasks. This ‘refinement’ can be done as often as required. Top-Down Design is used in all areas of design! The next slide includes an example of using Top-down design. You could say that if a task is complicated, just give it a name, and leavit it to one side – you can specify the details later.

Procedure for leaving room Top down design/stepwise refinement Start Unlock door Walk to door Outside Are we inside or outside the room? Inside Place hand on door handle Open door Push door Pull door Turn handle The action ‘open door’ may be quite complicated. In our first effort we ignore the complications. Next we have a look at the details that ‘open door’ might entail. ‘Swing door open’ requires a decision, yet in the 2nd effort we ignore the details involved in that task. Our third effort includes some of the details. Move through door Swing door open Details of ‘swing door open’ End Details of ‘Open door’

Data Identify Data Items data items are given names (identifiers) classified as either: constant value does not change during the operation of the program. variable value may change during operation of program. data type numeric (real, fixed, floating, integer) text (alphanumeric) other (date, etc) You should be aware that programs have variables and constants. You should also be aware that there are 2 main distinctions in data: numbers and text, and that they are stored using slightly different methods in the computers memory or storage.

Data Constant Variable Identifier Literal a piece of data that does not change Variable a piece of data that may change in BASIC or JavaScript programs variables may be used to store constants in which case it is up to the programmer to make sure it does not change during the operation of the program. Identifier the name by which the piece of data is known ie. what the variable or constant is called. Literal a piece of text that is fixed in a program. not necessarily given a name. usually has ““ around it to differentiate it from an identifier (variable name) eg cost “cost” Data Identifier – the name you give to a variable (or constant) If you have instructions in your program with “inverted commas” around pieces of text. These are called LITERALS as they never change and they don’t represent anything other than themselves.

Structured Programming Modularise your program. use top-down design methods. write the program as separate units (subroutines) Use standard pre-built modules if available. don’t ‘re-invent the wheel’ eg ‘Windows’ programming libraries. Use the 3 basic programming ‘control structures’. Use Pseudocode (not flowcharts) The trouble with flowcharts is that you may end up with diagrams with lines going everywhere that can be very hard to follow (known as ‘spagetti’ programs). Pseuducode (pronounced sudocode) has strict rules to stop the program design getting so complicated that no one can follow it.

The Three Programming Control Structures. All programs can be described by combining the following 3 control structures: Funnily enough it was a couple of Italian computer scientists that came up with this idea to get over the spagetti problem. They said that all programs can be broken down into the 3 structures shown here. You can either design a program with one structure after the other after the other, or even insert one of the structure inside one of the other structures ( top-down design). Simple Selection (IF-THEN-ELSE) Simple Repitition (Loop) Simple Sequence

Pseudocode Words that describe the individual tasks and special ‘keywords’ that describe the decisions to be made in regard to carrying out these tasks. Keywords for the 3 basic control structures. Forces the programmer to stick to these structures. Looks like a programming language but is NOT. It is a strict form of english. However, because you have to be very disciplined when drawing flowcharts to make sure they don’t end up like spagetti, it is considered to be safer to use PSEUDOCODE. You can’t draw diagrams in Pseudocode. Instead you use special words (called key words) to specify the 3 layouts already shown.

Note the ‘indentation’ Simple Sequence Start BEGIN Walk to door Open door Move through door END Walk to door Open door This is the easy one. BEGIN and END are the keywords that indicate the start and finish of the simple sequence. Move through door Note the ‘indentation’ End

Note the ‘indentation’ Simple Selection IF inside room THEN Pull door ELSE Push door ENDIF Outside Are we inside or outside the room? Inside Push door Pull door This structure has 4 keywords. What are they? Notice that between keywords you are free to use ordinary language. The best technique is to write down the keywords first and then fill in the blanks later. Note the ‘indentation’

Note the ‘indentation’ Simple Repitition WHILE nail is sticking out hit nail ENDWHILE Hit nail Is nail sticking out? Two keywords here. This is more or less saying that ‘as long as the nail is sticking out’ keep repeating the command ‘hit nail’. If you follow the logic of the design, you first ask ‘is the nail sticking out’ , then ‘hit nail’ if it is, then ask the question again. When you finally get no as an answer to the question, you don’t hit the nail , but instead continue on with whatever instructions follow on after the REPITITION structure. yes no Note the ‘indentation’

Hints for writing pseudocode(1) Identify the appropriate structure for the task to being described. sequence, selection or repitition Try these: Adding up a large set of numbers. putting ham or cheese onto a slice of bread. printing out ‘pass’ or ‘fail’ for a student. addressing and stamping an envelope. printing out results for a class of students. Try these examples. Some advice on next page.

Hints for writing pseudocode(2) IF .............. THEN ............... ELSE ........... .............. ENDIF WHILE................. ...... ............. .... ........ ENDWHILE BEGIN ................ ........... ......................... END Write down all the keywords for that structure and fill in the gaps. The gaps can be filled with ordinary English statements. The statements can represent complicated procedures which can be described in detail later.

Pseudocode Assignment Design pseudocode for the following tasks! Leaving the classroom. Making a sandwich (choice of ham or cheese) Adding up a set of numbers. Calculating the average of a set of numbers. Finding the middle of 3 numbers. Printing out results for a class of students. <40 is a fail <60 is a pass <70 is a merit >=70 is a distinction Print out your pseudocode and hand it in with your flowcharts for marking. If you want some practice, try a few of these.