Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage


Introduction to Programming - (CPTR125) - Lectures  


 Return to Courses | Course Content  



Introduction to Programming (CPTR 125)

by David Siguelnitzky, MIS; MTE

Lecture 06 - Version 1.0


Pseudocode algorithms using sequence, selection and repetition

 


Lecture Outline

- Eight Solution algorithms

- Defining the problem
- The control structures required
- The solution algorithm
- Desk checking

 

Pseudocode algorithms using sequence, selection and repetition

This chapter shows the program development for eight programs that have more than one control structure in them

Eight solution algorithms

We will now look at eight programs of increasing complexity. We have added one step to our design process called: The control structures required. This step comes after the defining diagram and before the solution algorithm.

a) Defining the problem

Breaks the problem up into output, input, and processes.

b) The control structures required

Write the control structures needed for this problem, such as sequence, selection, and repetition, along with any extra variables needed to support the structures. NOTE: You can have more than one of each structure.

c) The solution algorithm

Here is the PseudoEnglish outline of the algorithm.

d) Desk checking

Here, you find some test data, and expected answers, and then try them out on the algorithm.

Example 6.1 Process number pairs

You are to get and process pairs of numbers, until a pair of zero values is received. Display the numbers, their sum, product and average and flag values over 200.

A Defining diagram

Here is the IPO chart for this problem. NOTE: It should include the loop and decisions! It doesn't. That is part of the processes.

B Control structures required

0. We need to print the report header.
1. You will need a loop! DOWHILE will work.
2. You also need a selection to test the values for being greater than 200.
3. The condition for the DOWHILE can be written without the NOT! It would be: (number_1 <> 0 OR number_2 <> 0)
NOT operator should be avoided!

C Solution algorithm

Here is the solution algorithm with the rewritten condition.

Example 6.2 Print student records

Design a program to read a file of different types of student records and print out the student's number, name, and address.

A Defining diagram

Here is the IPO chart. It should have included the decisions and loops! It doesn't.

B Control structures required

0. Print report header.
1. We need a loop, a DOWHILE should work.
2. We need a selection to test for which type of records. Of course that will be an IF statement.

C Solution algorithm

Here is the algorithm.

Example 6.3 Print selected students

Design a program to read the above student file and only print female, part-time students.

A Defining diagram

Here is the IPO chart. It should have included the loop and selections.

B Control structures required

0. Print report header.
1. We need a loop, DOWHILE will work.
2. We need to test for female.
3. We need to test for part-time.
4. We need to test for record type.

C Solution algorithm

Here is the solution algorithm using three nested IFs.

Here is the solution algorithm using a compound IF to test for female and part-time.

Here is the solution algorithm using a single compound IF combining al three tests.

Example 6.4 Print and total selected students

This problem is the same as the above one, but, we also want to print total female-part-time students and total students.

A Defining diagram

Here is the defining diagram which is missing all the tests and loops.

B Control structures required

0. Print report header.
1. We need a loop.
2. We need the three selections we have before.
3. We need additional variables to keep the totals in.

C Solution algorithm

Here is the solution algorithm with the added total logic. Now, we do need two separate IFs so we can count all the students.

Example 6.5 Print Student Report

Design a program to read the same student file as before, but, we need to print information on each student using the format shown below.

A Defining diagram

Here is the IPO chart missing the loop and selections.

B Control structures required

0. Print report header.
1. We need a loop.
2. We need to selection between S and U records.
3. We need to accumulator total students.

C Solution algorithm

Here is the solution diagram. It even includes an error message if the record is not of type U or S!

Example 6.6 Produce sales report

Write a program to read a sales record file and produce a sales report. It will process the following tax code table.

A Defining diagram

Here is the IPO chart missing the loop and selections.

B Control structures required

0. Print report header.
1. We will need a loop.
2. We need a CASE statement for the tax code selection.

C Solution algorithm

Here is the solution algorithm

Example 6.7 Student test results

Design a program to read a student test results file and produce a student test grade report. Grades are based on the following table.

 

A Defining diagram

Here is the IPO chart missing the loop and selections.

B Control structures required

0. Print report header.
1. We need a loop.
2. We need nested IFs to calculate the grade (from 1 - 10 to percentage).

C Solution algorithm

Here is the solution algorithm.

Example 6.8 Gas supply billing

Design a program to read the Customer Usage file and produce a report with totals.

A Defining diagram

Here is the IPO chart missing the loop and selection.

B Control structures required

0. Print report header.
1. We need a loop.
2. We need a selection on amount of usage.
3. We need to accumulate total customers and total owed.

C Solution algorithm

Here is the solution diagram.

Required reading: Simple Program Design - Lesley Anne Robertson - Chapter 6.


Used with permission - Copyright © 2005 by James L. Fuller - Simple Program Design - Lesley Anne Robertson - Course Technology.



 Return to Courses | Course Content  


 Home | Résumé | Courses | Contact | Useful Links | Favorite Links | USC - Homepage