1
TEL 104 / MKK Fundamental Programming:
Lecture 4
Calculate_mowing_time1 Prompt operator for block_lenght, block_width2 Get block_length, block_width3 block_area = block_lenght*block_width4 Prompt operator for house_lenght, house_width5 Get house_lenght, house_width6 house_area=house_lenght*house_width7 Mowing_area=block_area-house_area8 Mowing_time=mowing_area/29 Output mowing_time to screen
END
Assignment 2 Review:
Desk Checking
1. Input data:
Data Set 1 Data Set 2
Block_lenght 30 40
Block_widht 30 20
House_lenght 20 20
House_width 20 10
2. Expected result:
Data Set 1 Data Set 2
Mowing_time 250 minutes 300 minutes
3. Set up a table of relevant variable names, and pass each test data set statement by statement.
Statement number
Block_lenght Block_width House_lenght House_width Block_area
House_area Mowing_area
Mowing_time
First Pass
1,2 30 30
3 900
4,5 20 20
6 400
7 500
8 250
9 Output
Second Pass
1,2 40 20
3 800
4,5 20 10
6 200
7 600
8 300
9 Output
4. Check the expected results match the actual results.
Assignment 3 Review:
Calculate_mowing_time1 Prompt operator for block_lenght, block_width2 Get block_length, block_width3 block_area = block_lenght*block_width4 Prompt operator for house_lenght, house_width5 Get house_lenght, house_width6 house_area=house_lenght*house_width7 Mowing_area=block_area-house_area8 Mowing_time=mowing_area/29 Output mowing_time to screen
END
Desk Checking
1. Input data:
Data Set 1 Data Set 2
Block_lenght 30 40
Block_widht 30 20
House_lenght 20 20
House_width 20 10
2. Expected result:
Data Set 1 Data Set 2
Mowing_time 250 minutes 300 minutes
3. Desk Check Table
Statement number
Block_lenght Block_width House_lenght
House_width
Block_area House_area Mowing_area
Mowing_time
First Pass
1,2 30 30
3 900
4,5 20 20
6 900
7 0
8 0
9 Output
Second Pass
1,2 40 20
3 800
4,5 20 10
6 800
7 0
8 0
9 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 = deleteAND account_balance = zero THEN
delete customer recordENDIF
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_AELSE IF record_code = ´B´ THEN increment counter_B ELSE
IF record_code = ´C´ THEN increment counter_C
ELSE increment error_counterENDIF
ENDIFENDIF
B. Non-linear nested IF statements
IF student_attendance = part_time THENIF student_gender = female THEN
IF student_age > 21 THENadd 1 to mature_female_pt_students
ELSEadd 1 to young_female_pt_students
ENDIFELSE
add 1 to male_pt_studentsENDIF
ELSEadd 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 THENIF student_age > 21 THEN
increment mature_pt_studentsENDIF
ENDIF
IF student_attendance = part_timeAND student_age > 21 THENincrement 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
Input Processing Output
Char_1Char_2Char_3
Prompt for charactersAccept three charactersSort three charactersOutput three characters
Char_1Char_2Char_3
Solution AlgorithmRead_three_characters
1 Prompt the operator for char_1, char_2, char_32 Get char_1, char_2, char_33 IF char_1 > char_2 THEN
temp = char_1char_1 = char_2char_2 = temp
ENDIF4 IF char_2 > char_3 THEN
temp = char_2char_2 = char_3char_3 = temp
ENDIF5 IF char_1 > char_2 THEN
temp = char_1char_1 = char_2char_2 = temp
ENDIF6 Output to the screen char_1, char_2, char_3
END
Desk Checking
Data Set 1 Data Set 2
Char_1 K Z
Char_2 B S
Char_3 G A
1. Input Data
Data Set 1 Data Set 2
Char_1 B A
Char_2 G S
Char_3 K Z
2. Expected Result
3. Desk Check Table
Statement number
Char_1 Char_2 Char_3 temp
First Pass
1,2 k b g
3 b k k
4 g k k
5
6 output output Output
Second Pass
1,2 z s a
3 s z z
4 a z z
5 a s s
6 output output output
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:
0 baby needs tax (0%)1 Vegetables and food tax (3%)2 drink sales tax (5%)3 special 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
Input Processing Output
Cust_namePurch_amtTax_code
Read customer detailsCompute sales taxCompute total amountPrint customer details
Cust_namePurch_amtSales_taxtotal_amt
Solution AlgorithmProcess_customer_record
1 Read cust_name, purch_amt,tax_code2 IF tax_code = 0 THEN
sales_tax = 0ELSE
IF tax_code = 1 THENsales_tax = purch_amt * 0.03
ELSEIF tax_code = 2 THEN
sales_tax = purch_amt * 0.05ELSE
sales_tax = purch_amt * 0.07ENDIF
ENDIFENDIF
3 total_amt = purch_amt + sales_tax4 Print cust_name, purch_amt, sales_tax, total_amt
END
Desk Checking
Data Set 1 Data Set 2
Purch_amt $ 10.00 $ 20.00
Tax_code 0 2
1. Input Data
2. Expected Results
Data Set 1 Data Set 2
Sales_tax 0 $1.00
Total_amt $10.00 $21.00
3. Desk Check TableStatement
numberPurch_amt Tax_code Sales_tax Total_amt
First Pass
1 $ 10.00 0
2 0
3 $ 10.00
4 print print print
Second Pass
1 $ 20.00 2
2 $ 1.00
3 $ 21.00
4 print print 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
Input Processing Output
Emp_noPay_rateHrs_worked
Read employee detailsValidate input fieldsCompute employee payPrint employee details
Emp_noPay_rateHrs_workedEmp_weekly_payError_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
StatementENDIF
• Can be simplified to imply `= true`: (but must be initially be set to true)IF valid_input_fields THEN
StatementENDIF
• Can also be simplified to imply `= false`:
IF NOT valid_input_fields THENStatement
ENDIF
Solution Algorithm Compute_employee_pay1 Set valid_input_fields to true2 Set error_message to blank3 Read emp_no, pay_rate, hrs_worked4 IF pay_rate > $ 25 THEN
error_message = ´Pay rate exceeds $25.00` Print emp_no, pay_rate, hrs_worked, error_messagevalid_input_fields=false
ENDIF5 IF hrs_worked > 60 THEN
error_message = `Hours worked exceeds 60`Print emp_no, pay_rate, hrs_worked, error_messagevalid_input_fields=false
ENDIF6 IF valid_input_fields THEN
IF hrs_worked <= 35 THENemp_weekly_pay = pay_rate * hrs_worked
ELSEovertime_hrs = hrs_worked – 35overtime_pay = overtime_hrs * pay_rate * 1.5emp_weekly_pay = (pay_rate * 35) + overtime_pay
ENDIFPrint emp_no, pay_rate, hrs_worked, emp_weekly_pay
ENDIFEND
Desk Checking
Data Set 1 Data Set 2
Pay_rate $ 10.00 $ 40.00
Hrs_worked 40 35
1. Input Data
2. Expected Results
Data Set 1 Data Set 2
Pay_rate $ 10.00 $40.00
Hrs_worked 40 35
Emp_weekly_pay
$425.00 -
Error_message blank Pay rate exceeds $25.00
3. Desk Check TableStatement number
Pay_rate Hrs_worked
Over_time_hrs
Over_time_pay
Emp_weekly_pay
Valid_input_fields
Error_message
First Pass
1 true
2 blank
3 $10.00 40
4
5
6 5 75.00 425.00 Print fields
Second Pass
1 true
2 blank
3 $40.00 35
4 false Pay rate exceeds $
25.00
Print message
5
6
The Case Structure
• Keywords: CASE OF and ENDCASE
CASE OF single variablevalue_1 : statement block_1value_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:
0 baby needs tax (0%)1 Vegetables and food tax (3%)2 drink sales tax (5%)3 special 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
Input Processing Output
Cust_namePurch_amtTax_code
Read customer detailsCompute sales taxCompute total amountPrint customer details
Cust_namePurch_amtSales_taxtotal_amt
Solution Algorithm
Process_customer_record1 Read cust_name, purch_amt,tax_code2 CASE OF tax_code
0 : sales_tax = 01 : sales_tax = purch_amt * 0.032 : sales_tax = purch_amt * 0.053 : sales_tax = purch_amt * 0.07
ENDCASE3 total_amt = purch_amt + sales_tax4 Print cust_name, purch_amt, sales_tax,total_amt
END
Desk Checking
Data Set 1 Data Set 2
Purch_amt $ 10.00 $ 20.00
Tax_code 0 2
1. Input Data
2. Expected Results
Data Set 1 Data Set 2
Sales_tax 0 $1.00
Total_amt $10.00 $21.00
3. Desk Check TableStatement number
Purch_amt Tax_code Sales_tax Total_amt
First Pass
1 $ 10.00 0
2 0
3 $ 10.00
4 print print print
Second Pass
1 $ 20.00 2
2 $ 1.00
3 $ 21.00
4 print print print