Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle.

Similar presentations


Presentation on theme: "Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle."— Presentation transcript:

1 Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle

2 2 1-Dimensional vs. 2-Dimensional Arrays Recall how we declared and created a 1-D array: Recall how we declared and created a 1-D array: int values[] = new int[5]; int values[] = new int[5]; We declare and create a 2-D array in much the same way: We declare and create a 2-D array in much the same way: int table[][] = new int[10][6]; int table[][] = new int[10][6]; The above Java statement creates a 2-D integer array with 10 rows and 6 columns, essentially a 10 x 6 matrix The above Java statement creates a 2-D integer array with 10 rows and 6 columns, essentially a 10 x 6 matrix Note: The above 2-D array code can also be written: int [][] table = new int [10][6]; Note: The above 2-D array code can also be written: int [][] table = new int [10][6];

3 3 1-Dimensional Array Recall how we initialized a 1-D array: Recall how we initialized a 1-D array: Initial values known Initial values known values[0] = 25; values[1] = 11; values[2] = 38; values[3] = 3; values[4] = 17; values[0] = 25; values[1] = 11; values[2] = 38; values[3] = 3; values[4] = 17; int values[] = {25, 11, 38, 3, 17}; int values[] = {25, 11, 38, 3, 17};

4 4 1-Dimensional Array Recall how we initialized a 1-D array: Recall how we initialized a 1-D array: Initial values not known Initial values not known Initialize element contents to 0 to “play it safe” Initialize element contents to 0 to “play it safe” int values[] = new int[5]; for (int i = 0; i < values.length; i++) values[i] = 0; int values[] = new int[5]; for (int i = 0; i < values.length; i++) values[i] = 0; We’ll assign real values to the array later. We’ll assign real values to the array later. The array “knows” how big it is.

5 5 2-Dimensional Array In Lab 6, we created 2-dimensional figures (triangle, arrow) using nested loops. In Lab 6, we created 2-dimensional figures (triangle, arrow) using nested loops. Similarly, we generally initialize 2- dimensional arrays using nested loops. Similarly, we generally initialize 2- dimensional arrays using nested loops. Code to initialize the 2-D array table to all 0s appears in the next slide. Code to initialize the 2-D array table to all 0s appears in the next slide.

6 6 2-Dimensional Array Typical initialization routine Typical initialization routine Initialize array elements to 0 Initialize array elements to 0 int[][] table = new int[10][6]; int row, column; for (row = 0; row < 10; row++) for (column = 0; column < 6; column++) table[row][column] = 0; int[][] table = new int[10][6]; int row, column; for (row = 0; row < 10; row++) for (column = 0; column < 6; column++) table[row][column] = 0;

7 7 2-Dimensional Array Alternate initialization routine Alternate initialization routine Initialize array elements to values specified by the programmer (as in the Tictactoe program in Lab 11 which employs a 3 x 3 character array) Initialize array elements to values specified by the programmer (as in the Tictactoe program in Lab 11 which employs a 3 x 3 character array) char [][] tictac = { {'X', 'X', 'O'}, {' ', 'O', ' '}, {'X', 'O', 'X'} }; char [][] tictac = { {'X', 'X', 'O'}, {' ', 'O', ' '}, {'X', 'O', 'X'} };

8 8 Sample Program The following program uses 2-D arrays to implement a table showing how interest rates affect bank balances: The following program uses 2-D arrays to implement a table showing how interest rates affect bank balances: InterestTable.java InterestTable.java InterestTable.java

9 9 Histograms Since Lab 11 employs histograms, I thought it wise to briefly discuss them in case any one of you is not familiar with them. Since Lab 11 employs histograms, I thought it wise to briefly discuss them in case any one of you is not familiar with them. According to the Wikipedia*: According to the Wikipedia*: “In statistics, a histogram is a graphical display of tabulated frequencies. A histogram is the graphical version of a table that shows what proportion of cases fall into each of several or many specified categories.” *This is the definition of a histogram most relevant for Lab 11; this and other definitions can be viewed at: http://en.wikipedia.org/wiki/Histogram “In statistics, a histogram is a graphical display of tabulated frequencies. A histogram is the graphical version of a table that shows what proportion of cases fall into each of several or many specified categories.” *This is the definition of a histogram most relevant for Lab 11; this and other definitions can be viewed at: http://en.wikipedia.org/wiki/Histogramstatisticsgraphical displayfrequenciescategories http://en.wikipedia.org/wiki/Histogramstatisticsgraphical displayfrequenciescategories http://en.wikipedia.org/wiki/Histogram

10 10 Die Rolling Simulation A program that simulates the rolls of a die is suitable for graphically displaying frequencies, such as the number of times a 1 is rolled, the number of times a 2 is rolled, etc. A program that simulates the rolls of a die is suitable for graphically displaying frequencies, such as the number of times a 1 is rolled, the number of times a 2 is rolled, etc. A sample program that does just that is: DieRollingSimulation.java A sample program that does just that is: DieRollingSimulation.java DieRollingSimulation.java

11 11 Lab 11 Now let’s look at how the preceding discussion relates to today’s lab. Now let’s look at how the preceding discussion relates to today’s lab.


Download ppt "Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle."

Similar presentations


Ads by Google