Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 3 November 2015Brookshear, Section 2.12 Review: parts of a computer arithmetic/logic unit register unit central processing unit : registers bus main memory control unit

3 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 2015Brookshear, Appendix C3

4 Review: computing 1. Write program in a high level language 2. Compile: reduce program to a sequence of machine code instructions 3. Load: place sequence of instructions in main memory 4. Run: CPU carries out the instructions one by one 3 November 2015Birkbeck College, U. London4

5 3 November 2015Brookshear, Sections 6.2 and 8.15 Array  Definition: a block of values, all of the same type (= homogeneous array).  Examples of one dimensional arrays: red_oranbluegree 100000101010 each value: 4 characters each value: 4 bits

6 3 November 2015Brookshear, Section 8.16 Two Dimensional Array Example of a 2D array: -3432182689 34-67214367-34 -231034678912 Each value is an integer in the range –256 to 255

7 3 November 2015Brookshear, Section 8.17 These Are Not Arrays 5redgreen7 The entries have different types 324510223240 Variable row length 32451022

8 3 November 2015Birkbeck College, U. London8 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. 4510^(10) -5 This is a 2x2 array:

9 3 November 2015Brookshear, Section 8.19 Why Use Arrays? Answer 1 Blocks of numbers turn up in many applications. 36 82 93 E.g. student marks for subject S1 John Angela Fred S1 The labels John, Angela, Fred, S1 are not part of the array.

10 3 November 2015Birkbeck College, U. London10 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.

11 3 November 2015Brookshear, Section 6.211 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 364674 823462 932859 A= B= columns ->rows 

12 3 November 2015Birkbeck College, U. London12 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 i a = 0; j = 0; n = number of columns of M While j < n a = a+M[i, j] j = j+1 EndWhile a = a/n

13 3 November 2015Birkbeck College, U. London13 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 i a = 0 j = 0 while j < n : # : indicates a compound statement a = a+M[i, j] # note the indentation j = j+1 a = a/n

14 3 November 2015Birkbeck College, U. London14 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

15 3 November 2015Birkbeck College, U. London15 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 i a =0 for j in range(n) : a = a+M[i, j] a=a/n

16 3 November 2015Birkbeck College, U. London16 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)

17 3 November 2015Birkbeck College, U. London17 Search Input: values, which is a non-empty 1D array of integers, and a searched value Output: message to show if the value was found searchedValue = 100 flag = False for i in range(length values) : if values[i] == searchedValue : flag = True print(“found at position: ”, i) if flag== False : print(“not found”)

18 Storage of an Array in Memory 3 November 2015Brookshear, Section 8.318 memory 40394243444541 27079111 1 D array 2D array 911 2 4 40 301

19 3 November 2015Brookshear, Section 8.319 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 address of A[n] is x+n.

20 3 November 2015Brookshear, Section 8.320 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] 1 st row 2 nd row 3 rd row 4 th row B[i, j]=entry at intersection of (i+1)th row and (j+1)th column

21 3 November 2015Brookshear, Section 8.321 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)

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


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

Similar presentations


Ads by Google