Combination of 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 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.
Fundamentals of Algorithms MCS - 2 Lecture # 4
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
Computational Thinking for Information Technology
 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
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
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.
ALGORITHMS Algorithm: Is an ordered set of unambiguous, executable steps defining a terminating process Unambiguous:during the execution of an algorithm,
1 TEL 104 / MKK Fundamental Programming: Lecture 4.
Module Cohesion and Coupling Lecture 10. How to determine a good module? Cohesion: a measure of the internal strenght of a module – how closely the elements.
Array Processing Lecture 7.
Repetition Control Structures
Pseudocode.
Pseudocode Algorithms Using Sequence, Selection, and Repetition.
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.
Pseudocode algorithms using sequence, selection and repetition
DCT 1123 Problem Solving & Algorithms
Looping While-continue.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
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.
Presentation © Copyright 2002, Bryan Meyers Top-Down, Structured Program Design Chapter 5.
CHAPTER 3 SELECTION STRUCTURE.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Controlling Function Behavior Sequence, Selection and Repetition.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
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.
Cs413_design04.ppt Design and Software Development Design : to create a functional interface that has high usability Development : an organized approach.
Pseudocode Algorithms Using Sequence, Selection, and Repetition
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Pseudocode Algorithms Using Sequence, Selection, and Repetition Simple Program Design Third Edition A Step-by-Step Approach 6.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Chapter 2 Murach's JavaScript and jQuery, C2© 2012, Mike Murach & Associates, Inc.Slide 1.
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.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Introduction to Computer Programming
while Repetition Structure
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
PROGRAM CONTROL STRUCTURE
JavaScript: Control Statements I
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 6: Repetition Statements
REPETITION Why Repetition?
Introduction to Pseudocode
Presentation transcript:

Combination of Sequence, Selection and Repetition Lecture 6

Example 6.1 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.

Defining diagram Input Processing Output Number1 Number2 Prompt for numbers Get numbers Calculate sum Calculate product Calculate average Display sum, product, average Display `*` Sum Product Average `*`

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 operand with the AND logical operator.

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 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 course type; regular (R/C) or short course (S/C) . 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`.

Defining diagram Input Processing Output `s`records Print heading Number Name Address Age Gender Course type `U` records Print heading Read student records Select `s`records Print selected records Heading line Selected student records number name address

Control structures required A DOWHILE loop to control the repetition An IF statement to select `S`records.

Solution Algorithm Print_student_records Print `STUDENT LIST` heading read student record DOWHILE more record 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 report of all female students who are enrolled for short course. The report is to be headed `SHORT COURSE FEMALE STUDENTS` and is to show the student‘s number, name, address and age.

Defining diagram Input Processing Output `s`records Print heading Number Name Address Age Gender Course type `U` records Print heading Read student records Select S/C female students Print selected records Heading line Selected student records number name address age

Control structures required A DOWHILE loop to control the repetition An IF statement to select `S` records, female and Short Course (S/C) students.

Solution Algorithm 1 (use non-linear nested IF) Produce_short_course_female_list Print `SHORT COURSE FEMALE STUDENTS`heading Read student record DOWHILE more records IF student record = `S` record THEN IF course_type = S/C THEN IF gender = female THEN Print student_number, name, address, age ENDIF ENDDO END

Solution Algorithm 2 (use a nested and compound IF) Produce_short_course_female_list Print `SHORT COURSE FEMALE STUDENTS`heading Read student record DOWHILE more records IF student record = `S` record THEN IF (course_type = S/C AND gender = female) THEN Print student_number, name, address, age ENDIF ENDDO END

Solution Algorithm 3 (use a compound IF) Produce_short_course_female_list Print `SHORT COURSE FEMALE STUDENTS`heading Read student record DOWHILE more records IF student record = `S` record AND course_type = S/C AND gender = female THEN Print student_number, name, address, age ENDIF 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 `SHORT COURSE 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.

Defining diagram Input Processing Output `s`records Number Name Address Age Gender Course type `U` records Print heading Read student records Select S/C female students Print selected records Compute total students Compute total selected students Print totals Heading line Selected student records number name address age Total_students Total_Selected_students

Control structures required A DOWHILE loop to control the repetition IF statements to select `S`, female and Short Course (S/C) students. Accumulators for total_selected_students and total_students.

Solution Algorithm Produce_short_course_female_list Print `SHORT COURSE FEMALE STUDENTS`heading Set total_students to zero Set total_selected_students to zero Read student record DOWHILE records exist IF student record = `S` record THEN increment total_students IF (course_type = S/C AND gender = female) THEN increment total_selected_students Print student_number, name, address, age ENDIF ENDDO Print total_students Print 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 course type from the `S` records (student records) and the unit number and unit name from the `U`records (enrolled units records) as follow: STUDENT REPORT Student name ...................... Student number ...................... Course type ...................... Enrolled units ...................... ................ ...................... ................ At the end of the report, print the total number of students enrolled.

Defining diagram Input Processing Output `s`records Number Name Address Age Gender Course type `U` records Unit_number Unit_name Print heading Read student records Print `s` record details Print `u` record details Compute total students Print total students Heading line Detail lines: name number course_type unit_number unit_name Total_students

Control structures required A DOWHILE loop to control the repetition An IF statement to select `S` and `U` records. An accumulator for total_students.

Solution Algorithm Print_student_report Print `STUDENT REPORT`heading Set total_students to zero Read student record DOWHILE records exist IF student record = `S` THEN add 1 to total_students Print `Student name`, name Print `Student number`, number Print `Course type`, course_type Print `Enrolled units` ELSE IF student record = `U` THEN Print unit_number, unit_name Print `Student record error` ENDIF ENDDO Print `Total students`, total_students END

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 the sale, as follows: Tax code Sales tax Tax exempt 1 3% 2 5% 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 owing.

Defining diagram Input Processing Output Sales_record customer_number Name Sales_amt Tax_code Print heading Read sales records Calculate sales tax Calculate total amount Print customer details Heading line Detail lines: name sales_amt sales_tax total_amount

Control structures required A DOWHILE loop to control the repetition A case statement to calculate the sales_tax

Solution Algorithm 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.05 ENDCASE total_amt = sales_amt + sales_tax Print customer_number, name, sales_amt, sales_tax, total_amt ENDDO END

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 a letter grade on the report. The letter grade is determined as follows: A = 90-100% B = 80-89% C = 70-79% D = 60-69% E = 0-59%

Defining diagram Input Processing Output Student_test_records student_number Name Test_score Print heading Read student records Calculate test percentage Calculate letter grade Print student details Heading line Student detail: name test_score grade

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 (CASE construct can not be used here  why ?)

Solution Algorithm Print_student_results Print `STUDENT TEST GRADES`heading Read student record DOWHILE not EOF percentage = test_score * 2 IF percentage >= 90 THEN grade = `A` ELSE IF percentage >= 80 THEN grade = `B` IF percentage >= 70 THEN grade = `C` IF percentage >= 60 THEN grade = `D` grade = `F` ENDIF Print student_number, name, test_score, grade Read student_record ENDDO END

Example 6.8 Gas Supply Billling The domestic Gas Supply Company records its customers‘ gas usage figures on a customer usage file. Each record on the file contains the customer number, customer name, customer address, and gas usage expressed in cubic metres. 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. The company bills its customers according to the following rate: if the customer‘s usage is 60 cubic metres or less, a rate of $2.00 per cubic metre is applied; if the customer‘s usage is more than 60 cubic metres, then a rate pf $ 1.75 per cubic metre is applied for the first 60 cubic metres and $1.50 per cubic metre for the remaining usage. At the end of the report, print the total number of customers and the total amount owing to the company.