=0; i--){ System.out.println(x[i]); // the result is printing the input in reverse sequence }"> =0; i--){ System.out.println(x[i]); // the result is printing the input in reverse sequence }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

 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.

Similar presentations


Presentation on theme: " 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."— Presentation transcript:

1

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 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

3 import 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 }

4  Arrays can be used with any of the primitive types char [] 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;

5  We can also have Arrays of more complex types… String [] months = {"Jan", "Feb", "Mar"…"Dec"};

6  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.

7  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

8  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

9  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.

10  Goodyear have collected vehicle data and translated it into 4D visualisation (4 th 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

11  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

12

13

14  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] 56 79 42 012012 0 1 values[2][0]

15  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 12 3 246 369 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

16 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.

17 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.

18 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 1 black = 24 row 2

19 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 1 banana = ears

20 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 3 orange = 15.2

21 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

22 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

23 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.

24 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.

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

26  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

27  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

28  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

29  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

30  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 cannot be done

31  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

32 The Greek letter sigma, , denotes summation. 32 = (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

33  First, the rule to multiply a row by a column IN THAT ORDER is given by: 33 This means multiply x 1 by y 1, x 2 by y 2, and so on, then add up the results. - the result is a scalar (a single number)

34 34 = (4  2) + (5  (-3)) + (2  1) = 8 – 15 + 2 = -5

35 35 0  8 + -2  -3 + 4  1 + 7  2 = 0 + 6 + 4 + 14 = 24

36  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

37 37 To begin with we will concentrate on 2  2 matrices, where the rule becomes For example:

38  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

39  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

40  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

41 41 2  3 3  1 2  1

42  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 (often called I 2 and I 3 ).

43  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  A I = I A = A. 43 Check this by multiplying

44  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

45  The inverse of A = is 45 The scalar ad - bc is the determinant of A. If ad – bc = 0, the matrix is singular (has no inverse).

46  Inverse of A = is 46 e.g. the inverse of is which is i.e.

47 47 Reflection in the x-axis of the x/y plane. y (x,y). x (x,y) if (x, y)  (x, y) then i.e so

48 48 This 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) and so (5, 2) goes to (5, -2).

49 Rotation 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) y (-2.83. 5.10) (3, 5) 60  O x

50 50 The matrix sends (x, y) to (y, x), since It represents a reflection in the diagonal line y = x. y (x,y) O x (y,x)

51 51 We can transform a whole set of points (as columns in a matrix) by a single matrix multiplication. e.g. 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.

52 52 If 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 -1 A(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.

53  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/ http://csl-staff.wlv.ac.uk/~in6659/matrices/


Download ppt " 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."

Similar presentations


Ads by Google