1 / 56

Logic and Algorithm

Learn how to develop an algorithm by dividing the problem into separate components: input, output, and processing. Includes examples and desk checking.

ellisk
Download Presentation

Logic and Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Logic and Algorithm

  2. Developing an algorithm • To help the initial analysis, the problem should be divided into 3 separate components: • Input: a list of the source data provided to the problem • Output: a list of the outputs required • Processing: a list of actions needed to produce the required outputs.

  3. Example 1. Add three numbers • A program is required to read three numbers, add them together and print their total.

  4. Solution: • 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.

  5. Defining diagram

  6. 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.

  7. Defining diagram

  8. Writing down the processing steps in an algorithm, Read three numbers Add numbers together Print total number

  9. Solution Algorithm • Add_three_numbers Read number1, number2, number3 Total = number1 + number2 + number3 Print total END

  10. 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.

  11. 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.

  12. Defining diagram

  13. 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.

  14. Defining diagram

  15. 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

  16. 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

  17. 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.

  18. Defining diagram

  19. 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.

  20. Defining diagram

  21. 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

  22. Desk Checking

  23. Checking the solution algorithm(Desk Checking) • Tracing through the logic of the algorithm with some chosen data..

  24. Step in desk Checking an algorithm • Choose valid simple input test case (2-3 enough) • Establish what the expected result should be. • Make a table of relevant variable names • Checking the test case line by line, step by step • Repeat process 4 for other test case • Check if expected result 2 matches with actual result 5

  25. Example 4. Desk Chek for example 1 • A program is required to read three numbers, add them together and print their total.

  26. Solution Algorithm • Add_three_numbers Read number1, number2, number3 Total = number1 + number2 + number3 Print total END

  27. Desk Checking • Choose two sets input test data. Set 1: 10,20, 30 and Set 2: 40, 41, 42

  28. 2. Establish the expected result for each test case

  29. 3. Set up a table of relevant variable names, and pass each test data set statement by statement.

  30. 4. Check the expected results (60 and 123) match the actual results.

  31. 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.

  32. 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

  33. Desk Checking • Choose two sets input test data. Set 1: 30, 10 and Set 2: 40, 20

  34. 2. Establish the expected result for each test case

  35. 3. Set up a table of relevant variable names, and pass each test data set statement by statement.

  36. 4. Check the expected results match the actual results.

  37. Assignment 2:Desk Checking forCompute 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.

  38. 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

  39. 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

  40. Assignment 2 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

  41. Desk Checking • Input data:

  42. 2. Expected result:

  43. 3. Set up a table of relevant variable names, and pass each test data set statement by statement.

  44. 4. Check the expected results match the actual results.

  45. What about This? 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

  46. Desk Checking • Input data:

  47. 2. Expected result:

  48. 3. Desk Check Table

  49. 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

  50. 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%) • Vegetables and food tax (3%) • drink sales tax (5%) • 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.

More Related