Presentation is loading. Please wait.

Presentation is loading. Please wait.

29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Similar presentations


Presentation on theme: "29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

1 29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2015 Week 1b: Algorithms

2 29 September 2015Birkbeck College, U. London2 Informal Algorithms Directions to go from one place to another. Cooking recipes. How to use a device (TV, microwave, etc.) How to assemble flat pack furniture A list of instructions for Tom

3 Example of a Cooking Algorithm 1. Obtain a basked of unshelled pea pods and an empty bowl. 2. As long as there are unshelled pea pods in the basket 3. Take a pea pod from the basket 4. Break open the pea pod 5. Dump the peas from the pod into the bowl 6. Discard the pod 29 September 2015Brookshear Section 5.13

4 Commentary Input: basket of unshelled pea pods and an empty bowl Output: bowl of peas and the pea pods Variable: the number of pea pods in the basket The instructions are carried out in a strict order, one after the other Instruction 2 affects the order in which the other instructions are carried out. 29 September 2015Birkbeck College, U. London4

5 Effect of Removing Instruction 2 1. Obtain a basket of unshelled pea pods and an empty bowl 2. Do nothing 3. Take a pea pod from the basket 4. Break open the pea pod 5. Dump the peas from the pod into the bowl 6. Discard the pod 29 September 2015Birkbeck College, U. London5

6 Flow Chart 29 September 2015Birkbeck College, U. London6 is there a pea pod in the basket? halt no carry out 3, 4, 5, 6 yes

7 Reduced Version of the Cooking Algorithm 1. Store a non-negative integer in a memory location called basket 2. As long as the value stored in basket is strictly greater than 0 3. Subtract 1 from the value stored in basket 29 September 2015Birkbeck College, U. London7

8 Variables 1 A variable is a named memory location e.g. the variable q. q=5 // store the value 5 in the memory location specified by q q=5 // assign the value 5 to the variable q The exact memory location specified by q is chosen when the program runs 12 October 2010Birkbeck College, U. London8

9 Variables 2 q=5+2 // evaluate the right hand side. Assign the resulting value to q. q=q+2 // evaluate the right hand side. Assign the resulting value to q. 5=q // error 29 September 2015Birkbeck College, U. London9

10 29 September 2015Birkbeck College, U. London10 Definition of an Algorithm An algorithm is an ordered set of unambiguous executable steps that defines a terminating process. It is implicit that something (e.g. a machine) carries out the steps.

11 Commentary Terminating process: print all the integers in the range 1 to 10 Non-terminating process: print all the integers Executable step: assign to x the value 1 Non executable step: attempt to assign to x a value larger than any that can be stored. 12 October 2010Birkbeck College, U. London11

12 29 September 2015Birkbeck College, U. London12 Algorithms and Computers An algorithms is converted into a list of instructions (program) for a particular computer. The details of the instructions vary from one computer to another If an algorithm is programmable on one computer, then in principle it is programmable on any computer.

13 29 September 2015Birkbeck College, U. London13 First Example of an Algorithm Input: integers 12, 5 Output: quotient q and remainder r on dividing 12 by 5 Algorithm 1. q = 0; r = 12 2. Subtract 5 from r; Increase q by 1 3. Subtract 5 from r; Increase q by 1 4. Output q, r 5. Halt

14 29 September 2015Birkbeck College, U. London14 Second Example of an Algorithm Input: strictly positive integers m, n Output: quotient q and remainder r on dividing m by n Algorithm 1. q = 0 2. r = m 3. If r < n, Output q, r; Halt 4. r = r-n 5. q = q+1 6. go to 3

15 29 September 2015Birkbeck College, U. London15 Third Example of an Algorithm Input: strictly positive integers m, n Output: quotient q and remainder r on dividing m by n Algorithm 1. q = 0 2. r = m 3. While r >= n, 4. r = r-n 5. q = q+1 6. EndWhile 7. Output q, r 8. Halt

16 29 September 2015Birkbeck College, U. London16 Exercise Sketch an algorithm that takes as input a strictly positive integer n and outputs an integer k such that


Download ppt "29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."

Similar presentations


Ads by Google