Two-dimensional Arrays Array: a numbered list of data items all of the same type. An array is an example of a data structure. To find the appropriate element in the data structure, we need ONE piece of information: the subscript. 2-D arrays Sometimes we need more than one array in a program. For example, if I were keeping track of the test scores for all of the students in this class, I would need three lists. Each row represents a student. Each column represents a test. The first number in each pair below represents the number of the student, and the second number represents the number of the test. [0, 0] [0, 1] [0, 2] [1, 0] [1, 1] [1, 2] [2, 0] [2, 1] [2, 2] [3, 0] [3, 1] [3, 2] [4, 0] [4, 1] [4, 2] [5, 0] [5, 1] [5, 2] [6, 0] [6, 1] [6, 2] [7, 0] [7, 1] [7, 2] One way to look at this data is that it is three lists. Another way to look at it is that it is one table. However, if we have a table, we can no longer find what we are looking for simply by using a single subscript. Now we need two pieces of information: a ROW and a COLUMN. C# provides us with a way to do this in our programs by allowing two-dimensional arrays. A 2-dimensional array has both rows and columns, but it has no visual representation—it is just stored in memory. 3/22/2016 Document1 Page 1 of 6 To declare a 2-dimensional array <type> [,] <arrayname>; Example: Create a new console program called Day10--2DArrays. The following creates an array of integers called scores with rows numbered from 0 to 19 and columns numbered from 0 to 2. const int maxStudents = 20; const int maxTests = 3; // In Main: int[,] score; score = new int[maxStudents, maxTests]; InitializeScores(score, maxStudents, maxTests); LookupScore(score, maxStudents, maxTests); Initialize the array with some dummy data: static void InitializeScores(int[,] score, int students, int tests) { for (int r = 0; r < students; r++) for (int c = 0; c < tests; c++) score[r, c] = (r + 1) * 10 + c + 1; } 3/22/2016 Document1 Page 2 of 6 To process a 2-dimensional array With a 2-dimensional array, we can process the data in several ways: (1) (2) (3) (4) Look up one element Process a row Process a column Process the entire array Looking up a single element in a 2D array static void LookupScore(int[,] score, int students, int tests) { int r, c; Console.Write("Enter student number (0-" + (students-1) + "): "); r = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter test number (0-" + (tests-1) + "): "); c = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(score[r, c]); Console.ReadLine(); } 3/22/2016 Document1 Page 3 of 6 Process a row in a 2D array Assume you want to process row 3. The following picture represents the cells that must be visited to process row 3. 0 1 2 0 1 2 3 4 5 6 7 8 9 The cells to visit are: (3, 0), (3, 1), (3, 2). Note that the row number remains constant while the column number goes from 0 to 2 (the last column). This can be processed by a for loop that sets the row number to 3 and takes the column number from 0 to 2. static void DisplayStudent(int[,] score, int students, int tests) { int student; Console.Write("Which student (0-" + (students - 1) + ")? "); student = Convert.ToInt32(Console.ReadLine()); for (int c = 0; c < tests; c++) Console.WriteLine(score[student, c]); Console.Write("Press Enter to continue..."); Console.ReadLine(); } 3/22/2016 Document1 Page 4 of 6 Process a column in a 2D array Assume you want to process column 2. The following picture represents the cells that must be visited to process column 2. 0 1 2 0 1 2 3 4 5 6 7 8 9 The cells to visit are (0, 2), (1, 2), (2, 2), … (9, 2). Note that the column number remains constant while the row number goes from 0 to 9 (the last row). This can be processed by a for loop that sets the column number to 2 and takes the row number from 0 to 9. static void DisplayTest(int[,] score, int students, int tests) { int test; Console.Write("Which test (0-" + (tests - 1) + ")? "); test = Convert.ToInt32(Console.ReadLine()); for (int r = 0; r < students; r++) Console.WriteLine(score[r, test]); Console.Write("Press Enter to continue..."); Console.ReadLine(); } 3/22/2016 Document1 Page 5 of 6 Process ALL the rows and columns in a 2D array To process all of the elements in a 2D array, you want to do the following: process row 0, process row 1, process row 2, … process row 9. The indexes are: 0 1 2 0 1 2 3 4 5 6 7 8 9 So the main algorithm looks something like this: for (int row = 0; row < students; row++) process row And, to process a row (see the algorithm above): for (int col = 0; col < tests; col++) process item at column col in this row So the algorithm to process all of the items in a 2D array looks like this: for (int row = 0; row < students; row++) for (int col = 0; col < tests; col++) process item at position (row, col) The code looks like this static void ShowAllStudentsAndTests(int[,] score, int students, int tests) { for (int r = 0; r < students; r++) { for (int c = 0; c < tests; c++) { Console.Write(score[r, c].ToString().PadLeft(5)); } Console.WriteLine(); } Console.Write("Press Enter to continue..."); Console.ReadLine(); } 3/22/2016 Document1 Page 6 of 6