Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pseudocode Algorithms Using Sequence, Selection, and Repetition

Similar presentations


Presentation on theme: "Pseudocode Algorithms Using Sequence, Selection, and Repetition"— Presentation transcript:

1 Pseudocode Algorithms Using Sequence, Selection, and Repetition

2 Simple Program Design, Fourth Edition Chapter 6
Objectives In this chapter you will be able to: Develop solution algorithms to eight typical programming problems using sequence, selection, and repetition constructs Simple Program Design, Fourth Edition Chapter 6

3 Eight Solution Algorithms
This chapter develops solution algorithms to eight programming problems of increasing complexity All the algorithms will use a combination of sequence, selection, and repetition constructs The algorithms have been designed to be interactive or to process sequential files Simple Program Design, Fourth Edition Chapter 6

4 Simple Program Design, Fourth Edition Chapter 6
1 Defining the Problem It is important that you divide the problem into its three components: Input Output Processing Simple Program Design, Fourth Edition Chapter 6

5 2 The Control Structures Required
Once the problem has been defined, write down the control structures (sequence, selection, and repetition) that may be needed, as well as any extra variables that the solution may require Simple Program Design, Fourth Edition Chapter 6

6 3 The Solution Algorithm
Having defined the problem and determined the required control structures, devise a solution algorithm and represent it using pseudocode Simple Program Design, Fourth Edition Chapter 6

7 Simple Program Design, Fourth Edition Chapter 6
4 Desk Checking You will need to desk check each of the developed algorithms with two or more test cases Simple Program Design, Fourth Edition Chapter 6

8 Example 6.1 Process Number Pairs
Design an algorithm that will prompt for and receive pairs of numbers from an operator at a terminal and display their sum, product, and average on the screen. If the calculated sum is over 200, an asterisk is to be displayed beside the sum. The program is to terminate when a pair of zero values is entered A Defining diagram (shown on page 74) Simple Program Design, Fourth Edition Chapter 6

9 Example 6.1 Process Number Pairs
B Control Structures Required A DOWHILE loop to control the repetition An IF statement to determine if an asterisk is to be displayed Note the use of the NOT operator with the AND logical operator C Solution algorithm Refer to the code of the solution algorithm on page 74 of the textbook Simple Program Design, Fourth Edition Chapter 6

10 Example 6.2 Print Student Records
A file of student records consists of ‘S’ records and ‘U’ records. An ‘S’ record contains the student’s number, name, age, gender, address, and attendance pattern; full-time (F/T) or part-time (P/T). A ‘U’ record contains the number and name of the unit or units in which the student has enrolled. There may be more than one ‘U’ record for each ‘S’ record. Design a solution algorithm that will read the file of student records and print only the student’s number, name, and address on a ‘STUDENT LIST’. A Defining Diagram (shown on page 75 of the text) Simple Program Design, Fourth Edition Chapter 6

11 Example 6.2 Print Student Records
B Control Structures Required A DOWHILE loop to control the repetition An IF statement to select ‘S’ records C Solution algorithm Examine the pseudocode illustrated on page 75 of the textbook Simple Program Design, Fourth Edition Chapter 6

12 Example 6.3 Print Selected Students
Design a solution algorithm that will read the same student file as in Example 6.2, and produce a report of all female students who are enrolled part-time. The report is to be headed ‘PART TIME FEMALE STUDENTS’ and is to show the student’s number, name, address, and age A Defining Diagram (shown on page 76) Simple Program Design, Fourth Edition Chapter 6

13 Example 6.3 Print Selected Students
B Control Structures Required A DOWHILE loop to control the repetition An IF statement or statements to select ‘S’, female, and part- time (P/T) students C Solution Algorithm Several algorithms for this problem will be presented, and all are equally correct The algorithms only differ in the way the IF statement is expressed, as shown in the code on pages 76 and in Solution 2 on page 77 of the textbook Simple Program Design, Fourth Edition Chapter 6

14 Example 6.4 Print and Total Selected Students
Design a solution algorithm that will read the same student file as in Example 6.3 and produce the same ‘PART TIME FEMALE STUDENTS’ report. In addition, you are to print at the end of the report the number of students who have been selected and listed, and the total number of students on the file A Defining Diagram (shown on page 78) Simple Program Design, Fourth Edition Chapter 6

15 Example 6.4 Print and Total Selected Students
B Control Structures Required A DOWHILE loop to control the repetition IF statements to select ‘S’, female, and P/T students Accumulators for total_selected_students and total_students C Solution Algorithm Examine the code listed on page 78 of the textbook Simple Program Design, Fourth Edition Chapter 6

16 Example 6.5 Print Student Report
Design an algorithm that will read the same student file as in Example 6.4 and for each student, print the name, number, and attendance pattern from the ‘S’ records (student records) and the unit number and unit name from the ‘U’ records (enrolled units records) as shown on page 79 of the textbook. At the end of the report, print the total number of students enrolled A Defining Diagram (shown on page 79) Simple Program Design, Fourth Edition Chapter 6

17 Example 6.5 Print Student Report
B Control Structures Required A DOWHILE loop to control the repetition An IF statement to select ‘S’ or ‘U’ records An accumulator for total_students C Solution Algorithm Examine the code illustrated on page 80 of the textbook for this program problem Simple Program Design, Fourth Edition Chapter 6

18 Example 6.6 Produce Sales Report
Design a program that will read a file of sales records and produce a sales report. Each record in the file contains a customer’s number, name, a sales amount, and a tax code. The tax code is to be applied to the sales amount to determine the sales tax due for that sale, as shown in the table on page 80 of the textbook. The report is to print a heading ‘SALES REPORT’, and detail lines listing the customer number, name, sales amount, sales tax, and the total amount owed A Defining Diagram (shown on page 81) Simple Program Design, Fourth Edition Chapter 6

19 Example 6.6 Produce Sales Report
B Control Structures Required A DOWHILE loop to control the repetition A case statement to calculate the sales_tax C Solution Algorithm Examine the code illustrated on page 81 which is the solution for this program problem Simple Program Design, Fourth Edition Chapter 6

20 Example 6.7 Student Test Results
Design a solution algorithm that will read a file of student test results and produce a student test grades report. Each test record contains the student number, name, and test score (out of 50). The program is to calculate for each student the test score as a percentage and to print the student’s number, name, test score (out of 50), and letter grade on the report. The letter grade is determined using the listing on page 81 of the text A Defining Diagram (shown on page 82) Simple Program Design, Fourth Edition Chapter 6

21 Example 6.7 Student Test Results
B Control Structures Required A DOWHILE loop to control the repetition A formula to calculate the percentage A linear nested IF statement to calculate the grade. (The case construct cannot be used here, as CASE is not designed to cater for a range of values.) C Solution Algorithm Refer to the code shown on page 82 of the textbook Simple Program Design, Fourth Edition Chapter 6

22 Example 6.8 Gas Supply Billing
Refer to the background of the Domestic Gas Supply company on page 83 of the text. Design a solution algorithm that will read the customer usage file, calculate the amount owing for gas usage for each customer, and print a report listing each customer’s number, name, address, gas usage, and the amount owing. Read the remainder of the problem specification on page 83 and at the end of the report, print the total number of customers and the total amount owed to the company A Defining Diagram (shown on page 83) Simple Program Design, Fourth Edition Chapter 6

23 Example 6.8 Gas Supply Billing
B Control Structures Required A DOWHILE loop to control the repetition An IF statement to calculate the amount_owing Accumulators for total_customers and total_amount_owing C Solution Algorithm Examine the code shown on page 84 of the textbook, which shows the pseudocode for this problem Simple Program Design, Fourth Edition Chapter 6

24 Simple Program Design, Fourth Edition Chapter 6
Summary This chapter developed solution algorithms to eight typical programming problems The approach to all eight problems followed the same pattern: The problem was defined, using a defining diagram The control structures required were written down, along with any extra variables required The solution algorithm was produced, using pseudocode and the three basic control structures: sequence, selection, and repetition Simple Program Design, Fourth Edition Chapter 6


Download ppt "Pseudocode Algorithms Using Sequence, Selection, and Repetition"

Similar presentations


Ads by Google