Sequences, Modules and Variables David Millard (dem@ecs.soton.ac.uk)
Overview <ul><li>Pseudocode </li></ul><ul><li>Flowcharts </li></ul><ul><li>What are Modules? </li></ul><ul><li>Variables <...
Pseudocode <ul><li>“ Pseudocode is a compact and informal high-level description of a computer programming algorithm that ...
Pseudocode <ul><li>“ Pseudocode is a compact and  informal high-level description  of a computer programming algorithm tha...
Pseudocode <ul><li>“ Pseudocode is a compact and  informal high-level description  of a computer programming algorithm tha...
Pseudocode <ul><li>“ Pseudocode is a compact and  informal high-level description  of a computer programming algorithm tha...
Example: Making a Cup of Tea
Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><u...
Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><u...
Modules <ul><li>Modules break an algorithm into logical parts </li></ul><ul><ul><li>Helps with Clarity and Understandabili...
Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><u...
Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><u...
Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></ul><ul><li>...
Variables <ul><li>Variables are named items in an algorithm that can take a variable value </li></ul><ul><li>For example w...
Parameters <ul><li>Parameters are variables that are  passed to a module  and are used inside like variables </li></ul><ul...
Where are the Variables and Parameters? <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></...
Where are the Variables and Parameters? <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></...
Where are the Variables and Parameters? <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></...
Summary <ul><li>Pseudocode </li></ul><ul><ul><li>High level description of algorithm… </li></ul></ul><ul><ul><li>…  intend...
Upcoming SlideShare
Loading in …5
×

Sequences Modules08

1,733 views

Published on

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

1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,733
On SlideShare
0
From Embeds
0
Number of Embeds
79
Actions
Shares
0
Downloads
24
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Sequences Modules08

  1. 1. Sequences, Modules and Variables David Millard (dem@ecs.soton.ac.uk)
  2. 2. Overview <ul><li>Pseudocode </li></ul><ul><li>Flowcharts </li></ul><ul><li>What are Modules? </li></ul><ul><li>Variables </li></ul><ul><li>Parameters </li></ul>
  3. 3. Pseudocode <ul><li>“ 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” </li></ul><ul><li>- Wikipedia </li></ul><ul><li>“ 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” </li></ul><ul><li>- Oxford English Dictionary </li></ul>
  4. 4. Pseudocode <ul><li>“ 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” </li></ul><ul><li>- Wikipedia </li></ul><ul><li>“ 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” </li></ul><ul><li>- Oxford English Dictionary </li></ul>
  5. 5. Pseudocode <ul><li>“ 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” </li></ul><ul><li>- Wikipedia </li></ul><ul><li>“ 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” </li></ul><ul><li>- Oxford English Dictionary </li></ul>
  6. 6. Pseudocode <ul><li>“ 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” </li></ul><ul><li>- Wikipedia </li></ul><ul><li>“ 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 ” </li></ul><ul><li>- Oxford English Dictionary </li></ul>
  7. 7. Example: Making a Cup of Tea
  8. 8. Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><ul><li>Get Tea </li></ul><ul><li>Get Milk </li></ul><ul><li>Get Sugar Lumps </li></ul><ul><li>Empty Kettle </li></ul><ul><li>Fill Kettle with Water </li></ul><ul><li>Switch Kettle on </li></ul><ul><li>Wait until Kettle Boils </li></ul><ul><li>Put Tea in Pot </li></ul><ul><li>Put Boiling Water in Pot </li></ul><ul><li>Wait 2 Minutes </li></ul><ul><li>Put Milk in Cup </li></ul><ul><li>Pour Tea in Cup </li></ul><ul><li>Put 1 Sugar Lump in Cup </li></ul><ul><li>Stir Tea in Cup </li></ul><ul><li>Give Cup of Tea to User </li></ul>Pseudocode Assumptions?
  9. 9. Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><ul><li>Get Tea </li></ul><ul><li>Get Milk </li></ul><ul><li>Get Sugar Lumps </li></ul><ul><li>Empty Kettle </li></ul><ul><li>Fill Kettle with Water </li></ul><ul><li>Switch Kettle on </li></ul><ul><li>Wait until Kettle Boils </li></ul><ul><li>Put Tea in Pot </li></ul><ul><li>Put Boiling Water in Pot </li></ul><ul><li>Wait 2 Minutes </li></ul><ul><li>Put Milk in Cup </li></ul><ul><li>Pour Tea in Cup </li></ul><ul><li>Put 1 Sugar Lump in Cup </li></ul><ul><li>Stir Tea in Cup </li></ul><ul><li>Give Cup of Tea to User </li></ul>Pseudocode <ul><li>Assumptions: </li></ul><ul><li>Electric Kettle </li></ul><ul><li>User wants Milk and Sugar </li></ul><ul><li>Only making 1 cup of tea </li></ul><ul><li>Nothing goes wrong! </li></ul>
  10. 10. Modules <ul><li>Modules break an algorithm into logical parts </li></ul><ul><ul><li>Helps with Clarity and Understandability </li></ul></ul><ul><li>Modules can be reused </li></ul><ul><ul><li>Within the same algorithm </li></ul></ul><ul><ul><li>In a different algorithm </li></ul></ul><ul><li>In Programming Modules can be called: </li></ul><ul><ul><li>Sub-routines (in older languages) </li></ul></ul><ul><ul><li>Functions (in procedural languages like C) </li></ul></ul><ul><ul><li>Methods (in object oriented languages like Java) </li></ul></ul>
  11. 11. Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><ul><li>Get Tea </li></ul><ul><li>Get Milk </li></ul><ul><li>Get Sugar Lumps </li></ul><ul><li>Empty Kettle </li></ul><ul><li>Fill Kettle with Water </li></ul><ul><li>Switch Kettle on </li></ul><ul><li>Wait until Kettle Boils </li></ul><ul><li>Put Tea in Pot </li></ul><ul><li>Put Boiling Water in Pot </li></ul><ul><li>Wait 2 Minutes </li></ul><ul><li>Put Milk in Cup </li></ul><ul><li>Pour Tea in Cup </li></ul><ul><li>Put 1 Sugar Lump in Cup </li></ul><ul><li>Stir Tea in Cup </li></ul><ul><li>Give Cup of Tea to User </li></ul>
  12. 12. Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Get Cup </li></ul><ul><li>Get Kettle </li></ul><ul><li>Get Tea </li></ul><ul><li>Get Milk </li></ul><ul><li>Get Sugar Lumps </li></ul><ul><li>Empty Kettle </li></ul><ul><li>Fill Kettle with Water </li></ul><ul><li>Switch Kettle on </li></ul><ul><li>Wait until Kettle Boils </li></ul><ul><li>Put Tea in Pot </li></ul><ul><li>Put Boiling Water in Pot </li></ul><ul><li>Wait 2 Minutes </li></ul><ul><li>Put Milk in Cup </li></ul><ul><li>Pour Tea in Cup </li></ul><ul><li>Put 1 Sugar Lump in Cup </li></ul><ul><li>Stir Tea in Cup </li></ul><ul><li>Give Cup of Tea to User </li></ul>Fetch Utensils and Ingredients Boil Water in Kettle Make Tea in Pot Add Tea, Milk and Sugar to Cup
  13. 13. Example: Making a Cup of Tea <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></ul><ul><li>Boil Water in Kettle </li></ul><ul><li>Make Tea in Pot </li></ul><ul><li>Add Tea, Milk and Sugar to Cup </li></ul><ul><li>Give Cup of Tea to User </li></ul>Fetch Utensils and Ingredients Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Boil Water in Kettle Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Make Tea in Pot Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Add Tea, Milk and Sugar to Cup Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lump in Cup Stir Tea in Cup Give Cup of Tea to User
  14. 14. Variables <ul><li>Variables are named items in an algorithm that can take a variable value </li></ul><ul><li>For example when calculating VAT </li></ul><ul><ul><li>Tax, Value of Product, and Total Cost are variables </li></ul></ul><ul><ul><li>Pay for Bike Including Tax </li></ul></ul><ul><ul><li>Tax = 17.5 </li></ul></ul><ul><ul><li>Value of Product = 200 </li></ul></ul><ul><ul><li>Total Cost = Value of Product * (100 + Tax)/100 </li></ul></ul><ul><ul><li>Customer pays Total Cost </li></ul></ul>
  15. 15. Parameters <ul><li>Parameters are variables that are passed to a module and are used inside like variables </li></ul><ul><li>For example when calculating VAT </li></ul><ul><ul><li>Value of Bike is a Parameter, Tax and Total Cost are Variables </li></ul></ul><ul><ul><li>Buy New Bike </li></ul></ul><ul><ul><li>Select Bike </li></ul></ul><ul><ul><li>Pay for Bike Including Tax (200) </li></ul></ul><ul><ul><li>Take Bike Home </li></ul></ul><ul><ul><li>Pay for Bike Including Tax (Value of Bike) </li></ul></ul><ul><ul><li>Tax = 17.5 </li></ul></ul><ul><ul><li>Total Cost = Value of Bike * (100 + Tax)/100 </li></ul></ul><ul><ul><li>Customer pays Total Cost </li></ul></ul>
  16. 16. Where are the Variables and Parameters? <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></ul><ul><li>Boil Water in Kettle </li></ul><ul><li>Make Tea in Pot </li></ul><ul><li>Add Tea, Milk and Sugar to Cup </li></ul><ul><li>Give Cup of Tea to User </li></ul>Fetch Utensils and Ingredients Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Boil Water in Kettle Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Make Tea in Pot Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Add Tea, Milk and Sugar to Cup Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lumps in Cup Stir Tea in Cup Give Cup of Tea to User
  17. 17. Where are the Variables and Parameters? <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></ul><ul><li>Boil Water in Kettle </li></ul><ul><li>Make Tea in Pot </li></ul><ul><li>Add Tea, Milk and Sugar to Cup </li></ul><ul><li>Give Cup of Tea to User </li></ul>Fetch Utensils and Ingredients Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Boil Water in Kettle Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Make Tea in Pot Put Tea in Pot Put Boiling Water in Pot Wait 2 Minutes Add Tea, Milk and Sugar to Cup Put Milk in Cup Pour Tea in Cup Put 1 Sugar Lumps in Cup Stir Tea in Cup Give Cup of Tea to User
  18. 18. Where are the Variables and Parameters? <ul><li>Make a Cup of Tea </li></ul><ul><li>Fetch Utensils and Ingredients </li></ul><ul><li>Boil Water in Kettle </li></ul><ul><li>Make Tea in Pot ( 2 ) </li></ul><ul><li>Add Tea, Milk and Sugar to Cup ( 1 ) </li></ul><ul><li>Give Cup of Tea to User </li></ul>Fetch Utensils and Ingredients Get Cup Get Kettle Get Tea Get Milk Get Sugar Lumps Boil Water in Kettle Empty Kettle Fill Kettle with Water Switch Kettle on Wait until Kettle Boils Make Tea in Pot ( n_min ) Put Tea in Pot Put Boiling Water in Pot Wait n_min Minutes Add Tea, Milk and Sugar to Cup ( n_sugars ) Put Milk in Cup Pour Tea in Cup Put n_sugars Sugar Lumps in Cup Stir Tea in Cup Give Cup of Tea to User
  19. 19. Summary <ul><li>Pseudocode </li></ul><ul><ul><li>High level description of algorithm… </li></ul></ul><ul><ul><li>… intended for human reading… </li></ul></ul><ul><ul><li>… but structured like a programming language </li></ul></ul><ul><li>Modules (subroutines/ functions/ methods) </li></ul><ul><ul><li>Break down bigger algorithms into chunks </li></ul></ul><ul><ul><li>Improves Clarity and Reuse </li></ul></ul><ul><li>Variables and Parameters </li></ul><ul><ul><li>Are named things with a value (like in algebra) </li></ul></ul><ul><ul><li>Can make algorithms more flexible </li></ul></ul><ul><ul><li>Can also improve Reuse </li></ul></ul>

×