+ All Categories
Home > Documents > An array is a structure that holds multiple values of the same type. ◦ Allows us to use one...

An array is a structure that holds multiple values of the same type. ◦ Allows us to use one...

Date post: 25-Dec-2015
Category:
Upload: vernon-west
View: 217 times
Download: 1 times
Share this document with a friend
Popular Tags:
53
Lecture 10 Two-dimensional arrays
Transcript
Page 1: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Lecture 10

Two-dimensional arrays

Page 2: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access

multiple items of data. The length of an array (the number of

items it contains) is established when the array is created (at runtime).

After creation, an array is a fixed-length structure. ◦ Called a static data structure

Arrays

Page 3: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Example Programimport javax.swing.JOptionPane;public class inputAndReverse { public static void main (String args []) { int [] x = new int[3]; // create array size 3 int i; String inputNr; // ask for a number using size of array for the exit condition // increment the control variable by 1 each time for(i=0; i<x.length; i++){ inputNr =JOptionPane.showInputDialog(null,"Enter number"); x[i] = Integer.parseInt(inputNr); } // ask for a number using size of array for the initial value // reduce the control variable by 1 each time i-- // this means it will go through the array in reverse // from the last element of the array to the first element for(i=x.length-1; i>=0; i--){ System.out.println(x[i]); // the result is printing the input in reverse sequence } }

Page 4: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Arrays of Other Types Arrays can be used with any of the primitive

typeschar [] c = {'j','a','v','a',' ','i','s',' ',

'n','i','c','e'};

double [] values = new double[3];

values[0] = 3.1;

values[1] = 3.2;

values[2] = 7.5;

Page 5: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Arrays of Other Types We can also have Arrays of more complex

types…

String [] months = {"Jan", "Feb", "Mar"…"Dec"};

Page 6: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

1-Dimensional Data So far we have only looked at 1-dimensional

data (1D).◦ Suitable for linear lists of objects

e.g. books in a library◦ Sequences of numbers or characters to be

processed.◦ 1D data can be stored in a 1D array and accessed

with a single index. Multidimensional data is very common, so

we need a means of storing it.

Page 7: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

2-Dimensional Data Chess board

◦ One axis labelled with numbers◦ One axis labelled with letters◦ A coordinate pair uniquely identifies a

square e.g. E4 Map

◦ A grid reference (coordinate pair) uniquely defines a point on the map

Pixels on a monitor◦ Integers are used to specify a pixel

location e.g. (0, 0) is the top left pixel

Page 8: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

3 – Dimensional Data Real world coordinates (x, y, z)

◦ x, y are floor coordinates◦ z is height off the floor

2D data over time◦ e.g. an animation◦ Each pixel requires 3 indices

x, y and frame number

time (frame number)y

x

Page 9: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Higher Dimensions 3D coordinates over time = 4D

◦ The path of an aeroplane (recorded at discrete time intervals)

◦ 4 indices are required to uniquely identify a data element.

Abstractions of problems in science and engineering can require even more dimensions.◦ An index is required for every dimension.

Page 10: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

CAD Research and Development

Goodyear have collected vehicle data and translated it into 4D visualisation (4th dimension is time)

Purpose is to analyse the car’s performance and its effect on the tyres

Engineers can simultaneously view 20 different elements of data collected from a test run

Page 11: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Can replay the data lap by lap in a real-time visualisation

Tyres swell or shrink to represent tyre pressure changes

Blue and yellow boxes along each side grow and shift to represent load transfers

Dials above the car indicate steering angle and over steering

Track side plot shows acceleration and deceleration as green and red vertical peaks

CAD Research and Development

Page 12: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.
Page 13: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.
Page 14: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

2-Dimensional Arrays Multi-dimension arrays are declared and used

just like the 1D arrays we have used before.

int [] [] values = {{5, 6}, {7, 9}, {4, 2}};

int [] [] values = new int [3][2];values[0][0] = 5;values[0][1] = 6;values[1][0] = 7;values[1][1] = 9;values[2][0] = 4;values[2][1] = 2;

3 rows and 2 columns

• More dimensions could be added by using more indices i.e. more [n]

5 6

7 9

4 2

0

1

2

0 1

values[2][0]

Page 15: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

A 2D example program What does the following code output?

for(x=1; x<=3; x++) {

for(y=1; y<=3; y++) {

System.out.print(x * y);

System.out.print("\t");

} // end for y

System.out.println();

} // end for x

1 2

3

2 4

6

3 6

9

A times table chart.

Values of multiplications can be found by using 2 indices.

tab

for-loops contained in for-loops are called nested loops

Page 16: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

public class TimesTable { public static void main (String args []) { int [] [] table;

// create table table = new int[13][13]; for(int row=0; row<13; row++) { for(int col=0; col<13; col++) { table[row][col] = row * col; } // end for col } // end for row } // end main} // end class

How could we print out the times table?

We could have put table.length here instead of 13.

We could have put table[0].length here.

A 2D array is an array of arrays.

table is an instance variable – its scope is the whole class.

row and col are instance variables – their scope is only the for loops.

Page 17: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

The answer

is . . .

public class TimesTable { public static void main (String args []) { int [] [] table; int row, col;

// create table table = new int[13][13]; for(row=0; row<13; row++) { for(col=0; col<13; col++) { table[row][col] = row * col; } // end for col } // end for row

// print table for(row=0; row<13; row++) { for(col=0; col<13; col++) { System.out.print(table[row][col]); } // end for col System.out.println(); } // end for row } // end main} // end class

Note the similarity between the nested loops used to calculate the table and to print it out.

Page 18: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

What is stored in the variable black after this code is run?

int [ ][ ] neck = {{59, 95, 87}, {99, 64, 16}, {36, 24, 76}, {71, 29, 17}};int black = neck[2][1];

neck[2][1] = row 2 column 1black = 24

row 2

Page 19: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

What is stored in the variable banana after this code is run?

String [ ][ ] potato = { {"back", "ears", "asparagus"}, {"beans", "pink", "mouth"}, {"corn", "foot", "cucumber"}, {"white", "nose", "pear"}};String banana = potato[0][1];

potato[0][1] = row 0 column 1banana = ears

Page 20: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

What is stored in the variable orange after this code is run?

double [ ][ ] spinach = { {16.9, 11.5, 30.3, 15.2}, {26.2, 17.0, 30.2, 13.2}, {30.7, 29.7, 25.7, 22.5}};double orange = spinach[0][3];

spinach[0][3] = row 0 column 3orange = 15.2

Page 21: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

x has 2 indices. What is the largest value the first index can have?

int [ ][ ] x = {{34, 58}, {93, 38}, {96, 39}};

Array x has three rows and two columns.Therefore the largest value for the first index (row) will be 2

Page 22: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

u has 2 indices. What is the largest value the second index can have?

int [ ][ ] u = {{43, 88, 16}, {93, 83, 50}, {89, 90, 21}, {85, 44, 41}};

Array u has four rows and three columns.Therefore the largest value for the second index (column) will be 2

Page 23: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

What is the value of y.length?

int [ ][ ] y = {{90, 70, 62, 71}, {86, 45, 61, 57}};

y.Length is the numbers of rows.Array y has two rows and four columns.Therefore the value of y.length will be 2.

Page 24: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

What is the value of s[2].length?

int [ ][ ] s = {{77, 32, 41, 46}, {72, 82, 75, 86}, {78, 26, 16, 15}};

S[2].length is the number of columns in row 2.Array s has three rows and four columns.Therefore s[2].length will be 4 because row index number 2 has four columns.

A two-dimensional array is an array of one-dimensional arrays.

Page 25: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Matrices (singular : “matrix”) have applications in· Solution of systems of linear equations·   Operational research·   Transformation geometry·   Computer graphics·   Games development

25

Matrices

Page 26: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

A matrix is a rectangular array of numbers enclosed by round brackets.

If there are m rows and n columns it is an m n matrix (“n by m”).

e.g. a 3 4 matrix :

has 3 rows and 4 columns

26

Definition and Example

11310

4272/1

5012

Page 27: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

An m n matrix can only be added to/subtracted from another m n matrix.

- result an m n matrix found by adding/subtracting corresponding elements

e.g.

27

Addition & Subtraction

??1

??4

511

220

372

104

Page 28: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

An m n matrix can only be added to/subtracted from another m n matrix.

- result an m n matrix found by adding/subtracting corresponding elements

e.g.

28

Addition & Subtraction

?81

?24

511

220

372

104

Page 29: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

An m n matrix can only be added to/subtracted from another m n matrix.

- result an m n matrix found by adding/subtracting corresponding elements

e.g.

29

Addition & Subtraction

881

324

511

220

372

104

Page 30: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

An m n matrix can only be added to/subtracted from another m n matrix.

- result an m n matrix found by adding/subtracting corresponding elements

e.g.

30

Addition & Subtraction

881

324

511

220

372

104

35

55

32

42

03

17

31

72

013

124 cannot be done

Page 31: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

In matrix theory a single number is called a scalar To calculate A ( a scalar, A any matrix) we

multiply all entries in A by . e.g.

31

Multiplication by a scalar

8

6

2

4

3

1

2

20

2/12/1

40

112

1

205

3422

4

6

0

8

10

4

Page 32: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

The Greek letter sigma, S, denotes summation.

32

A Reminder about Summations

5

2

43i

i

= (3x2 + 4) + (3x3 + 4) + (3x4 + 4) + (3x5 + 4)

= 10 + 13 + 16 +19

=58

upper limit of i

i is index of summation

lower limit of i

int i;

int sum = 0;

for(i=2;i<=5;i++) {

sum += (3*i)+4;

}

sum += (3*i)+4

is equivalent to

sum = sum + (3*i)+4

Page 33: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

First, the rule to multiply a row by a column IN THAT ORDER is given by:

33

Matrix Multiplication

This means multiply x1 by y1, x2 by y2, and so on, then add up the results.

- the result is a scalar (a single number)

n

1iii

n

1

n1 yx

y

y

).....x(x

Page 34: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

34

Matrix Multiplication Example

= (4 2) + (5 (-3)) + (2 1)

= 8 – 15 + 2

= -5

1

3

2

)254(

Page 35: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

35

Example

0 8 + -2 -3 + 4 1 + 7 2

= 0 + 6 + 4 + 14

= 24

2

1

3

8

)7420(

Page 36: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

If we multiply matrices A and B (in that order!) the result is called AB.

To find the entries in AB: If we multiply row 4 of A by column 2 of B (in

the way just described) the result becomes the entry in (row 4, column 2) of AB.

- and the same goes for the other rows and columns.

It’s much more complicated than adding them!

36

Matrix Multiplication

Page 37: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

37

Matrix MultiplicationTo begin with we will concentrate on 2 2 matrices, where the rule becomes

dhcfdgce

bhafbgae

hg

fe

dc

ba

81

2011

)42()03()22()13(

)45()01()25()11(

42

01

23

51

For example:

Page 38: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

To “multiply matrix A by matrix B” do we mean :

pre-multiply A by B - to get BA ? or post-multiply A by B - to get AB ? They may have different results, so it’s

important not to use one when the other is required.

38

Matrix Multiplication

Page 39: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Matrix division is not a valid operation !

(Since the question “What do you multiply B by to get A?” could have two possible answers, depending on whether we mean pre-multiply or post-multiply).

39

Matrix Division ?

Page 40: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

So far we have multiplied 2 2 matrices, but matrices of other sizes (and not necessarily square) can often be multiplied.

To be able to calculate AB, the length of the rows in A must equal the length of the columns in B

i.e. if A is m n, then B must be n s - the dimensions of AB will then be m s

40

More on Matrix Multiplication

Page 41: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

1

2

0

3

1

2

1

0

2

3

0

1

41

Matrix Multiplication

3

1

5

3

2

2

1

0

4

)33()12()50(

)32()11()54(

11

27

2 3 3 1 2 1

55

65

)11)22()00)(31()12()20(

)10()23()01)(10()13()21(

xxx

xxx

Page 42: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

If the entries on the main diagonal of a square matrix are all 1's and all other entries are zeros, it’s an identity matrix, denoted by I.

e.g.

42

Identity Matrices

100

010

001

10

01

(often called I2 and I3).

Page 43: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Identity matrices behave like the number 1 in ordinary arithmetic.

- multiplying a matrix by I doesn’t change it.

In particular, if A is any n n matrix and I is the n n identity then

AI = IA = A.

43

Identity Matrices

Check this by multiplying

10

01

32

14

32

14

Page 44: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

For a square matrix A, if there is a matrix B such that

AB = BA = I then B = A-1; B is the inverse of A. When A-1 exists it is unique, and A is called

a nonsingular matrix. A square matrix with no inverse is singular.

44

Inverse Matrices

Page 45: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

The inverse of A = is

45

Inverse of a 2 2 Matrix

dc

ba

ac-

b-d

bc-ad

1

The scalar ad - bc is the determinant of A.

If ad – bc = 0, the matrix is singular (has no inverse).

Page 46: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Inverse of A = is

46

Example

dc

ba

ac-

b-d

bc-ad

1

e.g. the inverse of

35

24

45-

2-3

10-12

1is

which is i.e.

45-

2-3

2

1

25/2-

1-3/2

Page 47: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

47

Application : GraphicsReflection in the x-axis of the x/y plane.

y

• (x,y)

. x

• (x,y)

if (x, y) (x, y) then

yy

xxi.e

1y0xy

0y1xx

y

x

10

01

y

xso

Page 48: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

48

Application : GraphicsThis transformation is "represented by" the matrix

y

•(x,y)

x

O

•(x,y)- to find where any point is sent, write it as a column and pre-multiply it by this matrix.

e.g. for the point (5, 2)

10

01

and so (5, 2) goes to (5, -2).

2

5

2

5

10

01

Page 49: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Another ExampleRotation through angle anticlockwise about origin O is represented by

e.g. for (3, 5)

e.g., for a 60 rotation, anticlockwise about O :

(3, 5) goes to (-2.83, 5.10)

cossin

sincos

60cos60sin

60sin60cos

10.5

83.2

5

3

5.0866.0

866.05.0

y

• •(-2.83. 5.10) (3, 5)

60

O x

5.0866.0

866.05.0

Page 50: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

50

Another ExampleThe matrix

sends (x, y) to (y, x), since

It represents a reflection in the diagonal line y = x.

01

10

x

y

y

x

01

10

y

• (x,y)

O x

(y,x)

Page 51: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

51

Action on a set of pointsWe can transform a whole set of points (as columns in a matrix) by a single matrix multiplication.

e.g.

312

043

312

043

10

01

so this matrix sends (3, 2) to (3, -2),

(4, -1) to (4, 1)

and (0, 3) to (0, -3).

A matrix can be used like this to transform a whole shape on the screen.

Page 52: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

52

Action of an inverse matrixIf the matrix A sends (x, y) to (x, y), i.e. A(x, y) = (x, y), and if B sends (x, y) to (x, y), i.e. B(x, y) = (x, y),Then BA(x, y) = B(x, y) = (x, y), so BA sends (x, y) to (x, y). - but AB may well do something different!

In particular, if the matrix A sends (x, y) to (x, y) then the inverse A-1 of A sends (x, y) back to (x, y).

This is because A-1(x, y) = A-1A(x, y) = I(x, y) = (x, y)

- because multiplying by I leaves points where they were.

So the inverse of A can be used to undo the action of A.

Page 53: An array is a structure that holds multiple values of the same type. ◦ Allows us to use one variable name to access multiple items of data.  The length.

Homework

Spend 5 to 10 minutes reading through what we have just covered◦ Ask questions if you don’t understand something

Finish Quiz week 9 (questions 8 to the end) Do workshops 10a and 10b Finish your Jafa exercises Do the Matrix Transformation exercises

http://csl-staff.wlv.ac.uk/~in6659/matrices/


Recommended