CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

25
CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    217
  • download

    3

Transcript of CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Page 1: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

CMT1000: Introduction to Programming

Ed Currie

Lecture 2A: Pizza

Page 2: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Pizza making algorithm

• Often when devising an algorithm, we will first express it using instructions at a high level of abstraction, that is, in quite general terms, without great detail.

• How can we do this for a pizza recipe?

Page 3: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

First stab

–1. Make the base

–2. Make the topping

–3. Put the topping on the base

–4. Cook it

Four smaller problems to solve;

Not sufficiently detailed yet to enable us to make the pizza

Page 4: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Stepwise refinement needed

• => an algorithm comprising instructions sufficiently detailed for our processor to follow.

• The detailed algorithm is said to be more concrete, or at a lower level of abstraction, than the original attempt.

Page 5: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Refinement

• 1. Make the base => Make the base according to the recipe on page 15

• 2. Make the topping ?????????

• 3. Put the topping on the base ????????

• 4. Cook it => Bake for 20 minutes directly on the oven shelf.

Page 6: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

It’s like a computer program:• Input data

• Instructions

• Output

• An algorithm being executed is generally known as a process, and the thing that is executing it is called a processor

• Another analogy is that of the text of a play being the algorithm, and the performance of the play being the process.

Page 7: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Sub-programs

• Make the base according to the recipe on page 15...

..is asking the cook to execute another algorithm, the one on page 15 for making a pizza base.

• This idea is very important in writing computer programs.

Page 8: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Question

• What are the advantages of subalgorithms?

Page 9: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Answer

• The main algorithm will be more succinct

• The main algorithm will be easier to understand, as the code for the subalgorithm is not cluttering it up.

• The subalgorithm can be used over and over again.

Page 10: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Question

• What would happen if an instruction in an algorithm told the processor to execute the algorithm of which the instruction was a part?

Page 11: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Parameters• “Make the base…” says follow the base-making

instructions on page 15...

• ...but using the quantities of ingredients (input) listed in the current recipe,

• causing a double quantity of the base to be made.

• The same instructions can be followed to make any required quantity of the base

• The quantities of ingredients are parameters to

the sub-algorithm.

Page 12: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Question

• What are the advantages of parameterising an algorithm?

Page 13: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Answer

• The same algorithm instructions may be told to operate on different data, making the algorithm much more general, versatile and useful.

Page 14: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Re-use of algorithms• Sometimes we can re-use an

algorithm which we already have available, as with ‘make the base’.

• This idea of re-use is also extremely important in computer programming.

• Libraries

• Java API

Page 15: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Pseudocode and structured programming

• The English language has enormous expressive power and subtlety..

...but it is often hard to be precise when using English to describe something accurately.

• Computer programming language instructions have precisely one meaning.

• We often use pseudocode to express our algorithms before final translation into programming languages.

Page 16: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Exercise

• Try to find as many meanings as you can for the sentence

“The woman made the robot fast”

Page 17: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Sequence• Consider the instruction “Make the topping”

• We refined this into the instruction sequence:

- Heat the oil in a frying pan

- Add the onion, garlic and chillies and fry for 5 minutes

etc…

• We will now further refine this towards something resembling a computer program, using pseudocode notation.

Page 18: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Repetition

• Heat the oil in a frying pan...

• How can we be more precise about this?

• How long should we heat the oil for? Until it sizzles when we add a piece of onion to it.

• Express using a pseudocode construct called a while loop.

Page 19: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Heat the oil in a frying pan

- Put oil in pan

- Light gas

- Place small piece of onion in pan

- WHILE onion doesn’t sizzle

Wait 30 seconds

–Semantics?

Page 20: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Question

–Which of the following are valid boolean expressions?

– It is raining.

– Is it raining?

– Eat my shorts.

– Doh!

Page 21: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Question

–I am cold and I am wet.

–This statement is false.

–2 = 2

–2 > 8

Page 22: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Selection

–Add the onion

–Add the garlic

–Add the chillies

–Fry for 5 minutes

Page 23: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

However, not everyone has a taste for spicy hot pizza!

- Add the onion

- Add the garlic

- IF eater likes hot pizza

Add the chillies

- Fry for 5 minutes

Page 24: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Some prefer browner onions

IF eater likes very brown onions

Fry for 10 minutes

ELSE

Fry for 5 minutes

Page 25: CMT1000: Introduction to Programming Ed Currie Lecture 2A: Pizza.

Test and evaluate

• Does the pizza taste as we wanted it to?

• Is it quick and easy to make?

• Are the ingredients appropriate? Could one or more have been left out?

• Does it cost too much to make?