Algorithm and Flowchart. 204112 Structured Programming 1

Similar documents
Lesson Description. Texas Essential Knowledge and Skills (Target standards) Texas Essential Knowledge and Skills (Prerequisite standards)

HSBC Debit Card User Guide. Restaurants Shopping Supermarkets

ALGORITHMS AND FLOWCHARTS

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

ChE-1800 H-2: Flowchart Diagrams (last updated January 13, 2013)

GROM Interface Installation into Nissan and Infiniti cars using NIS02 vehicle specific harness

Thank you for purchasing the ButtKicker Gamer. This guide will help you to quickly set up your new ButtKicker system with your PC or gaming console.

Card & Intesa Sanpaolo Bank Albania Network Frequently Asked Questions

ADDING A BET BONUS PROMO CODE TO YOUR ACCOUNT

NMC Sample Problems: Grade 5

5CHAPTER. Setting Up the Polling Place. IMPORTANT: The electronic poll book MUST be set up first and you MUST begin the update by 5:15 am.

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

EKT150 Introduction to Computer Programming. Wk1-Introduction to Computer and Computer Program

Standing Operating Procedure

Answer: The relationship cannot be determined.

ALGORITHMS AND FLOWCHARTS. By Miss Reham Tufail

2 SYSTEM DESCRIPTION TECHNIQUES

Sync Guide. Sync Overview. Before the Event. During the Event. After the Event. Greater Giving Event Software

Ch.4 Fractions and Mixed Numbers

LINE IN, LINE OUT TO TV, VIDEO IN, VIDEO OUT

Notes on Algorithms, Pseudocode, and Flowcharts

Specimen 2015 am/pm Time allowed: 1hr 30mins

Reading 13 : Finite State Automata and Regular Expressions

Chapter 3 Problem Solving

Exercises for Design of Test Cases

Many of these tips are just common sense and others are tips to keep in mind when doing a transaction, at ATMs, restaurants and merchants.

Process / Operation Symbols

A booklet for Parents

Advanced GMAT Math Questions

Configuring the Siemens TC35 modems for use with the MI2292

Building A Computer: A Beginners Guide

The RIDZ 8x2 Audio Switcher

Five EASY Steps to Switch to Fairfield National Bank

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

Five EASY Steps to Switch to The Missouri Bank II

BREAK INTO FASHION! JOKER BILL CASH SYMBOL

LINE IN, LINE OUT AUDIO IN, AUDIO OUT FIXED, VARIABLE TO TV, VIDEO IN, VIDEO OUT Sony Electronics Inc. All rights reserved.

This branch is closing but your bank is always open

TEST 2 STUDY GUIDE. 1. Consider the data shown below.

Set Top Box User Guide. Global Reach with a Local Touch

HOW TO TRANSFER FILES BETWEEN EEN IDL7000 PVR AND USB2 DEVICE

Courtesy Clerk Job Description

COLLEGE OF MEDICINE. Sony Portable Video Conference Systems. Operations Manual

MATHEMATICS Grade Released Test Questions


Part 2. Copyright 1998 Philips Consumer Communications L.P. All rights reserved. Printed in Mexico. Issue 1AT&T

MATHEMATICS GRADE 2 Extension Projects

PL-1, Pocket Logger B

Flowchart Techniques

Quick user guide for the Vista Quantum 960H range of DVRs, models included

Traffic Light Controller. Digital Systems Design. Dr. Ted Shaneyfelt

Counting Money and Making Change Grade Two

Use a probability line to compare the probability of events. E I will brush my teeth before going to bed tonight. Answers will vary.

Role-Play Cards and Props

This feature is available on select devices featuring VUDU. In order to use this feature, your VUDU device must be connected to the Internet.

The Dock The dock allow for well as removed from the. To remove an icon

USER GUIDE Version 2.0

Chapter One Introduction to Programming

Parts of a Computer. Preparation. Objectives. Standards. Materials Micron Technology Foundation, Inc. All Rights Reserved

Instructor Özgür ZEYDAN (PhD) CIV 112 Computer Programming

Quick Start Guide 3G Router from Vodafone

BUSINESS SERVICES DIVISION PROCEDURES MANUAL REVISED DATE 08/13 CASH HANDLING

Downloading <Jumping PRO> from Page 2

United States Naval Academy Electrical and Computer Engineering Department. EC262 Exam 1

ATM Card user s guide

Paying your FNB Credit Card account is as easy as setting up a monthly intelligent Debit Order.

BZWBK24 Internet. How to access the Bank? Logging on to BZWBK24 Internet: Step-by-step instruction

Review for Test 2. Chapters 4, 5 and 6

Checking Account. Money Smarts for Kids. Money Skills for Life. Member FDIC. Welcome! What Is a Checking Account? Why Is a Checking Account So Great?

2 Describing, Exploring, and

Sample Term Test 2A. 1. A variable X has a distribution which is described by the density curve shown below:

Teaching & Learning Plans. Arithmetic Sequences. Leaving Certificate Syllabus

PRODUCTIVITY THROUGH INNOVATION 600 CONTROL DIRECT DRIVE TECHNICAL/OPERATION MANUAL

Welcome to WAPPoint. Spectra

Current/Cheque/Savings Account and Fixed Deposit Terms

A GUIDE TO YOUR ACCOUNT

Your guide to servicing your account. at a Post Office branch

BANKING ON EXPERIENCE IT S EASY TO SWITCH MAKE THE SWITCH TO F&M BANK TODAY! 142 W. MAIN STREET P.O. BOX 718 MANCHESTER, GA 31816

Kiva Technology User s Manual

Member FDIC. Checking Account Guide

Fi TV Gold Quick Start Guide

Keystone National Middle School Math Level 8 Placement Exam

Chapter 12 Programming Concepts and Languages

TechniSat HDFS Satellite HD Digital Receiver. USB Recorder Instructions

SOFTWARE USER GUIDE. Aleratec. Part No , EU. 1:10 USB 3.0 Copy Cruiser Mini

Pensions Freedom. What do the pension changes really mean? This is for information purposes only.

STEP 7 MICRO/WIN TUTORIAL. Step-1: How to open Step 7 Micro/WIN

SIMATIC Safety Workshop

Switch Checklist. Direct Deposit Change Form

2. Capitalize initial keyword In the example above, READ and WRITE are in caps. There are just a few keywords we will use:

lesson six banking services supplemental materials 04/09

How to Use the Dimension Elite 3D Printer

Shortest Path Algorithms

DVB-T 730. User s Manual

Fractions. Chapter Understanding fractions

Mathematics (Project Maths Phase 1)

Cashiering. Reference and Training Guide. Kuali Financial Systems. Belongs to: Version

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Creating Advanced Reports with the SAP Query Tool

ReSound Unite TV FREQUENTLY ASKED QUESTIONS. Setup & Configuration. Use & Operation. Troubleshooting

Transcription:

Algorithm and Flowchart 204112 Structured Programming 1

Programming Methodology Problem solving Coding Problem statement and analysis Develop a high-level algorithm Detail out a low-level algorithm Choose a programming language Code the program using the selected algorithm Test the program and correct the errors 204112 Structured Programming 2

Algorithm Definition Solution to a computer programming problem. Algorithm can be written in 2 different ways Pseudo-code English-like steps that describes the solution Flowcharts Picture with specific blocks detailing out the logical flow of the solution 204112 Structured Programming 3

Flowchart Building Blocks CONTROL FLOW TERMINAL POINT - Start / End PROCESS - Initializing, Calculation... INPUT / OUTPUT - Keyboard, Display... DECISION CONNECTOR - used for big diagram across pages PRINTOUT ` STORAGE - Read or Write from CDs, Disks, Tapes SUB-ROUTINE 204112 Structured Programming 4

Example 1 Start Problem Statement Watch a movie at home Algorithm 1. Switch on the TV and UBC sets 2. Change to the required movie channel 3. Sit down and watch the movie Switch on the TV Switch on the UBC Change to the movie channel Sit down Watch movie End 204112 Structured Programming 5

Start Example 2 Problem Statement Withdraw cash from ATM Algorithm 1. Go to the ATM 2. Insert your card into the machine 3. Press in your code 4. Choose Withdraw and enter Amount required 5. Take the cash, slip and card. Go to the ATM Insert your card into the ATM machine Press in your code Choose Withdraw Enter the amount Take the cash Take the slip Take the card End 204112 Structured Programming 6

Start Example 3 Input Amount Problem Statement Calculate the interest of a bank deposit. You are to read the amount, years and interest rate from the keyboard and print the interest amount. Algorithm 1. Read Amount 2. Read Years 3. Read Rate 4. Set Interest as Amount * Rate * Years / 100 5. Print Interest Input Years Input Rate Compute Interest <- Amount * Years * Rate / 100 Output Interest End 204112 Structured Programming 7

Example 3 Input/Output Samples Inputs Amount = 5000 Years = 2 Rate = 2 Amount = 1000 Years = 1.5 Rate = 2.5 Outputs Interest = 200 Interest = 37.50 204112 Structured Programming 8

Example 4 Problem Statement Print what to do when driving to a traffic signal Start Input Signal Algorithm 1. Read traffic signal 2. If signal is GREEN then Set Action as GO Else Set Action as STOP 3. Print Action Yes Action <- GO Signal = GREEN? Output Action No Action <- STOP End 204112 Structured Programming 9

Example 4 Input/Output Samples Inputs Signal = GREEN Signal = RED Signal = YELLOW Check what happens if Signal = BLUE Outputs Action = GO Action = STOP Action = STOP Action = 204112 Structured Programming 10

Example 5 Start Problem Statement Read a number from the keyboard. Check and output if a given number N is ODD or EVEN Input N Remainder <- N modulo 2 Algorithm 1. Read N 2. Set Remainder as N modulo 2 3. If Remainder is equal to 0 then Set Answer as EVEN Else Set Answer as ODD 4. Print Answer Yes Answer <- EVEN Remainder = 0? Output Answer End No Answer <- ODD 204112 Structured Programming 11

Example 5 Input/Output Samples Inputs N = 5 N = 8 N = 0 N = -1 Outputs Answer = ODD Answer = EVEN Answer = EVEN Answer = ODD 204112 Structured Programming 12

Start Example 6 Input Gender Problem Statement Print Title for a person (Either Mr. or Miss. or Mrs.). You are to read the gender (and status if needed). Algorithm 1. Read Gender 2. If Gender is MALE then Title is Mr. Else Read Status If Status is MARRIED then Title is Mrs. Else Title is Miss. 3. Print Title Gender = Male? Yes Title <- Mr. No Input Status Status = Married? Yes No Title <- Mrs. Title <- Miss Output Title End 204112 Structured Programming 13

Example 6 Input/Output Samples Inputs Gender = Male Gender = Female Status = Married Gender = Female Status = Single Check what happens if Gender = Boy Status = Intelligent Outputs Title = Mr. Title = Mrs. Title = Miss. Title = 204112 Structured Programming 14

Example 7 Start Initialize X <- 0 Problem Statement Print 1 to 20 Increment X by 1 Algorithm Print X 1. Initialize X as 0 2. Increment X by 1 3. Print X Yes ` X < 20 4. If X is less than 20 then go back to Step 2 No End 204112 Structured Programming 15

Start Example 8 Retrieve computer time Problem Statement Given computer time is stored in 24 hours format, you are to print the time in AM/PM format ExtractHOURS Extract MINUTES HOURS = 0? No 1 <= HOURS <= 12 Algorithm 1. Retrieve computer time 2. Extract Hours and Minutes 3. If Hours is equal to 0 then Print 12 Else If Hours is between 1 and 12 then Print Hours Else Print Hours 12 4. Print : 5. Print Minutes 6. If Hours is less than 12 then Print AM Else Print PM Yes Output 12 Output HOURS Output (Hours - 12) Output ":" 204112 Structured Programming 16 Yes Output AM End Yes Output MINUTES HOURS < 12 No No Output PM

Example 8 Input/Output Samples Inputs Computer time = 8:30 Computer time = 20:30 Computer time = 0:15 Computer time = 12:15 Outputs Printed time 8:30 AM Printed time 8:30 PM Printed time 12:15 AM Printed time 12:15 PM 204112 Structured Programming 17

Start Example 9 Input MONTH MONTH = 2? Problem Statement Read the Month (and Year, if needed) and print the number of days in that month Input YEAR Algorithm 1. Read MONTH 2. If MONTH is equal to 2 then Read YEAR If YEAR is a leap year then Set DAYS as 29 Else Set DAYS as 28 Else If MONTH is either 4 or 6 or 9 or 11 then Set DAYS as 30 Else Set DAYS as 31 3. Print DAYS Set LEAP <- YEAR modulo 4 LEAP = 0? Set DAYS <- 30 Set DAYS <- 28 Set DAYS <- 29 Output DAYS MONTH = 4 or MONTH = 6 or MONTH = 9 or MONTH = 11? Set DAYS <- 31 204112 Structured Programming End 18

Example 9 Input/Output Samples Inputs Outputs Month = 2 Days = 29 Year = 2004 Month = 2 Days = 28 Year = 2005 Month = 10 Days = 31 Month = 4 Days = 30 Check what happens if Month = -1 Days = 204112 Structured Programming 19

Example 10 Problem Statement Prepare sandwiches High-level Algorithm 1. Go to the nearest supermarket 2. Pick the groceries you need 3. Pay at the cashier 4. Bring the groceries home 5. Prepare the sandwiches Low-level Algorithm 1.1 Take the car keys and wallet from the counter 1.2 Drive the car to the supermarket 1.3 Park the car 1.4 Take the lift to the supermarket floor 2.1 Take an empty cart and walk around the floor 2.2 Put the needed groceries into the cart 2.3 Take the cart to the cashier 3.1 Give the credit card to the cashier 3.2 Sign on the credit card slip 4.1Take the cart with the plastic bags to the car 4.2 Put the plastic bags to the car 4.3 Drive the car home 4.4 Remove the plastic bags from the car 5.1 Cut the bread into half 5.2 Prepare the bacon and salad 5.3 Put the ingredients between 2 slices of bread 204112 Structured Programming 20

Example 11 Problem Statement Make an urgent call to your friend from the airport High-level Algorithm 1. Go to a public booth 2. Dial your friend s number 3. Give the message to your friend Low-level Algorithm 1.1 Walk to the next phone booth 1.2 If phone booth is not working, then repeat from step 1.1 2.1 Retrieve the number from pocket diary 2.2 Put some coins into the slot. 2.3 Dial the number 2.4 If the line is busy, hang up, then take back the coins and repeat from step 2.2 3.1 If your friend can come to the phone, then talk to your friend. 3.2 If your friend cannot come to the phone, then leave a message for your friend. 3.2 Hang up the phone. 3.4 Retrieve any coins not used. 204112 Structured Programming 21

Start Example 11 Walk to next phone booth Phone working? Retrieve Number from Pocket Diary Put some coins into the slot Dial the number Line is busy? Friend found? Talk to friend Leave message Hang up the phone Retrieve the unused coins 204112 Structured Programming End 22

Example 12 Problem Statement Automatically return change for a purchase of N baht when given a 20 baht note. Check that N is between 1 and 20. High-level Algorithm 1. Read and Validate N 2. Calculate Change 3. Decide how many 10 baht coins, 5 baht coins and 1 baht coins to return What happens if customer can pay by any kinds of banknotes: 1000, 500, 100, 20, and 10. and any kinds of coins: 10, 5, 2, and 1. That means N is not be fixed. Low-level Algorithm 1.1 Read N 1.2 If T (1 <= N <= 20) then Print Error Message Go back to Step 1.1 2.1 Initialize CHANGE as 20 2.2 Deduct N from CHANGE 3.1 If CHANGE is less than 10 then Number of 10 baht coin is 0. Else Number of 10 baht coin is 1. Deduct 10 from CHANGE 3.2 If CHANGE is less than 5 then Number of 5 baht coin is 0. Else Number of 5 baht coin is 1. Deduct 5 from CHANGE 3.3 Number of 1 baht coin is CHANGE 204112 Structured Programming 23

Start Example 12 Check what happens if N = 20 Check what happens if N = 0 Check what happens if N = 21 Input N N >= 1 and N <= 20 Set CHANGE <- 20 - N Output Error CHANGE < 10? Set COIN10 <- 0 Set COIN10 <- 1 Set CHANGE <- CHANGE - 10 CHANGE < 5? SET COIN05 <- 0 Set COIN05 <- 1 SET CHANGE <- CHANGE - 5 Set COIN01 <- CHANGE 204112 Structured Programming End 24

Example 12 Input/Output Samples Inputs Outputs N = 17 Number of 10 B coin 0 Number of 5 B coin 0 Number of 1 B coin 3 N = 6 Number of 10 B coin 1 Number of 5 B coin 0 Number of 1 B coin 4 N = 13 Number of 10 B coin 0 Number of 5 B coin 1 Number of 1 B coin 2 Check what happens if N = 20 Check what happens if N = 0 Check what happens if N = 21 204112 Structured Programming 25

Example 13 Problem Statement Find the average of a given list of numbers High-level Algorithm 1. Find the SUM of the given numbers 2. Find the COUNT of the given numbers 3. AVERAGE is SUM COUNT Low-level Algorithm 1. Initialize SUM as 0 and COUNT as 0 2. If there are no more numbers remaining to be processed, then go to step 7. 3. Set ITEM as next number in the list 4. Add ITEM to SUM 5. Increment COUNT by 1 6. Go back to step 2 7. If COUNT is equal to 0, then AVERAGE is undefined Else AVERAGE is SUM COUNT 204112 Structured Programming 26

Start Example 13 Set SUM <- 0 Set COUNT <- 0 Any more unprocessed numbers? Set ITEM <- Next number in list Set SUM <- SUM + ITEM Set COUNT <- COUNT + 1 COUNT = 0? Set AVERAGE = SUM / COUNT Output "Undefined" Error Output AVERAGE 204112 Structured Programming End 27

Example 13 Input/Output Samples Inputs Outputs List = 20, 2, 5, -3 Average = 6 List = 2, 5, -3, -8, -1 Average = -1 List = 2, 7, 5, 3, 6 Average = 4.60 List = 4 Average = 4 List = Average = undefined 204112 Structured Programming 28

Example 14 Problem Statement Given a 2-D polygon with N sides (and N vertices). Find the smallest rectangular box required to cover the polygon completely Algorithm 1. Initialize MINX, MINY, MAXX, MAXY using the 1 st Vertex 2. Retrieve the next unevaluated vertex (X, Y) 3. If X < MINX, then set MINX as X 4. If X > MAXX, then set MAXX as X 5. If Y < MINY, then set MINY as Y 6. If Y > MAXY, then set MAXY as Y 7. If all vertices have not been evaluated then go back to step 2 8. Set LENGTH as MAXX MINX 9. Set HEIGHT as MAXY MINY 204112 Structured Programming 29

Start Example 14 Get the 1st Vertex (X,Y) Set MINX <- X Set MAXX <- X Set MINY <- Y Set MAXY <- Y Get the next Vertex (X,Y) A X < MINX Set MINX <- X B X > MAXX Set MAXX <- X Set LENGTH <- MAXX - MINX Set HEIGHT <- MAXY - MINY Y < MINY Set MINY <- Y End Y > MAXY Set MAXY <- Y B Any unevaluated vertex left? 204112 Structured Programming 30 A

Example 14 Input/Output Samples Inputs Outputs 4 sides (2,2) (5,3) (3,5) (6,2) Length = 4 Height = 3 3 sides (1,2) (5,3) (8, -2) Length = 7 5 sides (2,5) (7,1) (3,2) (-3, -5) (4,1) Height = 5 Length = 10 Height = 10 204112 Structured Programming 31