School of Business Eastern Illinois University Repetition Control Structures (Week 9, Friday 3/07/2003) © Abdou Illia, Spring 2003 Learning Objectives Understand using Trailer record to control Repetitive structures Understand using End-Of-File (EOF) condition to control Repetitive structures Design programs using Trailer record logic and EOF condition. 2 Recall (Algorithm vocabulary) SYMBOLS START NAME USE Terminal interrupt symbols Terminal point (start, stop, or break) 3 STOP Input/Output symbol Process symbol Reading data from an input medium or writing data to an output medium Processing input data Recall (Algorithm vocabulary) SYMBOLS NAME USE Flowline symbol Sequence of operations and direction of data flow Decision symbol Predefined-process symbol 4 Decision-making operations Operations specified elsewhere (not in the current algorithm) Recall (Algorithm vocabulary) SYMBOLS NAME USE Connector symbol Exit to, or entry from, another part of the Flowchart Preparation symbol . . Annotation symbol Control operations: Set limit on loop-control variables, Initialize accumulators, etc. Addition explanation; comments. 5 6 Problem: Defective parts A manufacturing company produces a large number of automotive parts each year in its two plants. Some of these plants are returned to the main sale Office because of defects in manufacture. An input record is prepared for each defective part, containing a code, part number, type of part, and date returned. The code is either 1 or 2, indicating whether plant 1 or plant 2 made the defective part. Example of file containing input records A trailer record containing a plant code of 9 to signal the end of the input file Plant Code Part # Part Type Date Returned 2 L45603 TAILLIGHT 04/16/02 1 W07722 WIPER BLADE 04/17/02 1 C19654 CLOCK 04/16/01 2 D33045 DOMELIGHT 04/20/01 . . 9 . . 999999 . . END OF FILE . . 00/00/00 Problem: Defective parts Plant Code Part # Part Type Date Returned 2 L45603 TAILLIGHT 04/16/02 1 W07722 WIPER BLADE 04/17/02 1 C19654 CLOCK 04/16/01 2 D33045 DOMELIGHT 04/20/01 . . 9 . . 999999 . . END OF FILE . . 00/00/00 7 The company needs a read-and-print program to process the records and produce a listing of their contents. In addition the number of defective parts manufactured by each plant is to be totaled for future reference. The output of the program is to consist of a printed listing of the contents of the input records, followed by the total computed for each plant. Plant Code Part # Part Type Date Returned 2 L45603 TAILLIGHT 04/16/02 1 W07722 WIPER BLADE 04/17/02 1 C19654 CLOCK 04/16/01 2 D33045 DOMELIGHT 04/20/01 . . 9 . . 999999 . . END OF FILE . . 00/00/00 START Solution: Using Trailer record (Program Flowchart) COUNT1 = 0 COUNT2 = 0 Priming Read: - Placed before the loop - Gets the computer ready (primes the computer) to make the loop test READ CODE, PART, TYPE, DATE CODE WRITE NO COUNT1, COUNT2 ≠9? YES Loop controlled by a Trailer record NO CODE YES =1? Loop Read: -Within the loop -Causes next record to be read. 8 COUNT2 = COUNT1 = COUNT2 + 1 COUNT1 + 1 READ WRITE CODE, PART, TYPE, DATE CODE, PART, TYPE, DATE STOP Solution: Using Trailer record (Pseudocode) Start COUNT1 = 0 COUNT2 = 0 Read CODE, PART, TYPE, DATE DOWHILE CODE ≠ 9 IF CODE = 1 THEN COUNT1 = COUNT1 + 1 ELSE COUNT2 = COUNT2 + 1 ENDIF Write CODE, PART, TYPE, DATE Read CODE, PART, TYPE, DATE ENNDO Write COUNT1, COUNT 2 Stop Plant Code Part # Part Type Date Returned 2 L45603 TAILLIGHT 04/16/02 1 W07722 WIPER BLADE 04/17/02 1 C19654 CLOCK 04/16/01 2 D33045 DOMELIGHT 04/20/01 . . 9 . . 999999 . . END OF FILE . . 00/00/00 9 Questions 10 What if the Priming Read is omitted ? What if the Loop Read is omitted ? What if the DOWHILE statement (in the previous slide) is replaced by an IF statement ? Solution: Using Trailer record (Program Flowchart) Adds Heading line(s) to the output Additional test for invalid code Plant Code Part # Part Type Date Returned 2 L45603 TAILLIGHT 04/16/02 1 W07722 WIPER BLADE 04/17/02 1 C19654 CLOCK 04/16/01 2 D33045 DOMELIGHT 04/20/01 . . 9 . . 999999 . . END OF FILE . . 00/00/00 11 Solution: Using Trailer record (Pseudocode) Plant Code Part # Part Type Date Returned 2 L45603 TAILLIGHT 04/16/02 1 W07722 WIPER BLADE 04/17/02 1 C19654 CLOCK 04/16/01 2 D33045 DOMELIGHT 04/20/01 . . 9 . . 999999 . . END OF FILE . . 00/00/00 Start Write heading(s) COUNT1 = 0 COUNT2 = 0 Read CODE, PART, TYPE, DATE DOWHILE CODE ≠ 9 IF CODE = 1 THEN COUNT1 = COUNT1 + 1 ELSE IF CODE = 2 THEN COUNT2 = COUNT2 + 1 ELSE Write ‘Bad Input’ ENDIF ENDIF Write CODE, PART, TYPE, DATE Read CODE, PART, TYPE, DATE ENDDO Write COUNT1, COUNT2 Stop 12 Solution: Using Trailer record & Modules Identify main tasks performed by the program Task 1: Write the headings Task 2: Initialize counters Task 4: Write totals for each plant. Task 3:Write detail records Note: Many ways for identifying mains tasks 13 Solution: Using Trailer record & Modules 14 Structure Chart (or Hierarchy Chart) – Shows relationships of all modules within a program Higher level: Represents the Control Module (or Driver Module) Lower level: 1) Represents Modules that may be given control during program execution 2) Each module performs one or a small number of tasks. Solution: Using Trailer record & Modules 15 Overall Control program Flowchart & Pseudocode A000 Start Process headings (B000) Process initialization (B010) Read CODE, PART, TYPE, DATE IF CODE = 9 THEN Write ‘No data’ ELSE DOWHILE CODE ≠ 9 Process a detail record (B020) Read CODE, PART, TYPE, DATE ENDDO Process totals (B030) ENDIF Stop Solution: Using Trailer record & Modules 16 Modules’ Flowcharts & Pseudocodes B000 WRITE HEADING(S) RETURN B000 Enter Write headings Return B010 COUNT1 = 0 COUNT2 = 0 RETURN B010 Enter COUNT1 = 0 COUNT2 = 0 Return B030 WRITE ‘TOTAL EFFECTIVE PLANT 1 PARTS:’, COUNT1 WRITE ‘TOTAL EFFECTIVE PLANT 2 PARTS:’, COUNT2 RETURN B030 Enter Write ‘Total defective plant 1 parts:’, COUNT1 Write ‘Total defective plant 2 parts:’, COUNT2 Return Solution: Using Trailer record & Modules 17 Modules’ Flowcharts & Pseudocodes B020 Enter IF CODE = 1 THEN COUNT1 = COUNT1 + 1 ELSE IF CODE = 2 THEN COUNT2 = COUNT2 + 1 ELSE Write ‘Bad Input’ ENDIF ENDIF Write CODE, PART, TYPE, DATE Return Solution: Using End-Of-File condition 18 Not always necessary to physically place a trailer record Most programming languages support a built-in function for testing the End-of-File A000 Start Process headings (B000) Process initialization (B010) Read a record IF end of file (EOF) THEN Write ‘No data’ ELSE DOWHILE not end of file (not EOF) Process a detail record (B020) Read a record ENDDO Process totals (B030) ENDIF Stop Exercise Redo the Weekly Payroll problem done during Week 2 (see next 3 slides): 1) Using a Trailer record (NUM = 999) 2) Using an End-Of-File condition Exercise : Weekly Payroll problem 20 Exercise 19 (Chapter 4) Construct a program flowchart and corresponding pseudocode to solve the following problem: ABC company needs a weekly payroll report for its salespeople. Input to the program is a salesperson’s name, number, and weekly sales. Output is the salesperson’s name, number, and pay. Each salesperson receives a base pay of $300 as well as a 10% commission on his or her total sales up to and including $500. Any sales over $500 merit a 15% commission for the employee. (For example, if sales = $600, then pay = $300 + $50 [or .10 * 500] + $15 [.15 * 100] = $350). Use a DOWHILE loop and a counter to compute the weekly payroll for exactly 20 employees. Initial Solution: (Program Flowchart) START COUNT = 0 COUNT < 20 ? NO STOP YES READ NAME, NUM, SALES NO SALES > 500 ? YES PAY = PAY = 300 * (SALES * .10) COUNT = COUNT + 1 300 + (500 * .10) + ((SALES – 500) * .15) WRITE NAME, NUM, PAY 21 Initial Solution: (Pseudocode) Start COUNT = 0 DOWHILE COUNT < 20 Read NAME, NUM, SALES IF SALES > 500 THEN PAY = 300 + (500 * .10) + ((SALES – 500) * .15) ELSE PAY = 300 + (SALES * .10) ENDIF Write NAME, NUM, PAY COUNT = COUNT + 1 ENDDO Stop 22 Solution: Using Trailer record (Program Flowchart) (To be done in class) 23 Solution: Using Trailer record (Pseudocode) (To be done in class) 24 Solution: Using EOF (Program Flowchart) (To be done in class) 25 Solution: Using Trailer record (Pseudocode) (To be done in class) 26
© Copyright 2025 Paperzz