1 / 32

Chapter 2

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.

Télécharger la présentation

Chapter 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 Control Structures

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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.

  7. 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

  8. Sequence Structure: Pseudocode • General format: BEGIN SEQUENCE_BLOCK_NAME sequence sequence statement; … sequence statement; END SEQUENCE_BLOCK_NAMEsequence

  9. Read Title Read FirstName Read Surname Sequence Structure: Example 1 Name Print full name Sub-components of a Sequence Structure - Name

  10. 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

  11. 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

  12. Problem Analysis • Initialization • none • Input Definition • qty, price • Output Definition sales : $ 999.99

  13. Problem Analysis • Processing Requirements • Read qty, price • Calculate sales sales = qty * price • Display sales • Processing Control • sales is displayed in 2 decimal places

  14. Problem Analysis • Test Data & Expected Result

  15. Order Read qty, price sales = qty * price Display sales Sequence Structure: Example 2

  16. 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;

  17. 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.

  18. Sequence Structure: Your task! Do the PROBLEM ANALYSIS Draw the STRUCTURED CHART Write the PSEUDOCODE

  19. Problem Analysis • Initialization • Cost = 0.05 • Input Definition • current, previous • Output Definition Amount due : $ 999.99

  20. 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

  21. Problem Analysis • Test Data & Expected Result

  22. 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

  23. 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

  24. 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¢

  25. Sequence Structure: Example 4 • Initialization • none • Input Definition • sales_price • Output Definition Denomination: 50¢ - 9 20¢ - 9 10¢ - 9 5¢ - 9 1¢ - 9

  26. 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

  27. Problem Analysis • Processing Control • sales_price must be between 1 and 100 cents inclusive • Test Data & Expected Result

  28. Sequence Structure: Example 4 Change change = 100 – sales_price Read sales_price Set of change Display change A B

  29. 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)

  30. Sequence Structure: Example 4 B coin50 coin20 coin10 coin5 coin1

  31. 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

  32. 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.

More Related