foundation of programming

31
Foundation of programming Week 3

Upload: tadeo

Post on 23-Mar-2016

42 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Foundation of programming

Foundation of programming

Week 3

Page 2: Foundation of programming

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

Page 3: Foundation of programming

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

Page 4: Foundation of programming

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

Page 5: Foundation of programming

Top down design

Page 6: Foundation of programming

Bottom up design

Page 7: Foundation of programming

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

Page 8: Foundation of programming

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

Page 9: Foundation of programming

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

Page 10: Foundation of programming

The sequential logic structure

1

2

Page 11: Foundation of programming

Decision logic structure

Yes No

1 2

IF(cond=TRUE) THEN instruction1 ELSE instruction2 ENDIF

Page 12: Foundation of programming

Loop logic structure

cond

body

program

yes

No

1

2

WHILE(cond=TRUE) DO {instruction1 ; instruction2} ENDWHILE

Page 13: Foundation of programming

The case logic structure

Page 14: Foundation of programming

switch x{

x== condition1 instruction1x== condition 2 instruction 2x== condition 3 instruction 3x== condition 4 instruction 4

}

Page 15: Foundation of programming

Discussionwhich logic will you use?

• Which logic did you use in your solutions to the following problems?1. Use a filter coffee machine to make a cup of

coffee2. Hang a picture on the wall3. Drive a train

Page 16: Foundation of programming

Making a cup of coffee

1. Put water in coffee machine;2. Open coffee holder;3. Put filter paper in machine;4. Measure coffee for one cup;5. Put coffee into filter paper;6. Shut the coffee holder;7. Turn machine on;8. Wait for coffee to filter through;9. Pour coffee into mug;10. Turn off the machine;

Page 17: Foundation of programming

Analyse our solution

• Is sugar needed?• If (yes) how much?• White coffee? If yes add milk?

Page 18: Foundation of programming

Make a cup of coffeemodified

1. Put water in coffee machine;2. Open coffee holder;3. Put filter paper in machine;4. Measure coffee for one cup;5. Put coffee into filter paper;6. Shut the coffee holder;7. Turn the machine on;8. Wait for coffee to filter through;9. Find out how many sugars required;

10. WHILE(sugar added not equal to sugar required)

11. DO11.1 add one spoon of sugar11.2 add 1 spoon

12. ENDWHILE13. IF (white coffee required)

13.1 add milk/cream

14. ENDIF15. Pour coffee into mug16. Stir coffee17. Turn machine off

Page 19: Foundation of programming

Making 6 cups of coffee1. Put water in coffee machine; change to water for 6 cups2. Open coffee holder;3. Put filter paper in machine;4. Measure coffee for one cup; coffee for 6 cups5. Put coffee into filter paper;6. Shut the coffee holder;7. Turn the machine on;8. Wait for coffee to filter through;9. Find out how many sugars required;10. Find out weather milk required11. WHILE(sugar added not equal to sugar required)12. DO

12.1 add one spoon of sugar12.2 add 1 spoon

13. ENDWHILE14. IF (white coffee required)

14.1 add milk/cream

15. ENDIF16. Pour coffee into mug17. Stir coffee

18. Turn machine off

Repeated actions

Page 20: Foundation of programming

Making a 6 cups of coffee1. Put waterF for 6 cups in coffee

machine2. Open coffee holder;3. Put filter paper in machine;4. Measure coffee for 6 cup; 5. Put coffee into filter paper;6. Shut the coffee holder;7. Turn the machine on;8. Wait for coffee to filter through;

10. While(cups poured not equal to 6)11. Do

1. Find out how many sugars required;2. Find out weather milk required3. WHILE(sugar added not equal to sugar required)4. DO

12.1 add one spoon of sugar12.2 add 1 spoon

5. ENDWHILE6. IF (white coffee required)

14.1 add milk/cream

7. ENDIF8. Pour coffee into mug9. Stir coffee

10.Add 1 to the number of cups poured

12. ENDWHILE

13. Turn machine off

Page 21: Foundation of programming

Making a pot of coffee

1. Find out how many cups required2. Put waterF for number cups required

n coffee machine3. Open coffee holder;4. Put filter paper in machine;5. Measure coffee for cups required

6. Put coffee into filter paper;7. Shut the coffee holder;8. Turn the machine on;9. Wait for coffee to filter through;

10. While( cups poured not equal to cups required )11. Do

1. Find out how many sugars required;2. Find out weather milk required3. WHILE(sugar added not equal to sugar required)4. DO

12.1 add one spoon of sugar12.2 add 1 spoon

5. ENDWHILE6. IF (white coffee required)

14.1 add milk/cream

7. ENDIF8. Pour coffee into mug9. Stir coffee

10.Add 1 to the number of cups12. ENDWHILE

13. Turn machine off

Page 22: Foundation of programming

Limit the cups required to six

1. Find out how many cups required2. IF(more than zero cups required)

1. IF (more than six cup wanted)1. Limit cupsrequired to six;

2. ENDIF

3. Put waterF for number cups required n coffee machine

4. Open coffee holder;5. Put filter paper in machine;6. Measure coffee for cups required 7. Put coffee into filter paper;8. Shut the coffee holder;9. Turn the machine on;10. Wait for coffee to filter through;11. While( cups poured not equal to cups

required )

11. While( cups poured not equal to cups required )

12. Do 1. Find out how many sugars required;2. Find out weather milk required3. WHILE(sugar added not equal to sugar required)4. DO

12.1 add one spoon of sugar12.2 add 1 spoon

5. ENDWHILE6. IF (white coffee required)

14.1 add milk/cream

7. ENDIF8. Pour coffee into mug9. Stir coffee

10.Add 1 to the number of cups

13. ENDWHILE

14.Turn machine off

15.ENDIF

Page 23: Foundation of programming

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’

Page 24: Foundation of programming

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

Page 25: Foundation of programming

Exercise2

1) Write a pseudo code program which allow the user to enter the monthly rain full and add it the total rainfall for the year.

2) Change your program to work out the average rainfall for the previous year

3) Add the average rainfall sofar (the year has not finished yet)

Page 26: Foundation of programming

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;

Page 27: Foundation of programming

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

Page 28: Foundation of programming

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)

Page 29: Foundation of programming

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.

Page 30: Foundation of programming

Solutionis this correct?

1. Display ‘please enter an age ( 0 to finish);2. Get value of age;3. WHILE(age notequal 0)

1. totalAge < -- totalAge + age;2. numberOfAges < -- numberOfAges + 1;3. Get value of age;

4. ENDWHILE5. If(numberOfAges >0)

1. averageAge < -- totalAge / numberOfAges;2. Display averageAge;

6. ENDIF

Page 31: Foundation of programming

Solutionadd initialisation

1. totalAge < -- 0; // intialise totalAge to zero2. numberOfAges < -- 0; // initialise numberOfAges to zero

3. Display ‘please enter an age ( 0 to finish);4. Get value of age;5. WHILE(age notequal 0)

1. totalAge < -- totalAge + age;2. numberOfAges < -- numberOfAges + 1;3. Get value of age;

6. ENDWHILE7. If(numberOfAges >0)

1. averageAge < -- totalAge / numberOfAges;2. Display averageAge;

8. ENDIF