DCT 1123 PROBLEM SOLVING ALGORITHM CONTROL STRUCTURE COMBINATION
DCT 1123 PROBLEM SOLVING & ALGORITHM CONTROL STRUCTURE - COMBINATION
CONTENTS � In this chapter you will be able to: � Develop solution algorithms to FIVE typical programming problems using sequence, selection, and repetition constructs
Five Solution Algorithms � This chapter develops solution algorithms to five programming problems of increasing complexity. � All the algorithms will use a combination of sequence, selection, and repetition constructs.
A Defining the Problem � It is important that you divide the problem into its three components: 1. Input 2. Output 3. Processing
B 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
C The Solution Algorithm � Having defined the problem and determined the required control structures, ◦ devise a solution algorithm and represent it using pseudo code
D Desk Checking � You will need to desk check each of the developed algorithms with two or more test cases.
Example 7. 1 Process Number Pairs
Example 7. 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
Example 7. 1 Process Number Pairs �A Defining diagram Input Processing Output number 1 Prompt for numbers Sum number 2 Get numbers product Calculate sum average Calculate product * Calculate average Display sum, product, average Display *
Example 7. 1 Process Number Pairs �B Control Structure Required 1. A DOWHILE loop to control the repetition 2. IF statements to determine if an asterisk is to be displayed
Example 7. 1 Process Number Pairs • C Solution algorithm • START Process_number_pairs – – • – – – END Set sum to zero Prompt for number 1, number 2 Get number 1, number 2 DOWHILE NOT (number 1=0 AND number 2=0) • sum = number 1+number 2 • product = number 1*number 2 • average = sum/2 • IF sum > 200 THEN – Display sum, *, product, average • ELSE – Display sum, product, average • ENDIF Prompt for number 1, number 2 Get number 1, number 2 ENDDO
Example 7. 1 Process Number Pairs • C Solution algorithm
Example 7. 2 Print Student Records
Example 7. 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’.
Example 7. 2 Print Student Records �A Defining Diagram Input Processing Output ‘s’ records Print headings Heading line • number Read student records Selected student records • name Select ‘s’ records • number • address Print selected records • name • age • gender • attendance_pattern ‘u’ records • address
Example 7. 2 Print Student Records �B Control Structure Required 1. A DOWHILE loop to control the repetition 2. An IF statements to select ‘S’ record
Example 7. 2 Print Student Records � C Solution algorithm � START Print_student_records ◦ Print ‘STUDENT LIST’ headings ◦ Read student record ◦ DOWHILE move records exist �IF student record = “S” record THEN �Print student_number, name, address �ENDIF �Read student record ◦ ENDDO � END
Example 7. 2 Print Student Records � C Solution algorithm
Example 7. 3 Print Selected Students
Example 7. 3 Print Selected Students � Design a solution algorithm that will read the same student file as in Example 7. 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
Example 7. 3 Print Selected Students A Defining Diagram Input Processing Output ‘s’ records Print headings Heading line • number Read student records Selected student records • name Select P/T female students • number • address Print selected records • name • age • address • gender • age • attendance_pattern ‘u’ records
Example 7. 3 Print Selected Students �B Control Structure Required 1. A DOWHILE loop to control the repetition 2. IF statements to print the selected students
Example 7. 3 Print Selected Students �C � � Solution algorithm START Produce_part_time_female_list ◦ Print ‘PART TIME FEMALE STUDENTS’ heading ◦ Read student record ◦ DOWHILE more records � IF student record = “S” record THEN IF attendance_pattern = P/T THEN � IF gender = female THEN � Print student_number, name, address, age � ENDIF � Read student record ◦ ENDDO END
Example 7. 3 Print Selected Students �C Solution algorithm
Example 7. 4 Produce Sales Report
Example 7. 4 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 follow : Tax code Sales tax 0 Tax exempt The report is to print a 1 heading ‘SALES REPORT’, and detail 3% lines listing the customer number, name, sales amount, sales 2 5% tax, and the total amount owed
Example 7. 4 Produce Sales Report A Defining Diagram Input Processing Output sales record Print headings Heading line • customer_number Read sales records Detaillines • name Calculate sales tax • customer_number • sales_amt Calculate total amount • name • tax_coode Print customer details • sales_amt • sales_tax • total_amount
Example 7. 4 Produce Sales Report �B Control Structure Required 1. A DOWHILE loop to control the repetition 2. A case statement to calculate the sales_tax
Example 7. 4 Produce Sales Report �C � � Solution algorithm START Produce_sales_report ◦ Print ‘SALES REPORT’ heading ◦ Read sales record ◦ DOWHILE not EOF � CASE of tax_code � 0: sales_tax = 0 � 1: sales_tax = sales_amt * 0. 03 � 2 : sales_tax = sales_amt * 0. 0 � ENDCASE � Total_amt = sales_amt+sales_tax � Print customer_number, name, sales_amt, sales-tax, total_amt � Read sales_report ◦ ENDDO END
Example 7. 4 Produce Sales Report �C Solution algorithm
Example 7. 5 Student Test Results
Example 7. 5 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.
Example 7. 5 Student Test Results A Defining Diagram Input Processing Output student test record Print headings Heading line • student_number Read student records Student details • name Calculate test percentage • student_number • test_score Calculate letter grade • name Print student details • test_score • grade
Example 7. 5 Student Test Results � B Control Structures Required 1. A DOWHILE loop to control the repetition 2. A formula to calculate the percentage 3. 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. )
Example 7. 5 Student Test Results � � C Solution algorithm START Print_student_results ◦ Print ‘Student Test Grades’ heading ◦ Read student record ◦ DOWHILE NOT EOF ◦ Percentage = test_score*2 ◦ IF percentage > 89 THEN ◦ grade = A ◦ ELSE IF percentage >89 THEN ◦ grade = B ◦ ELSE IF precentage > 79 THEN ◦ grade = C ◦ ELSE IF percentage > 69 THEN grade = D ◦ ELSE ◦ grade = F ◦ ENDIF ◦ Print studet_number, name, test_score ◦ Read student record ◦ ENDDO ◦ END
Example 7. 5 Student Test Results � C Solution algorithm
Summary � � This chapter developed solution algorithms to five typical programming problems. The approach to all five problems followed the same pattern: 1. The problem was defined, using a defining diagram. 2. The control structures required were written down, along with any extra variables required. 3. The solution algorithm was produced, using pseudocode and the three basic control structures: sequence, selection, and repetition
- Slides: 39