1 TEL 104 / MKK Fundamental Programming: Lecture 4.

Slides:



Advertisements
Similar presentations
Repetition Control Structures
Advertisements

Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language.
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
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
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.
Program Design and Development
Modularisation II Lecture 9. Communication between modules Also known as intermodule communication. The fewer and the simpler the communications, the.
Repetition Control Structures
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
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.
Combination of Sequence, Selection and Repetition
Pseudocode algorithms using sequence, selection and repetition
Chapter 9 Describing Process Specifications and Structured Decisions
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
First Steps in Modularization Simple Program Design Third Edition A Step-by-Step Approach 8.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Developing an Algorithm
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Developing an Algorithm
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
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.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
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.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Week 4 Program Control Structure
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.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
For Monday Read WebCT quiz 18.
CHAPTER 2 & 3: Pseudocode and Developing and Algorithm
Program Design Introduction to Computer Programming By:
Pseudocode.
Pseudocode algorithms using sequence, selection and repetition
SELECTION STATEMENTS (2)
Understanding the Three Basic Structures
Iteration: Beyond the Basic PERFORM
Boolean Expressions to Make Comparisons
Week 3 – Program Control Structure
Flowcharts and Pseudo Code
Life is Full of Alternatives
REPETITION Why Repetition?
Chapter 5 Desk Checking/Dry Running.
Introduction to Pseudocode
Presentation transcript:

1 TEL 104 / MKK Fundamental Programming: Lecture 4

Calculate_mowing_time 1 Prompt operator for block_lenght, block_width 2 Get block_length, block_width 3 block_area = block_lenght*block_width 4 Prompt operator for house_lenght, house_width 5 Get house_lenght, house_width 6 house_area=house_lenght*house_width 7 Mowing_area=block_area-house_area 8 Mowing_time=mowing_area/2 9 Output mowing_time to screen END Assignment 2 Review:

Desk Checking 1.Input data: Data Set 1Data Set 2 Block_lenght3040 Block_widht3020 House_lenght20 House_width2010

2. Expected result: Data Set 1Data Set 2 Mowing_time250 minutes300 minutes

3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number Block_lenghtBlock_widthHouse_lenghtHouse_widthBlock_are a House_areaMowing_are a Mowing_time First Pass 1, , Output Second Pass 1, , Output

4. Check the expected results match the actual results.

Assignment 3 Review: Calculate_mowing_time 1 Prompt operator for block_lenght, block_width 2 Get block_length, block_width 3 block_area = block_lenght*block_width 4 Prompt operator for house_lenght, house_width 5 Get house_lenght, house_width 6 house_area=house_lenght*house_width 7 Mowing_area=block_area-house_area 8 Mowing_time=mowing_area/2 9 Output mowing_time to screen END

Desk Checking 1.Input data: Data Set 1Data Set 2 Block_lenght3040 Block_widht3020 House_lenght20 House_width2010

2. Expected result: Data Set 1Data Set 2 Mowing_time250 minutes300 minutes

3. Desk Check Table Statement number Block_lenghtBlock_widthHouse_leng ht House_widt h Block_areaHouse_areaMowing_are a Mowing_tim e First Pass 1, , Output Second Pass 1, , Output

4. Check the expected results match the actual results. We found that: –The calculation for the house_area is incorrect, the result is zero, which cannot be right. –The algorithm needs to be adjusted: house_area=block_lenght * block_width –Is changed to: house_area=house_lenght * house_width

Selection Control Structures

Relational Expression <less than > greater than = equal to <= less than or equal to >=greater than or equal to <>not equal to

1.Simple Selection (Simple IF statement ) Keywords: IF, THEN, ELSE, ENDIF Example: IF account_balance < $300 THEN service_charge = $5.00 ELSE service_charge = $2.00 ENDIF

2. Simple Selection with null false branch (null ELSE statement) Example: IF student_attendance = part_time THEN add 1 to part_time_count ENDIF

3. Combined Selection (Combined IF statement) Operator used: AND and OR AND  both condition must be true Example: IF student_attendance = part_time AND student_gender = female THEN add 1 to female_part_time_count ENDIF

Combined Selection (Cont) OR  only one can be true Example: IF student_attendance = part_time OR student_gender = female THEN add 1 to female_part_time_count ENDIF

Combined Selection (Combined IF statement) Both operators used If both operators are used, parentheses must be used to avoid ambiguity. Example: IF record_code = ´23´ OR update_code = delete AND account_balance = zero THEN delete customer record ENDIF Combined Selection (Cont)

Avoid the ambiguity by: IF (record_code = ´23´ OR update_code = delete) AND account_balance = zero THEN delete customer record ENDIF Combined Selection (Cont)

NOT Operator Example: IF NOT (record_code = ´23´) THEN update customer record ENDIF Example with AND and OR operator: IF NOT (record_code = ´23´ AND update_code = delete) THEN update customer record ENDIF

4. Nested Selection (nested IF statement) A.Linear Nested IF statements If record_code = `A´ THEN increment counter_A ELSE IF record_code = ´B´ THEN increment counter_B ELSE IF record_code = ´C´ THEN increment counter_C ELSE increment error_counter ENDIF

B. Non-linear nested IF statements IF student_attendance = part_time THEN IF student_gender = female THEN IF student_age > 21 THEN add 1 to mature_female_pt_students ELSE add 1 to young_female_pt_students ENDIF ELSE add 1 to male_pt_students ENDIF ELSE add 1 to full_time_students ENDIF

IF possible, replace a series of non-linear nested IF statements with a combined IF statements. IF student_attendance = part_time THEN IF student_age > 21 THEN increment mature_pt_students ENDIF IF student_attendance = part_time AND student_age > 21 THEN increment mature_pt_student ENDIF

Algorithms Using Selection Example 4.1 Read three characters Design an algorithm that will prompt a terminal operator for three characters, accept those characters as input, sort them into ascending sequence and output them to the screen.

Defining diagram InputProcessingOutput Char_1 Char_2 Char_3 Prompt for characters Accept three characters Sort three characters Output three characters Char_1 Char_2 Char_3

Solution Algorithm Read_three_characters 1 Prompt the operator for char_1, char_2, char_3 2 Get char_1, char_2, char_3 3 IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF 4 IF char_2 > char_3 THEN temp = char_2 char_2 = char_3 char_3 = temp ENDIF 5 IF char_1 > char_2 THEN temp = char_1 char_1 = char_2 char_2 = temp ENDIF 6 Output to the screen char_1, char_2, char_3 END

Desk Checking Data Set 1Data Set 2 Char_1KZ Char_2BS Char_3GA 1. Input Data

Data Set 1Data Set 2 Char_1BA Char_2GS Char_3KZ 2. Expected Result

3. Desk Check Table Statement number Char_1Char_2Char_3temp First Pass 1,2kbg 3bkk 4gkk 5 6output Output Second Pass 1,2zsa 3szz 4azz 5ass 6output

Example 4.2 Process Customer Record A program is required to read a customer‘s name, a purchase amount and a tax code. The tax code has been validated and will be one of the following: 0baby needs tax (0%) 1Vegetables and food tax (3%) 2drink sales tax (5%) 3special sales tax (7%) The program must then compute the sales tax and the total amount due, and print the customer‘s name, purchase amount, sales tax and total amount due.

Defining diagram InputProcessingOutput Cust_name Purch_amt Tax_code Read customer details Compute sales tax Compute total amount Print customer details Cust_name Purch_amt Sales_tax total_amt

Solution Algorithm Process_customer_record 1Read cust_name, purch_amt,tax_code 2IF tax_code = 0 THEN sales_tax = 0 ELSE IF tax_code = 1 THEN sales_tax = purch_amt * 0.03 ELSE IF tax_code = 2 THEN sales_tax = purch_amt * 0.05 ELSE sales_tax = purch_amt * 0.07 ENDIF 3total_amt = purch_amt + sales_tax 4Print cust_name, purch_amt, sales_tax, total_amt END

Desk Checking Data Set 1Data Set 2 Purch_amt$ 10.00$ Tax_code02 1. Input Data

2. Expected Results Data Set 1Data Set 2 Sales_tax0$1.00 Total_amt$10.00$21.00

3. Desk Check Table Statement number Purch_amtTax_codeSales_taxTotal_amt First Pass 1$ print Second Pass 1$ $ $ print

Example 4.3 Calculate Employee‘s pay A program is required by a company to read an employee‘s number, pay rate and the number of hours worked in a week. The program is then to validate the pay rate and the hours worked fields and, if valid, compute the employee‘s weekly pay and print it along with the input data. Validation: according to the company‘s rules, the maximum hours an employee can work per week is 60 hours, and the maximum hourly rate is $25.00 per hour. If the hours worked field or the hourly rate field is out of range, the input data and appropriate message is to be printed and the employee‘s weekly pay is not to be calculated.

Weekly pay calculation: Weekly pay is calculated as hours worked times pay rate. If more than 35 hours are worked, payment for the overtime hours worked is calculated at time-and-a-half. Example 4.3 (cont)

Defining diagram InputProcessingOutput Emp_no Pay_rate Hrs_worked Read employee details Validate input fields Compute employee pay Print employee details Emp_no Pay_rate Hrs_worked Emp_weekly_pay Error_message

Boolean variables A variable called as Boolean variable if it contains only one of 2 possible values (true or false) IF valid_input_fields = true THEN Statement ENDIF Can be simplified to imply `= true`: (but must be initially be set to true) IF valid_input_fields THEN Statement ENDIF Can also be simplified to imply `= false`: IF NOT valid_input_fields THEN Statement ENDIF

Solution Algorithm Compute_employee_pay 1Set valid_input_fields to true 2Set error_message to blank 3Read emp_no, pay_rate, hrs_worked 4IF pay_rate > $ 25 THEN error_message = ´Pay rate exceeds $25.00` Print emp_no, pay_rate, hrs_worked, error_message valid_input_fields=false ENDIF 5IF hrs_worked > 60 THEN error_message = `Hours worked exceeds 60` Print emp_no, pay_rate, hrs_worked, error_message valid_input_fields=false ENDIF 6IF valid_input_fields THEN IF hrs_worked <= 35 THEN emp_weekly_pay = pay_rate * hrs_worked ELSE overtime_hrs = hrs_worked – 35 overtime_pay = overtime_hrs * pay_rate * 1.5 emp_weekly_pay = (pay_rate * 35) + overtime_pay ENDIF Print emp_no, pay_rate, hrs_worked, emp_weekly_pay ENDIF END

Desk Checking Data Set 1Data Set 2 Pay_rate$ 10.00$ Hrs_worked Input Data

2. Expected Results Data Set 1Data Set 2 Pay_rate$ 10.00$40.00 Hrs_worked4035 Emp_weekly_p ay $ Error_messageblankPay rate exceeds $25.00

3. Desk Check Table Statement number Pay_rateHrs_worke d Over_time _hrs Over_time _pay Emp_wee kly_pay Valid_input _fields Error_mes sage Print First Pass 1true 2blank 3$ Print fields Second Pass 1true 2blank 3$ falsePay rate exceeds $ Print message 5 6

The Case Structure Keywords: CASE OF and ENDCASE CASE OF single variable value_1 : statement block_1 value_2 : statement block_2. value_n : statement block_n ENDCASE

Example 4.4 Process Customer Record A program is required to read a customer‘s name, a purchase amount and a tax code. The tax code has been validated and will be one of the following: 0baby needs tax (0%) 1Vegetables and food tax (3%) 2drink sales tax (5%) 3special sales tax (7%) The program must then compute the sales tax and the total amount due, and print the customer‘s name, purchase amount, sales tax and total amount due.

Defining diagram InputProcessingOutput Cust_name Purch_amt Tax_code Read customer details Compute sales tax Compute total amount Print customer details Cust_name Purch_amt Sales_tax total_amt

Solution Algorithm Process_customer_record 1 Read cust_name, purch_amt,tax_code 2 CASE OF tax_code 0 : sales_tax = 0 1 : sales_tax = purch_amt * : sales_tax = purch_amt * : sales_tax = purch_amt * 0.07 ENDCASE 3 total_amt = purch_amt + sales_tax 4 Print cust_name, purch_amt, sales_tax,total_amt END

Desk Checking Data Set 1Data Set 2 Purch_amt$ 10.00$ Tax_code02 1. Input Data

2. Expected Results Data Set 1Data Set 2 Sales_tax0$1.00 Total_amt$10.00$21.00

3. Desk Check Table Statement number Purch_amtTax_codeSales_taxTotal_amt First Pass 1$ print Second Pass 1$ $ $ print