3 november 2015birkbeck college, u. london1 introduction to computer systems lecturer: steve maybank...

22
3 November 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 6b: Arrays

Upload: mitchell-greene

Post on 19-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

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

3 November 2015 Birkbeck College, U. London 1

Introduction to Computer Systems

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

[email protected] 2015

Week 6b: Arrays

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

3 November 2015 Brookshear, Section 2.1 2

Review: parts of a computer

arithmetic/logicunit

register unit

central processing unit

:

registers

bus

mainmemory

control unit

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

Review: instructions Instructions are simple commands

which can be carried out by the CPU.

There are only a small number of different types of instruction.

Most instructions contain a memory address or a register name or data.

3 November 2015 Brookshear, Appendix C 3

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

Review: computing

1. Write program in a high level language2. Compile: reduce program to a

sequence of machine code instructions3. Load: place sequence of instructions in

main memory4. Run: CPU carries out the instructions

one by one

3 November 2015 Birkbeck College, U. London 4

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

3 November 2015 Brookshear, Sections 6.2 and 8.1 5

Array

Definition: a block of values, all of the same type (= homogeneous array).

Examples of one dimensional arrays:red_ ora

nblue

gree

1000

0010

1010

each value: 4 characters

each value: 4 bits

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

3 November 2015 Brookshear, Section 8.1 6

Two Dimensional Array Example of a 2D array:

-34 32 1 8 26 89

34 -67 21 43 67 -34

-23 10 34 67 89 12

Each value is an integer in the range –256 to 255

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

3 November 2015 Brookshear, Section 8.1 7

These Are Not Arrays

5 red green

7 The entries havedifferent types

32 45 10 22 32 40

Variable row length

32 45 10 22

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

3 November 2015 Birkbeck College, U. London 8

Size of an Array The size of a one dimensional array

is the number of entries. The size of a two dimensional array

is the number of rows and the number of columns.

45 10^(10)

-1 -5

This is a 2x2 array:

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

3 November 2015 Brookshear, Section 8.1 9

Why Use Arrays? Answer 1 Blocks of numbers turn up in

many applications.

36

82

93

E.g. studentmarks forsubject S1

John

Angela

Fred

S1

The labels John,Angela, Fred, S1are not part ofthe array.

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

3 November 2015 Birkbeck College, U. London 10

Why Use Arrays? Answer 2

Names of variables can be used efficiently

Eg a 1000x1000 array can be referred to using a single name such as A.

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

3 November 2015 Brookshear, Section 6.2 11

Notation for Array Entries

Array indexing starts from 0 in Python

In 2D arrays the row is given first, then the column

A[0]=36; B[0,0]=36; B[1,2]=62;

36

82

93

36 46 74

82 34 62

93 28 59

A= B=

columns ->rows

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

3 November 2015 Birkbeck College, U. London 12

Calculation of an Average Mark

students: numbered 0 to m-1 courses: numbered 0 to n-1 M[i, j] = mark obtained by student i on course

j

# obtain the average mark for student ia = 0; j = 0; n = number of columns of MWhile j < n

a = a+M[i, j]j = j+1

EndWhile a = a/n

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

3 November 2015 Birkbeck College, U. London 13

Average Mark Python Style students: numbered 0 to m-1 courses: numbered 0 to n-1 M[i, j] = mark obtained by student i on course j

# obtain the average mark for student ia = 0j = 0while j < n : # : indicates a compound

statementa = a+M[i, j] # note the indentationj = j+1

a = a/n

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

3 November 2015 Birkbeck College, U. London 14

for Loop in Python

for i in range(5) : print(i) # prints 0, 1, 2, 3, 4

The colon signals a compound statement Note the indentation range creates a sequence of integers Each time round the loop a new value of i is

used

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

3 November 2015 Birkbeck College, U. London 15

Calculation Using a for Loop students: numbered 0 to m-1 courses: numbered 0 to n-1 M[i, j]=mark obtained by student i on course

j

# obtain the average mark for student ia =0for j in range(n) :

a = a+M[i, j]a=a/n

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

3 November 2015 Birkbeck College, U. London 16

Largest Value in an Array

Input: values, which is a non-empty 1D array of integers

Output: the largest entry in values

largest = values[0]for i in range(length values) : if values[i] > largest :

largest = values[i]print(largest)

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

3 November 2015 Birkbeck College, U. London 17

SearchInput: values, which is a non-empty 1D array of

integers, and a searched valueOutput: message to show if the value was found

searchedValue = 100flag = Falsefor i in range(length values) : if values[i] == searchedValue :

flag = Trueprint(“found at position: ”, i)

if flag== False :print(“not found”)

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

Storage of an Array in Memory

3 November 2015 Brookshear, Section 8.3 18

memory4039 42 43 44 4541

27 0 79 1111 D array

2D array911

2

4

4 0

3 01

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

3 November 2015 Brookshear, Section 8.3 19

1D Array Stored in Memory

A[0]

A[1]

A[2]

A[3]

A[4]

A[5]

A[6]

Memory address: x x+1 x+2 x+3 x+4 x+5 x+6

Suppose that each array entry occupies one memory cell.If x is the address of A[0], then the addressof A[n] is x+n.

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

3 November 2015 Brookshear, Section 8.3 20

2D Array Stored in Memory

2D array with 4 rows and 2 columns stored in row major order

B[0][0]

B[0][1]

B[1][0]

B[1][1]

B[2][0] B[2][1]

B[3][0]

B[3][1]

1st row 2nd row 3rd row 4th row

B[i, j]=entry at intersection of (i+1)th row and (j+1)th column

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

3 November 2015 Brookshear, Section 8.3 21

Address of the i,j th Array Entry

m=number of rows n=number of columns x=address of B[0, 0] Each array entry occupies one memory

cell Address of B[i, j] (row major order) is

x+(i*n+j)

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

Problem

How can an array C of size 10x10x10 be stored in memory?

If C[0,0,0] is stored in cell x, then in which cell is C[2, 5, 1] stored?

3 November 2015 Birkbeck College, U. London 22