sequences modules08

19
Sequences, Modules and Variables David Millard ([email protected])

Upload: hoosfoos

Post on 08-May-2015

1.366 views

Category:

Education


0 download

DESCRIPTION

A brief introduction to how algorithms can be broken down into modules (functions or methods)

TRANSCRIPT

Page 1: Sequences Modules08

Sequences, Modules and Variables

David Millard([email protected])

Page 2: Sequences Modules08

Overview

•Pseudocode•Flowcharts•What are Modules?•Variables•Parameters

Page 3: Sequences Modules08

Pseudocode

“Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading”

- Wikipedia

“A notation resembling a simplified programming language, used in program design; esp. one consisting of expressions in natural language syntactically structured like a programming language”

- Oxford English Dictionary

Page 4: Sequences Modules08

Pseudocode

“Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading”

- Wikipedia

“A notation resembling a simplified programming language, used in program design; esp. one consisting of expressions in natural language syntactically structured like a programming language”

- Oxford English Dictionary

Page 5: Sequences Modules08

Pseudocode

“Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading”

- Wikipedia

“A notation resembling a simplified programming language, used in program design; esp. one consisting of expressions in natural language syntactically structured like a programming language”

- Oxford English Dictionary

Page 6: Sequences Modules08

Pseudocode

“Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of some programming language, but is intended for human reading rather than machine reading”

- Wikipedia

“A notation resembling a simplified programming language, used in program design; esp. one consisting of expressions in natural language syntactically structured like a programming language”

- Oxford English Dictionary

Page 7: Sequences Modules08

Example: Making a Cup of Tea

Page 8: Sequences Modules08

Example: Making a Cup of TeaMake a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User

Pseudocode

Assumptions?

Page 9: Sequences Modules08

Example: Making a Cup of TeaMake a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User

Pseudocode

Assumptions:

• Electric Kettle• User wants Milk and Sugar• Only making 1 cup of tea• Nothing goes wrong!

Page 10: Sequences Modules08

Modules

• Modules break an algorithm into logical parts▫Helps with Clarity and Understandability

• Modules can be reused▫Within the same algorithm▫ In a different algorithm

• In Programming Modules can be called:▫Sub-routines (in older languages)▫Functions (in procedural languages like C)▫Methods (in object oriented languages like

Java)

Page 11: Sequences Modules08

Example: Making a Cup of TeaMake a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User

Page 12: Sequences Modules08

Fetch Utensils and Ingredients

Boil Water in Kettle

Make Tea in Pot

Example: Making a Cup of Tea

Add Tea, Milk and Sugar to Cup

Make a Cup of TeaGet CupGet KettleGet TeaGet MilkGet Sugar LumpsEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle BoilsPut Tea in PotPut Boiling Water in PotWait 2 MinutesPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User

Page 13: Sequences Modules08

Example: Making a Cup of TeaMake a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in PotAdd Tea, Milk and Sugar to Cup

Give Cup of Tea to User

Fetch Utensils and IngredientsGet CupGet KettleGet TeaGet MilkGet Sugar Lumps

Boil Water in KettleEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle Boils

Make Tea in PotPut Tea in PotPut Boiling Water in PotWait 2 Minutes

Add Tea, Milk and Sugar to CupPut Milk in CupPour Tea in CupPut 1 Sugar Lump in CupStir Tea in CupGive Cup of Tea to User

Page 14: Sequences Modules08

Variables• Variables are named items in an algorithm that can

take a variable value

• For example when calculating VAT

▫Tax, Value of Product, and Total Cost are variables

Pay for Bike Including TaxTax = 17.5Value of Product = 200Total Cost = Value of Product * (100 + Tax)/100Customer pays Total Cost

Page 15: Sequences Modules08

Parameters• Parameters are variables that are passed to a module

and are used inside like variables

• For example when calculating VAT

▫ Value of Bike is a Parameter, Tax and Total Cost are Variables

Buy New BikeSelect BikePay for Bike Including Tax (200)Take Bike Home

Pay for Bike Including Tax (Value of Bike)Tax = 17.5Total Cost = Value of Bike * (100 + Tax)/100Customer pays Total Cost

Page 16: Sequences Modules08

Where are the Variables and Parameters?

Make a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in PotAdd Tea, Milk and Sugar to Cup

Give Cup of Tea to User

Fetch Utensils and IngredientsGet CupGet KettleGet TeaGet MilkGet Sugar Lumps

Boil Water in KettleEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle Boils

Make Tea in PotPut Tea in PotPut Boiling Water in PotWait 2 Minutes

Add Tea, Milk and Sugar to CupPut Milk in CupPour Tea in CupPut 1 Sugar Lumps in CupStir Tea in CupGive Cup of Tea to User

Page 17: Sequences Modules08

Where are the Variables and Parameters?

Make a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in PotAdd Tea, Milk and Sugar to Cup

Give Cup of Tea to User

Fetch Utensils and IngredientsGet CupGet KettleGet TeaGet MilkGet Sugar Lumps

Boil Water in KettleEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle Boils

Make Tea in PotPut Tea in PotPut Boiling Water in PotWait 2 Minutes

Add Tea, Milk and Sugar to CupPut Milk in CupPour Tea in CupPut 1 Sugar Lumps in CupStir Tea in CupGive Cup of Tea to User

Page 18: Sequences Modules08

Where are the Variables and Parameters?

Make a Cup of TeaFetch Utensils and IngredientsBoil Water in KettleMake Tea in Pot (2)Add Tea, Milk and Sugar to Cup

(1)

Give Cup of Tea to User

Fetch Utensils and IngredientsGet CupGet KettleGet TeaGet MilkGet Sugar Lumps

Boil Water in KettleEmpty KettleFill Kettle with WaterSwitch Kettle onWait until Kettle Boils

Make Tea in Pot (n_min)Put Tea in PotPut Boiling Water in PotWait n_min Minutes

Add Tea, Milk and Sugar to Cup (n_sugars)Put Milk in CupPour Tea in CupPut n_sugars Sugar Lumps in CupStir Tea in CupGive Cup of Tea to User

Page 19: Sequences Modules08

Summary

• Pseudocode▫ High level description of algorithm…▫ … intended for human reading…▫ … but structured like a programming language

• Modules (subroutines/ functions/ methods) ▫ Break down bigger algorithms into chunks▫ Improves Clarity and Reuse

• Variables and Parameters▫ Are named things with a value (like in algebra)▫ Can make algorithms more flexible▫ Can also improve Reuse