Pseudocode Algorithms Using Sequence, Selection, and Repetition.

Slides:



Advertisements
Similar presentations
Repetition There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is.
Advertisements

Repetition Control Structures
Repetition Control Structures School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 9, Friday 3/07/2003)
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
 Control structures  Algorithm & flowchart  If statements  While statements.
Selection control structures
Repetition Control Structure
TEL 104 / MKK Fundamental Programming: Lecture 3
Nassi-Schneidermann Diagrams Lecture 13. Three basic control Structures.
Steps in Program Development
Logic and Algorithm. Developing an algorithm To help the initial analysis, the problem should be divided into 3 separate components: 1.Input: a list of.
1 TEL 104 / MKK Fundamental Programming: Lecture 4.
Program Design and Development
Pseudocode and Algorithms
Modularisation II Lecture 9. Communication between modules Also known as intermodule communication. The fewer and the simpler the communications, the.
Repetition Control Structures
Pseudocode.
Pseudocode.
DCT 1123 Problem Solving & Algorithms
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Combination of Sequence, Selection and Repetition
Pseudocode algorithms using sequence, selection and repetition
DCT 1123 Problem Solving & Algorithms
Looping While-continue.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
Array Processing.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Developing an Algorithm
Any Questions? Control Breaks Agenda Control Breaks –Also known as Reports with sub-totals.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Pseudocode Algorithms Using Sequence, Selection, and Repetition
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
2Object-Oriented Program Development Using C++ 3 Basic Loop Structures Loops repeat execution of an instruction set Three repetition structures: while,
Pseudocode Algorithms Using Sequence, Selection, and Repetition Simple Program Design Third Edition A Step-by-Step Approach 6.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Flowcharts. Symbol Terminal Symbol: indicates the starting or stopping pointin the logic. Input/Output Symbol: Represents an input or output process in.
Flowcharts Lecture 12.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
LO: We’re learning to outline a program using Pseudo Code.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
while Repetition Structure
Chapter 5: Control Structures II
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Chapter 5: Control Structures II
PROGRAM CONTROL STRUCTURE
Chapter 5: Control Structures II
For Monday Read WebCT quiz 18.
CHAPTER 2 & 3: Pseudocode and Developing and Algorithm
Pseudocode.
Pseudocode algorithms using sequence, selection and repetition
For Wednesday No new reading No quiz.
3 Control Statements:.
CHAPTER 4 Iterative Structure.
Introduction to Pseudocode
Presentation transcript:

Pseudocode Algorithms Using Sequence, Selection, and Repetition

Objectives In this chapter you will be able to: Develop solution algorithms to eight typical programming problems using sequence, selection, and repetition constructs

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 Eight Solution Algorithms

It is important that you divide the problem into its three components: –Input –Output –Processing 1 Defining the Problem

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

3 The Solution Algorithm Having defined the problem and determined the required control structures, devise a solution algorithm and represent it using pseudocode

4 Desk Checking You will need to desk check each of the developed algorithms with two or more test cases

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)

Example 6.1 Process Number Pairs A Defining diagram InputProcessingOutput number1Prompt for numbersSum number2Get numbersproduct Calculate sumaverage Calculate product* Calculate average Display sum, product, average Display *

Example 6.1 Process Number Pairs B Solution algorithm Process_number_pairs Set sum to zero Prompt for number1, number2 Get number1, number2 DOWHILE NOT (number1=0 AND number2=0) sum = number1+number2 product = number1*number2 average = sum/2 IF sum > 200 THEN Display sum, *, product, average ELSE Display sum, product, average ENDIF Prompt for number1, number2 Get number1, number2 ENDDO END

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)

Example 6.2 Print Student Records A Defining Diagram InputProcessingOutput ‘s’ recordsPrint headingsHeading line numberRead student recordsSelected student records nameSelect ‘s’ records number addressPrint selected records name age address gender attendance_pattern ‘u’ records

Example 6.2 Print Student Records B Solution algorithm 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 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

Example 6.3 Print Selected Students A Defining Diagram InputProcessingOutput ‘s’ recordsPrint headingsHeading line numberRead student recordsSelected student records nameSelect P/T female students number addressPrint selected records name age address gender age attendance_pattern ‘u’ records

Example 6.3 Print Student Records B Solution algorithm – non-linear nested IF 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 6.3 Print Student Records B Solution algorithm – nested and compound IF statement 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 AND gender = female THEN Print student_number, name, address, age ENDIF Read student record ENDDO END

Example 6.3 Print Student Records B Solution algorithm – compound IF Produce_part_time_female_list Print ‘PART TIME FEMALE STUDENTS’ heading Read student record DOWHILE more records IF student record = “S” record AND attendance_pattern = P/T AND gender = female THEN Print student_number, name, address, age ENDIF Read student record ENDDO END

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)

Example 6.4 Print and Total Selected Students B Control Structures Required 1.A DOWHILE loop to control the repetition 2.IF statements to select ‘S’, female, and P/T students 3.Accumulators for total_selected_students and total_students C Solution Algorithm Examine the code listed on page 78 of the textbook

Example 6.4 Print Student Records B Solution algorithm Produce_part_time_female_list Print ‘FEMALE PART-TIME STUDENTS’ heading Set total_students to zero Set total_selected_students to zero Read student record DOWHILE more records IF student record = “S” record THEN increment total_students IF attendance_pattern = P/T AND gender = female THEN inrement total_selected_students Print student_number, name, address, age ENDIF Read student record ENDDO Print total_students, total_selected_students END

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)

Example 6.5 Print Student Report B Control Structures Required 1.A DOWHILE loop to control the repetition 2.An IF statement to select ‘S’ or ‘U’ records 3.An accumulator for total_students C Solution Algorithm Examine the code illustrated on page 80 of the textbook for this program problem

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)

Example 6.6 Produce Sales Report B Control Structures Required 1.A DOWHILE loop to control the repetition 2.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

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)

Example 6.7 Student test results A Defining Diagram InputProcessingOutput student test recordPrint headingsHeading line student_numberRead student recordsStudent details nameCalculate test percentage student_number test_scoreCalculate letter grade name Print student details test_score grade

Example 6.7 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.) C Solution Algorithm Refer to the code shown on page 82 of the textbook

B Solution algorithm 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 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)

Example 6.8 Gas Supply Billing B Control Structures Required 1.A DOWHILE loop to control the repetition 2.An IF statement to calculate the amount_owing 3.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

Summary This chapter developed solution algorithms to eight typical programming problems The approach to all eight 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