Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME...

14
Matrices and Nested For Loops Matrices 0 1 2 3 4 5 6 7 8 9 10 11 y 1 . . . . . . . . . matrix name element index NOTICE: index + 1 = size index starts at 0 not 1!! y [0][0] = is an actual coordinate row starts at 0, column starts at 0 Holds ONLY ONE value per element!! Homogeneous -- all of the elements have to be of the same type, e.g., int, float, char, etc. Coordinates in a matrix It’s important to find a pattern in a coordinate system when using a matrix Lupoli y (column) 0 1 2 3 4 5 6 1

Transcript of Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME...

Page 1: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

Matrices and Nested For Loops

Matrices 0 1 2 3 4 5 6 7 8 9 10 11 …y 0 … 1

. . . . . . . . .matrix name element indexNOTICE:index + 1 = sizeindex starts at 0 not 1!!y [0][0] = is an actual coordinaterow starts at 0, column starts at 0Holds ONLY ONE value per element!!Homogeneous -- all of the elements have to be of the same type, e.g., int, float, char, etc.

Coordinates in a matrixIt’s important to find a pattern in a coordinate system when using a matrix

Lupoliy (column)

x (row)

0 1 2 3 4 5 60 [0]

[0][0][1]

[0][2] [0][3] … … …

1 [1][0]

2 [2][0]

#2C

3 [3][0]

#1A

4 [4][0]

#4

5 … #3B

1

Page 2: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

6

What are the coordinates AROUND [2][4]??

Determine formula ALL coordinates for “C” on chart below

x y equation for coordinate to some point

[x-1][y][x][y-1] [x][y]

[x+1][y-1] [x+1][y]

This idea is used for Minesweeper!!

Declaration using different datatypes Matrices need a NAME and a size

o name one word, CAMELCASE floorOne floor_One

o size (coordinates, rows and columns) greater than 0 extra is fine, but not too much!!!

Matrices will have a value in EACH ELEMENT automatically!!!o used before in Computero called garbage!!!o good to set value (below) to your choice of a default

type [][]matrixname = new type[rows][columns];

x y equation for coordinate “C”

C == [2][4]

2

Page 3: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

Declaration of Multiple datatype MatricesDouble double [][] studentGPAs = new double[2][3];

[0] [1] [2]studentGPAs [0] [1]

Integer int [][] multiTable = new int[6][6];String String [][] names = new String[2][25];Char char [][] floorOne = new char[2][10];

Draw what the “floorOne” Matrices would look likeHow many elements does EACH matrix has above??

Placing/Updating values in an element AFTER YOU HAVE DELCARED THE MATRICES!!! Slightly different syntax depending on data type notice DATATYPE is NOT placed in from of assignment YOU CAN CHANGE A VALUE AFTERWARD!! AT ANY TIME!!!

Matricesname[index] = value;

Placing values in multiple datatype MatricesDouble Integer String Chardouble studentGPAs[0][0] = 3.65;// incorrect code

studentGPAs[0][0] = 3.65;studentGPAs[0][1] = 1.23;studentGPAs[0][2] = 4.00;studentGPAs[1][0] = 2.65;studentGPAs[1][1] = 1.79;studentGPAs[1][2] = 2.76;studentGPAs[2][0] = 2.65;studentGPAs[2][1] = 3.20;studentGPAs[2][2] = 2.78;

same as

String [][] names = new String[25][2];

names[0][0]= “Lupoli”;names[0][1]= “Mr.”;names[1][0]= “Grove”;names[1][1]= “Eric”;names[2][0]= “Reardon”;names[2][1]= “Greg”;names[3][0]= “Myers”;names[3][1]= “Will”;…

names[1][0] = “Munson”;

create the floor with ALL blanks (‘ ‘)

Draw what the names matrix would look like with the new data.I made a mistake in studentGPAs, the 5th element should be 1.80, how do I fix it?

3

Page 4: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

Displaying an element in a Matrix (the long way) Display by each and every individual element

o THERE IS NO WAY TO DISPLAY THE ENTIRE MATRICES IN ONE LINE!!!

o System.out.print(sodaPrice + “\n”); // that will not display the entire Matrices

Display code is the SAME for all datatypeso notice I use println() to print each ELEMENT on a separate line

THERE IS A PATTERN!!!o start from TOP left, and go right (and bottom)

[0][0] [0][1] [0][2] [0][3] [0][4] [0][5] [0][6] [0][7] [0][8] [0][9]

[1][0] [1][1] [1][2] [1][3] [1][4] [1][5] [1][6] [1][7] [1][8] [1][9]

[2][0] [2][1] [2][2] [2][3] [2][4] [2][5] [2][6] [2][7] [2][8] [2][9]

[3][0] [3][1] [3][2] [3][3] [3][4] [3][5] [3][6] [3][7] [3][8] [3][9]

Displaying values in multiple datatype MatricesDouble Integer String Char

System.out.println(studentGPAs[0][0]);System.out.println(studentGPAs[0][1]);System.out.println(studentGPAs[0][2]);System.out.println(studentGPAs[1][0]);System.out.println(studentGPAs[1][1]);System.out.println(studentGPAs[1][2]);System.out.println(studentGPAs[2][0]);System.out.println(studentGPAs[2][1]);System.out.println(studentGPAs[2][2]);

same as String [][] names = new String[25][2];// …

System.out.println(names[0][0]);System.out.println(names[0][1]);System.out.println(names[1][0]);System.out.println(names[1][1]);System.out.println(names[2][0]);…System.out.println(names[24][1]);

???

How many PrintLn statements would we need to display THE ENTIRE Matrices of 150 elements??Create the code to display ALL of floorOne (char)

4

12

3

4

Page 5: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

Introduction to the “Nested For” Loop used to display matrices, (which we will cover later)

Remember the bouncing balls!!

for(int i = 0; i < 10; i++){ System.out.print( “I will not cheat on my quiz\n”)}

inner loop will ALWAYS run faster than the outside loop

Remember the bouncing balls!!

for(int i = 0; i < 10; i++) // will cover all ROWS!!!{

for(int j = 0; j < 2; j++) // will cover all COLUMNS!!! { System.out.print( “I will not cheat on my quiz\n”) } }

Using Repetition to display Matrices To display our simple 6x6 Matrices AS A MATRIX (block), we would have to

o display each element separatelyo display a line break after each COMPLETED row

remember the pattern above, top left to right and bottom

row 1 [0][0] [0][1] [0][2] [0][3] [0][4] [0][5]row 2 [1][0] [1][1] [1][2] [1][3] [1][4] [1][5]row 3 [2][0] [2][1] [2][2] [2][3] [2][4] [2][5]row 4 [3][0] [3][1] [3][2] [3][3] [3][4] [3][5]row 5 [0][0] [0][1] [0][2] [0][3] [0][4] [0][5]row 6 [1][0] [1][1] [1][2] [1][3] [1][4] [1][5]

5

1 2

3

45

6

78 10

9

13

12

11

1 2

3 4

5

67

8

10 91

3

12

11

14…

Page 6: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

Placing and Displaying values in a Matrix we use loops since

o i++ AND j++ incrementationo can repeat code

Placing values into Matrices using LoopsNon-loop Loop

int [][] test = new int[10][2];

test[0][0] = sc.nextInt();test[0][1] = sc.nextInt();test[0][2] = sc.nextInt();test[0][3] = sc.nextInt();test[0][4] = sc.nextInt();test[0][5] = sc.nextInt();test[0][6] = sc.nextInt();test[0][7] = sc.nextInt();test[0][8] = sc.nextInt();test[0][9] = sc.nextInt();test[1][0] = sc.nextInt();… test[0][0] = sc.nextInt();

int [][] test = new int[10][2];

for(int i = 0; i < test.length; i++) // covers all ROWS{

for(int j = 0; j < test[i].length; j++) // covers all COLS{ test[i][j] = -1; }

}

for(int i = 0; i < test.length; i++){

for(int j = 0; j < test[i].length; j++) { System.out.print(test[i][j] + “ “); }

System.out.println();}

Which one of the loops above is displaying?Which one of the loops above is placing values?

using the “barney number balls”, list each step (1-14) of the loop below (should look like above)

Create a nested loop that will do BOTH place and display.

6

Page 7: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

The length function for Matrices “returns” the exact length (integer) of the Matrices when creating Matrices, there are features that come with it nice feature since you don’t have to remember!!! The computer will find out!! syntax

o Matrices_name.length for ROWo Matrices_name[0].length for COLUMN

Examples of length in use (Matrix)int minecount = 0;for(int i = 0; i < field.length; i++){

for(int j = 0; j < field[0].length; j++) // how big is the matrix??{

// all equations go here when I and J involvedif(field[i][j] = = ‘*’) { minecount++; }else { } // no mine

}}

1. Create the loop to display the “floorOne” Matrix

2. Now create a for loop to display the Matrices “d” as such: (SLIP)

|0|1|2|3|4||5|6|7|8|9||10|11|12|13|14||15|16|17|18|19|

7

Page 8: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

Multiple Nested For Loops using “i” is always convenient until you have many for loopso depends on the position on the “i” (initialize), covered below

you CAN have multiple loops use the SAME variable!!

Using the same “i” for many loops

8

Page 9: Arraysfaculty.cse.tamu.edu/slupoli/notes/Java/MatricesNotes.doc  · Web viewMatrices need a NAME and a size. name. one word, CAMELCASE. floorOne. floor_One. size (coordinates, rows

Searching through an array/matrix most simplest search through an matrix, is using the linear search

o starts from 0, and continues until it finds the target, OR ends at the end of the array

o you will learn more efficient searches later use a loop to

o iterate through the array since indices are integers and increment by 1 (i++)

o loop also checks to see if new index contains the targeto nested loop matrix

whole statements can go inside nested loops

Matrix (nested Loop) exampleint minecount = 0;for(i = 0; i < 10; i++){

for(int j = 0; j < 10; j++) // how big is the matrix??{

// all equations go here when I and J involvedif(field[i][j] = = ‘*’) { minecount++; }else { } // no mine

}} //how could I change this to work with NAY size matrix??

// 1. Create an INTEGER matrix 10 x 10 named “board”// 2. Place random values (0 to 50) in EACH element (in Arrays)// 3. Display the matrix AS a matrix

9