Title: Pseudocode Algorithms Using Sequence, Selection, and Repetition
1Pseudocode Algorithms Using Sequence, Selection,
and Repetition
2Objectives
- In this chapter you will be able to
- Develop solution algorithms to eight typical
programming problems using sequence, selection,
and repetition constructs
3Eight 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
41 Defining the Problem
- It is important that you divide the problem into
its three components - Input
- Output
- Processing
52 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
63 The Solution Algorithm
- Having defined the problem and determined the
required control structures, devise a solution
algorithm and represent it using pseudocode
74 Desk Checking
- You will need to desk check each of the developed
algorithms with two or more test cases
8Example 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)
9Example 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
10Example 6.2 Print Student Records
- A file of student records consists of S records
and U records. An S record contains the
students 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 students number, name, and
address on a STUDENT LIST. - A Defining Diagram (shown on page 75 of the
text)
11Example 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
12Example 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 students
number, name, address, and age - A Defining Diagram (shown on page 76)
13Example 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
14Example 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)
15Example 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
16Example 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)
17Example 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
18Example 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 customers 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)
19Example 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
20Example 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 students 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)
21Example 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
22Example 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 customers 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)
23Example 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
24Summary
- 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