Arrays
Matrices and Nested For Loops
Matrices
0 1 2 3 4 5 6 7 8 9 10 11 …
y 0 …
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) |
| |
| |
| |
|x (row) |
| | | |
| |C == [2][4] | |
| | | |
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 Computer
o called garbage!!!
o good to set value (below) to your choice of a default
type [][]matrixname = new type[rows][columns];
|Declaration of Multiple datatype Matrices |
|Double |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 like |
How 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 Matrices |
|Double |Integer |String |Char |
|double studentGPAs[0][0] = 3.65; |same as ( |String [][] names = new String[25][2]; |create the floor with ALL blanks (‘ ‘) |
|// incorrect code | | | |
| | |names[0][0]= “Lupoli”; | |
|studentGPAs[0][0] = 3.65; | |names[0][1]= “Mr.”; | |
|studentGPAs[0][1] = 1.23; | |names[1][0]= “Grove”; | |
|studentGPAs[0][2] = 4.00; | |names[1][1]= “Eric”; | |
|studentGPAs[1][0] = 2.65; | |names[2][0]= “Reardon”; | |
|studentGPAs[1][1] = 1.79; | |names[2][1]= “Greg”; | |
|studentGPAs[1][2] = 2.76; | |names[3][0]= “Myers”; | |
|studentGPAs[2][0] = 2.65; | |names[3][1]= “Will”; | |
|studentGPAs[2][1] = 3.20; | |… | |
|studentGPAs[2][2] = 2.78; | | | |
| | |names[1][0] = “Munson”; | |
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?
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 datatypes
o 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)
| |
|Double |Integer |String |Char |
| |same as ( |String [][] names = new String[25][2]; |??? |
|System.out.println(studentGPAs[0][0]); | |// … | |
|System.out.println(studentGPAs[0][1]); | | | |
|System.out.println(studentGPAs[0][2]); | |System.out.println(names[0][0]); | |
|System.out.println(studentGPAs[1][0]); | |System.out.println(names[0][1]); | |
|System.out.println(studentGPAs[1][1]); | |System.out.println(names[1][0]); | |
|System.out.println(studentGPAs[1][2]); | |System.out.println(names[1][1]); | |
|System.out.println(studentGPAs[2][0]); | |System.out.println(names[2][0]); | |
|System.out.println(studentGPAs[2][1]); | |… | |
|System.out.println(studentGPAs[2][2]); | |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)
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 separately
o 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] |
Placing and Displaying values in a Matrix
• we use loops since
o i++ AND j++ incrementation
o can repeat code
|Placing values into Matrices using Loops |
|Non-loop |Loop |
| | |
|int [][] test = new int[10][2]; |int [][] test = new int[10][2]; |
| | |
|test[0][0] = sc.nextInt(); |for(int i = 0; i < test.length; i++) // covers all ROWS |
|test[0][1] = sc.nextInt(); |{ |
|test[0][2] = sc.nextInt(); |for(int j = 0; j < test[i].length; j++) // covers all COLS |
|test[0][3] = sc.nextInt(); |{ test[i][j] = -1; } |
|test[0][4] = sc.nextInt(); |} |
|test[0][5] = sc.nextInt(); | |
|test[0][6] = sc.nextInt(); |for(int i = 0; i < test.length; i++) |
|test[0][7] = sc.nextInt(); |{ |
|test[0][8] = sc.nextInt(); |for(int j = 0; j < test[i].length; j++) |
|test[0][9] = sc.nextInt(); |{ System.out.print(test[i][j] + “ “); } |
|test[1][0] = sc.nextInt(); |System.out.println(); |
|… |} |
|test[0][0] = sc.nextInt(); | |
| |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. |
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 ROW
o 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 involved |
|if(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|
Multiple Nested For Loops
• using “i” is always convenient until you have many for loops
o 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 |
|[pic] |
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 target
o nested loop ( matrix
▪ whole statements can go inside nested loops
|Matrix (nested Loop) example |
|int 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 involved |
|if(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
-----------------------
1
2
3
4
11
13
10
8
7
5
4
2
1
12
9
6
3
12
11
2
1
14…
13
10
9
7
6
4
3
8
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- raptor rapid application programming tool for ordered
- operations maintenance manual o m manual template
- intermediate programming instructor greg shaw
- competency examples with performance statements
- c elementary programming
- csci 515 c c programming fall 2001
- represent and solve problems involving multiplication and
- an efficient double array establishing algorithm based on