38d76963a79025ca7384925e9e6b2f55.ppt
- Количество слайдов: 56
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 the source data provided to the problem 2. Output: a list of the outputs required 3. Processing: a list of actions needed to produce the required outputs.
Example 1. Add three numbers § A program is required to read three numbers, add them together and print their total.
Solution: 1. Underline the nouns and adjectives used in the specification establish the input, output component and any object that are required. A program is required to read three numbers, add them together and print their total.
• Defining diagram Input Number 1 Number 2 Number 3 Processing Output total
2. Underline the verbs and adverbs used in the specification establish the action required. A program is required to read three numbers, add them together and print their total.
• Defining diagram Input Processing Number 1 Read three numbers Number 2 Add numbers together Number 3 Print total number Output total
3. Writing down the processing steps in an algorithm, Read three numbers Add numbers together Print total number
Solution Algorithm • Add_three_numbers Read number 1, number 2, number 3 Total = number 1 + number 2 + number 3 Print total END
Example 2. Find average temperature • A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.
Step 1 • A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.
• Defining diagram Input Max_temp Min_temp Processing Output Avg_temp
Step 2 • A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.
• Defining diagram Input Max_temp Min_temp Processing Output Prompt for temperatures Avg_temp Get temperatures Calculate average temperature Display average temperature
Solution Algorithm • Find average_temperature Prompt operator for max_temp, min_temp Get max_temp, min_temp Avg_temp= (max_Temp + min_temp)/2 Output avg_temp to the screen END
Example 3. Compute Mowing Time • A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute
Step 1 • A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.
• Defining diagram Input Block_lenght Block_width House_lenght House_width Processing Output Mowing_time
Step 2 • A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.
• Defining diagram Input Block_lenght Block_width House_lenght House_width Processing Prompt for block measurements Get block measurements Prompt for house measurements Get house measurements Calculate mowing area Calculate mowing time Output Mowing_time
Solution Algorithm Calculate_mowing_time Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght*block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=house_lenght*house_width Mowing_area=block_area-house_area Mowing_time=mowing_area/2 Output mowing_time to screen END
Desk Checking
Checking the solution algorithm (Desk Checking) • Tracing through the logic of the algorithm with some chosen data. .
Step in desk Checking an algorithm 1. Choose valid simple input test case (2 -3 enough) 2. Establish what the expected result should be. 3. Make a table of relevant variable names 4. Checking the test case line by line, step by step 5. Repeat process 4 for other test case 6. Check if expected result 2 matches with actual result 5
Example 4. Desk Chek for example 1 § A program is required to read three numbers, add them together and print their total.
Solution Algorithm • Add_three_numbers Read number 1, number 2, number 3 Total = number 1 + number 2 + number 3 Print total END
Desk Checking 1. Choose two sets input test data. Set 1: 10, 20, 30 and Set 2: 40, 41, 42 Data Set 1 Data Set 2 Number 1 10 40 Number 2 20 41 Number 3 30 42
2. Establish the expected result for each test case Data Set 1 Total Data Set 2 60 123
3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number 1 number 2 number 3 10 20 total 30 First Pass 1 2 60 3 Print Second Pass 1 40 41 42 2 123 3 Print
4. Check the expected results (60 and 123) match the actual results.
Desk Check of Example 2. • A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.
Solution Algorithm • Find average_temperature Prompt operator for max_temp, min_temp Get max_temp, min_temp Avg_temp= (max_Temp + min_temp)/2 Output avg_temp to the screen END
Desk Checking 1. Choose two sets input test data. Set 1: 30, 10 and Set 2: 40, 20 Data Set 1 Data Set 2 Max_temp 30 40 Min_temp 10 20
2. Establish the expected result for each test case Data Set 1 Data Set 2 Avg_temp 20 30
3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number Max_temp Min_temp 30 Avg_temp 10 First Pass 1, 2 3 20 4 0 utput Second Pass 1, 2 40 20 3 30 4 output
4. Check the expected results match the actual results.
Assignment 2: Desk Checking for Compute mowing time • A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.
Solution Algorithm Calculate_mowing_time Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght*block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=house_lenght*house_width Mowing_area=block_area-house_area Mowing_time=mowing_area/2 Output mowing_time to screen END
Assignment 3 – Desk Checking for Mowing_time which now contains a logic error Calculate_mowing_time Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght * block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=block_lenght * block_width Mowing_area=block_area - house_area Mowing_time=mowing_area/2 Output mowing_time to screen END
Assignment 2 Review: 1 2 3 4 5 6 7 8 9 Calculate_mowing_time Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght*block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=house_lenght*house_width Mowing_area=block_area-house_area Mowing_time=mowing_area/2 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_ti me 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 30 House_lenght House_width Block_are a House_area Mowing_are a Mowing_tim e 30 First Pass 1, 2 3 900 4, 5 20 20 6 400 7 500 8 250 9 Output Second Pass 1, 2 40 20 3 4, 5 6 7 800 20 10 200 600 8 300 9 Output
4. Check the expected results match the actual results.
What about This? 1 2 3 4 5 6 7 8 9 Calculate_mowing_time Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght*block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=house_lenght*house_width Mowing_area=block_area-house_area Mowing_time=mowing_area/2 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_ti me 250 minutes 300 minutes
3. Desk Check Table Statement number Block_lenght Block_width 30 House_leng ht House_width Block_area House_area Mowing_are a Mowing_tim e 30 First Pass 1, 2 3 900 4, 5 20 20 6 900 7 0 8 0 9 Output Second Pass 1, 2 40 20 3 4, 5 6 7 800 20 10 800 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
Example 5. 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 Cust_name Read customer details Purch_amt Compute sales tax Tax_code Compute total amount Print customer details Output Cust_name Purch_amt Sales_tax total_amt
Solution Algorithm 1 2 3 4 Process_customer_record Read cust_name, purch_amt, tax_code IF 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 total_amt = purch_amt + sales_tax Print cust_name, purch_amt, sales_tax, total_amt END
Desk Checking 1. Input Data Set 1 Data Set 2 Purch_amt $ 10. 00 $ 20. 00 Tax_code 0 2
2. Expected Results Data Set 1 Data Set 2 Sales_tax 0 $1. 00 Total_amt $10. 00 $21. 00
3. Desk Check Table Statement number Purch_amt Tax_code $ 10. 00 Sales_tax Total_amt 0 First Pass 1 2 0 3 4 $ 10. 00 print Second Pass 1 $ 20. 00 2 2 $ 1. 00 3 4 $ 21. 00 print


