320 likes | 436 Views
Chapter 2. Control Structures. Control Structures. Solving a problem using the Stepwise refinement method, the instructions that make up the algorithm and the structured chart are combinations of 3 control structures: Sequence Structure Selective Structure Iterative Structure.
E N D
Chapter 2 Control Structures
Control Structures • Solving a problem using the Stepwise refinement method, the instructions that make up the algorithm and the structured chart are combinations of 3 control structures: • Sequence Structure • Selective Structure • Iterative Structure
Sequence Structure • The most commonly used and simple structure • Consists of two or more sub components • Instructions are executed/processed one after another in a sequence, in a top to bottom manner • The structure is read from left to right • Normally used in conjunction with one or more of the other control structures; ie, selection and/or iteration
Sequence Structure • The instructions are represented in the subcomponents of the structured diagram , i.e., elementary components • An elementary component is one which has no lower-level components associated and is represented in a data structure diagram simply by placing its name inside a rectangle
Sequence Structure • The sequence control structure can be used to represent 4 basic computer operations; • to receive information e.g. Read customername, address • put out information e.g. Display pageCount • perform arithmetic e.g. add 1 to pageCount • assign values e.g. set pageCount to zero
Sequence Structure: General A B C The component (box) A is a sequence. Boxes B and C (Elementary components) represent component parts of the sequence (sub-components) A is a sequence of B followed by C.
Sequence structure: Pseudocode • use a separate line for each logical element. • align each statement verticallyuntil a change in logic flow occurs • Sequence block is preceded by BEGIN, terminated by END
Sequence Structure: Pseudocode • General format: BEGIN SEQUENCE_BLOCK_NAME sequence sequence statement; … sequence statement; END SEQUENCE_BLOCK_NAMEsequence
Read Title Read FirstName Read Surname Sequence Structure: Example 1 Name Print full name Sub-components of a Sequence Structure - Name
Name Read Title Read FirstName Read Surname Print full name Sequence Structure: Pseudocode Example 1 BEGINNAMEsequence READ title; READ firstname; READ surname; WRITE title + firstname +surname; ENDNAMEsequence
Sequence Structure: Example 2 Problem Specification: Design a program to process an order. A process consists of accepting the quantity sold and price of an item and then display the sales value
Problem Analysis • Initialization • none • Input Definition • qty, price • Output Definition sales : $ 999.99
Problem Analysis • Processing Requirements • Read qty, price • Calculate sales sales = qty * price • Display sales • Processing Control • sales is displayed in 2 decimal places
Problem Analysis • Test Data & Expected Result
Order Read qty, price sales = qty * price Display sales Sequence Structure: Example 2
Sequence Structure: Example 2 BEGIN ORDER sequence END ORDER sequence Order Read qty,price sales = qty * price Display sales Pseudocode: • READ qty, price; • sales = qty * price; • WRITE sales;
Sequence Structure: Example 3 Problem Specification: Calculate the amount due on an electricity bill. The present meter reading and the previous meter readings are entered. The amount due is the difference between the two readings multiplied by the cost of a unit of electricity, currently 5cents per unit.
Sequence Structure: Your task! Do the PROBLEM ANALYSIS Draw the STRUCTURED CHART Write the PSEUDOCODE
Problem Analysis • Initialization • Cost = 0.05 • Input Definition • current, previous • Output Definition Amount due : $ 999.99
Problem Analysis • Processing Requirements • Read current, previous • Calculate bill 2.1. used = current – previous 2.2. bill = used * cost • Display bill • Processing Control • current value must be >= previous value • bill is displayed in 2 decimal places
Problem Analysis • Test Data & Expected Result
Sequence Structure: Example 3 Electricity Bill Calculate bill Display bill Cost = 0.05 Read meters Read current Read previous used = current – previous bill = used*cost
Sequence Structure: Example 3 BEGIN ELECTRICITYBILL sequence cost = 0.05; BEGIN READMETERS sequence READ current; READ previous; END READMETERS sequence BEGIN CALCULATEBILL sequence used = current – previous; bill = used * cost; END CALCULATEBILL sequence WRITE bill; END ELECTRICITYBILL sequence Electricity Bill Display bill Calculate bill Read meters cost = 0.05 bill = used*cost Read current Read previous used = current – previous
Sequence Structure: Example 4 A ONE DOLLAR shop wants you to write a program to read in the sales price in cents which is not to exceed one dollar. The change from one dollar is then to be calculated and printed as the most compact set of change (i.e. the fewest coins). It is assumed that the denominations are 50¢, 20¢, 10¢, 5¢ and 1¢
Sequence Structure: Example 4 • Initialization • none • Input Definition • sales_price • Output Definition Denomination: 50¢ - 9 20¢ - 9 10¢ - 9 5¢ - 9 1¢ - 9
Problem Analysis • Processing Requirements • Read sales_price • Calculate change from $1 • change = 100 – sales_price • Calculate set of change • coin50 = change / 50 • change = change – (coin50 * 50) • coin20 = change / 20 • change = change – (coin20 * 20) • coin10 = change / 10 • change = change – (coin10 * 10) • coin5 = change / 5 • change = change – (coin5 * 5) • coin1 = change • Print coin50, coin20, coin10, coin5, coin1
Problem Analysis • Processing Control • sales_price must be between 1 and 100 cents inclusive • Test Data & Expected Result
Sequence Structure: Example 4 Change change = 100 – sales_price Read sales_price Set of change Display change A B
Sequence Structure: Example 4 A coin50 = change/50 coin20 = change/20 coin10 = change/10 coin1 = change coin5 = change/5 change= change – (coin50*50) change= change – (coin20* 20) change= change – (coin10*10) change= change – (coin5*5)
Sequence Structure: Example 4 B coin50 coin20 coin10 coin5 coin1
Sequence Structure: Example 4 Pseudocode BEGIN CHANGE sequence READ sales_price; change = 100 – sales_price; BEGIN SETOFCHANGE sequence coin50 = change/50; change = change - (coin50*50); coin20 = change / 20; change = change - (coin20*20); coin10 = change / 10; change = change-(coin10*10); coin5 = change / 5; change = change - (coin5*5); coin1 = change; END SETOFCHANGE sequence BEGIN DISPLAYCHANGE sequence WRITE coin50; WRITE coin20; WRITE coin10; WRITE coin5; WRITE coin1; END DISPLAYCHANGE sequence END CHANGE sequence
Class Exercise • Using stepwise Refinement method (sequence control structure) analyse the following problem, draw the structured chart and write the pseudocode: A major department store needs a program to prepare a bill for each customer. Lets assume that each customer purchases only one item type. There will be 4 input values required; customer’s name, itemID, quantity purchased and unit price. Output will be the customer’s bill after a 10% discount is taken before taxes and a 5 percent sales tax is added.