16
29 September 2015 Birkbeck College, U. London 1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems [email protected] Autumn 2015 Week 1b: Algorithms

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

Embed Size (px)

Citation preview

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

29 September 2015 Birkbeck College, U. London 1

Introduction to Computer Systems

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

[email protected] 2015

Week 1b: Algorithms

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

29 September 2015 Birkbeck College, U. London 2

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

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

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 basket4. Break open the pea pod5. Dump the peas from the pod into the

bowl6. Discard the pod

29 September 2015 Brookshear Section 5.1 3

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

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 2015 Birkbeck College, U. London 4

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

Effect of Removing Instruction 2

1. Obtain a basket of unshelled pea pods and an empty bowl

2. Do nothing3. Take a pea pod from the basket4. Break open the pea pod5. Dump the peas from the pod into the

bowl6. Discard the pod

29 September 2015 Birkbeck College, U. London 5

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

Flow Chart

29 September 2015 Birkbeck College, U. London 6

is there apea pod in the basket?

haltno

carry out3, 4, 5, 6

yes

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

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 2015 Birkbeck College, U. London 7

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

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 2010 Birkbeck College, U. London 8

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

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 2015 Birkbeck College, U. London 9

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

29 September 2015 Birkbeck College, U. London 10

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.

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

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 2010 Birkbeck College, U. London 11

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

29 September 2015 Birkbeck College, U. London 12

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.

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

29 September 2015 Birkbeck College, U. London 13

First Example of an Algorithm

Input: integers 12, 5Output: quotient q and remainder r on dividing 12

by 5Algorithm1. q = 0; r = 122. Subtract 5 from r; Increase q by 13. Subtract 5 from r; Increase q by 14. Output q, r5. Halt

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

29 September 2015 Birkbeck College, U. London 14

Second Example of an Algorithm

Input: strictly positive integers m, nOutput: quotient q and remainder r on dividing m

by nAlgorithm1. q = 02. r = m3. If r < n, Output q, r; Halt4. r = r-n5. q = q+1 6. go to 3

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

29 September 2015 Birkbeck College, U. London 15

Third Example of an AlgorithmInput: strictly positive integers m, nOutput: quotient q and remainder r on dividing m by nAlgorithm1. q = 02. r = m3. While r >= n,4. r = r-n5. q = q+1 6. EndWhile7. Output q, r8. Halt

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

29 September 2015 Birkbeck College, U. London 16

Exercise

Sketch an algorithm that takes as input a strictly positive integer n and outputs an integer k such that

nn kk 12 and 2