1 / 31

Foundation of programming

Foundation of programming. Week 3. Last week. ‘How to think like a programmer’ The HTTLAP 6 step approach: Understand the problem Devise a plan to solve it Carry out the plan Assess the result Reflect on what you have learned Document the solution Descriptive languages

tadeo
Télécharger la présentation

Foundation of programming

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. Foundation of programming Week 3

  2. Last week • ‘How to think like a programmer’ • The HTTLAP 6 step approach: • Understand the problem • Devise a plan to solve it • Carry out the plan • Assess the result • Reflect on what you have learned • Document the solution • Descriptive languages • Pseudo Code

  3. Lecture Outlines • Program design methods • Top down • Bottom up • Data-structure approaches • Data-flow approaches • Logic structures • Sequential • Conditional • IF • Switch • Loops (iterations) • More on pseudo-code

  4. Reading • Chapter 5 and 6 (HTTP) • EXERCISES • Chapter 5 exercises 1 TO 10, pages 127 to 129 • Chapter 6 exercises 1 to 14 , pages 162 to 166

  5. Top down design

  6. Bottom up design

  7. Data flow approaches • Data movement and transformation • Top down flow of data • Activity, in pairs: • Think about the oyster card • What is the data? • Where does it flow from+to? • How is it transformed? • Try to draw a diagram of this

  8. The four logic structures • Sequential – flows straight down • Decision – the flow splits into two • Loop – the flow repeats a section • Case – the flow splits into many streams

  9. Example data frlow x = 10; y =4; sum = x+y; subtract = x-y; product = x*y; X=10 y=4 sum=x+y subtract=x-y product=x*y

  10. The sequential logic structure 1 2

  11. Decision logic structure No Yes 1 2 IF(cond=TRUE) THENinstruction1 ELSEinstruction2 ENDIF

  12. Loop logic structure No cond yes 1 2 body program WHILE(cond=TRUE) DO {instruction1 ;instruction2}ENDWHILE

  13. The case logic structure

  14. switch x { x== condition1 instruction1 x== condition 2 instruction 2 x== condition 3 instruction 3 x== condition 4 instruction 4 }

  15. Discussionwhich logic will you use? • Which logic did you use in your solutions to the following problems? • Use a filter coffee machine to make a cup of coffee • Hang a picture on the wall • Drive a train

  16. Making a cup of coffee • Put water in coffee machine; • Open coffee holder; • Put filter paper in machine; • Measure coffee for one cup; • Put coffee into filter paper; • Shut the coffee holder; • Turn machine on; • Wait for coffee to filter through; • Pour coffee into mug; • Turn off the machine;

  17. Analyse our solution • Is sugar needed? • If (yes) how much? • White coffee? If yes add milk?

  18. Make a cup of coffeemodified • Put water in coffee machine; • Open coffee holder; • Put filter paper in machine; • Measure coffee for one cup; • Put coffee into filter paper; • Shut the coffee holder; • Turn the machine on; • Wait for coffee to filter through; • Find out how many sugars required; • WHILE(sugar added not equal to sugar required) • DO 11.1 add one spoon of sugar 11.2 add 1 spoon • ENDWHILE • IF (white coffee required) 13.1 add milk/cream • ENDIF • Pour coffee into mug • Stir coffee • Turn machine off

  19. Making 6 cups of coffee • Put water in coffee machine; change to water for 6 cups • Open coffee holder; • Put filter paper in machine; • Measure coffee for one cup; coffee for 6 cups • Put coffee into filter paper; • Shut the coffee holder; • Turn the machine on; • Wait for coffee to filter through; • Find out how many sugars required; • Find out weather milk required • WHILE(sugar added not equal to sugar required) • DO 12.1 add one spoon of sugar 12.2 add 1 spoon • ENDWHILE • IF (white coffee required) 14.1 add milk/cream • ENDIF • Pour coffee into mug • Stir coffee • Turn machine off Repeated actions

  20. Making a 6 cups of coffee • Put waterF for 6 cups in coffee machine • Open coffee holder; • Put filter paper in machine; • Measure coffee for 6 cup; • Put coffee into filter paper; • Shut the coffee holder; • Turn the machine on; • Wait for coffee to filter through; • While(cups poured not equal to 6) • Do • Find out how many sugars required; • Find out weather milk required • WHILE(sugar added not equal to sugar required) • DO 12.1 add one spoon of sugar 12.2 add 1 spoon • ENDWHILE • IF (white coffee required) 14.1 add milk/cream • ENDIF • Pour coffee into mug • Stir coffee • Add 1 to the number of cups poured • ENDWHILE • Turn machine off

  21. Making a pot of coffee While( cups poured not equal to cups required ) Do Find out how many sugars required; Find out weather milk required WHILE(sugar added not equal to sugar required) DO 12.1 add one spoon of sugar 12.2 add 1 spoon ENDWHILE IF (white coffee required) 14.1 add milk/cream ENDIF Pour coffee into mug Stir coffee Add 1 to the number of cups ENDWHILE Turn machine off • Find out how many cups required • Put waterF for number cups required n coffee machine • Open coffee holder; • Put filter paper in machine; • Measure coffee for cups required • Put coffee into filter paper; • Shut the coffee holder; • Turn the machine on; • Wait for coffee to filter through;

  22. Limit the cups required to six • Find out how many cups required • IF(more than zero cups required) • IF (more than six cup wanted) • Limit cupsrequired to six; • ENDIF • Put waterF for number cups required n coffee machine • Open coffee holder; • Put filter paper in machine; • Measure coffee for cups required • Put coffee into filter paper; • Shut the coffee holder; • Turn the machine on; • Wait for coffee to filter through; • While( cups poured not equal to cups required ) • While( cups poured not equal to cups required ) • Do • Find out how many sugars required; • Find out weather milk required • WHILE(sugar added not equal to sugar required) • DO 12.1 add one spoon of sugar 12.2 add 1 spoon • ENDWHILE • IF (white coffee required) 14.1 add milk/cream • ENDIF • Pour coffee into mug • Stir coffee • Add 1 to the number of cups • ENDWHILE • Turn machine off • ENDIF

  23. EXERCISE • Write a pseudo code for a program to work out the final grade. • (Mark >= 80) grade = ‘A’ • (70<=Mark<80) grade = ‘B’ • (60<=Mark <70) grade= ‘C’ • (50<=Mark<60) grade ‘D’ • (40<=Mark<50) grade ‘E’ • (Mark<40) grade =‘F’

  24. SolutionNested IFs • IF (Mark>=80) • grade < -- ‘A’; • ELSE IF (Mark>=70) • grade < -- ‘B’; • ELSE IF (Mark>=60) • grade < -- ‘C’; • ELSE IF (Mark>=50) • grade < -- ‘D’; • ELSE IF (Mark>=40) • grade < -- ‘E’; • ELSE • grade < -- ‘F’; ENDIF

  25. Exercise2 • Write a pseudo code program which allow the user to enter the monthly rain full and add it the total rainfall for the year. • Change your program to work out the average rainfall for the previous year • Add the average rainfall sofar(the year has not finished yet)

  26. Solution: • totalRainfall< --- 0; • onth < --- 1; • WHILE(month <= 12) • Display ‘please enter the month’s rain fall’; • Get monthRainfall; • totalRainfall < --- totalRainfall + monthRainfall; • month < -- month +1; • ENDWHILE;

  27. Solution: • totalRainfall< --- 0; • month < --- 1; • WHILE(month <= 12) • Display ‘please enter the month’s rain fall’; • Get monthRainfall • totalRainfall < --- totalRainfall + monthRainfall; • month < -- month +1; • ENDWHILE • AverageRainfall = totalRainfall/12

  28. Solution (3) • totalRainfall< --- 0; • Get currentMonth • month < --- 1; • WHILE(month <= currentmonth) • Display ‘please enter the month’s rain fall’; • Get monthRainfall • totalRainfall < --- totalRainfall + monthRainfall; • month < -- month +1; • ENDWHILE • AverageRainfallsofar = totalRainfallsofar /currenthmonth; (assuming last minute of the month)

  29. Exercise • Write a pseudo-code program that allows the user the to enter the ages all students on the class and work out the average age. The user enters 0 when all ages are entered.

  30. Solutionis this correct? • Display ‘please enter an age ( 0 to finish); • Get value of age; • WHILE(age notequal 0) • totalAge< -- totalAge + age; • numberOfAges < -- numberOfAges + 1; • Get value of age; • ENDWHILE • If(numberOfAges >0) • averageAge < -- totalAge / numberOfAges; • Display averageAge; • ENDIF

  31. Solutionadd initialisation • totalAge< -- 0; // intialisetotalAge to zero • numberOfAges < -- 0; // initialise numberOfAges to zero • Display ‘please enter an age ( 0 to finish); • Get value of age; • WHILE(age notequal 0) • totalAge< -- totalAge + age; • numberOfAges < -- numberOfAges + 1; • Get value of age; • ENDWHILE • If(numberOfAges >0) • averageAge < -- totalAge / numberOfAges; • Display averageAge; • ENDIF

More Related