Download presentation
Presentation is loading. Please wait.
1
Introduction to Computer Systems
Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2017 Week 1b: Algorithms 10 January 2017 Birkbeck College, U. London
2
Birkbeck College, U. London
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 10 January 2017 Birkbeck College, U. London
3
Example of a Cooking Algorithm
Obtain a basket of unshelled pea pods and an empty bowl. As long as there are unshelled pea pods in the basket Take a pea pod from the basket Break open the pea pod Dump the peas from the pod into the bowl Discard the pod 10 January 2017 Brookshear Section 5.1
4
Birkbeck College, U. London
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. 10 January 2017 Birkbeck College, U. London
5
Effect of Removing Instruction 2
Obtain a basket of unshelled pea pods and an empty bowl Do nothing Take a pea pod from the basket Break open the pea pod Dump the peas from the pod into the bowl Discard the pod 10 January 2017 Birkbeck College, U. London
6
Birkbeck College, U. London
Flow Chart is there a pea pod in the basket? no halt yes carry out 3, 4, 5, 6 10 January 2017 Birkbeck College, U. London
7
Reduced Version of the Cooking Algorithm
Store a non-negative integer in a memory location called basket As long as the value stored in basket is strictly greater than 0 Subtract 1 from the value stored in basket 10 January 2017 Birkbeck College, U. London
8
Birkbeck College, U. London
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 10 January 2017 Birkbeck College, U. London
9
Birkbeck College, U. London
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 10 January 2017 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. 10 January 2017 BB Section 5.1
11
Birkbeck College, U. London
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. 10 January 2017 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. 10 January 2017 Birkbeck College, U. London
13
First Example of an Algorithm
Input: integers 12, 5 Output: quotient q and remainder r on dividing 12 by 5 Algorithm q = 0; r = 12 Subtract 5 from r; Increase q by 1 Output q, r Halt 10 January 2017 Birkbeck College, U. London
14
Second Example of an Algorithm
Input: strictly positive integers m, n Output: quotient q and remainder r on dividing m by n Algorithm q = 0 r = m If r < n, Output q, r; Halt r = r-n q = q+1 go to 3 10 January 2017 Birkbeck College, U. London
15
Third Example of an Algorithm
Input: strictly positive integers m, n Output: quotient q and remainder r on dividing m by n Algorithm q = 0 r = m While r >= n, r = r-n q = q+1 EndWhile Output q, r Halt 10 January 2017 Birkbeck College, U. London
16
Building a New Algorithm
Give the third algorithm a name: division(m, n) Show how division(m, n) can be used in a new algorithm to print out all the exact divisors of m, i.e. those integers n ≥1 for which m/n is an integer. 10 January 2017 Birkbeck College, U. London
17
Birkbeck College, U. London
Exercise Sketch an algorithm that takes as input a strictly positive integer n and outputs an integer k such that 10 January 2017 Birkbeck College, U. London
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.