Multi-dimensional arrays help you store data in a table or grid form, like rows and columns. This makes it easy to organize and access related data together.
Multi-dimensional arrays in C Sharp (C#)
class Program { static void Main() { // Declare a 2D array with 3 rows and 4 columns int[,] matrix = new int[3, 4]; // Declare and initialize a 2D array int[,] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Access element at row 1, column 2 int value = grid[1, 2]; } }
Use commas inside the square brackets to separate dimensions, e.g., [rows, columns].
Multi-dimensional arrays in C# are rectangular, meaning each row has the same number of columns.
int[,] emptyArray = new int[0, 0];
int[,] singleElementArray = new int[1, 1] { { 42 } };
int[,] edgeArray = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; int firstElement = edgeArray[0, 0]; int lastElement = edgeArray[1, 2];
This program creates a 3x3 grid representing a Tic Tac Toe board. It prints the board, updates the center cell, and prints the board again to show the change.
using System; class Program { static void Main() { // Create a 3x3 multi-dimensional array int[,] ticTacToeBoard = new int[3, 3] { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 0, 1 } }; Console.WriteLine("Tic Tac Toe Board Before Update:"); PrintBoard(ticTacToeBoard); // Update the center cell (row 1, column 1) to 1 ticTacToeBoard[1, 1] = 1; Console.WriteLine("\nTic Tac Toe Board After Update:"); PrintBoard(ticTacToeBoard); } static void PrintBoard(int[,] board) { int rows = board.GetLength(0); int columns = board.GetLength(1); for (int row = 0; row < rows; row++) { for (int column = 0; column < columns; column++) { Console.Write(board[row, column] + " "); } Console.WriteLine(); } } }
Accessing an element in a multi-dimensional array takes constant time O(1).
Multi-dimensional arrays use contiguous memory, so they are efficient for grid-like data.
Common mistake: Mixing up row and column indexes. Remember the first index is the row, the second is the column.
Use multi-dimensional arrays when you need a fixed-size grid. For jagged or uneven rows, consider jagged arrays instead.
Multi-dimensional arrays store data in rows and columns, like a table.
They are useful for grids, boards, and matrices.
Access elements using two indexes: [row, column].
Practice
Solution
Step 1: Understand array dimensions
A multi-dimensional array stores data in multiple dimensions, such as rows and columns, unlike a simple one-dimensional array.Step 2: Recognize the definition
An array with more than one dimension, like rows and columns correctly describes a multi-dimensional array as having more than one dimension, like a table with rows and columns.Final Answer:
An array with more than one dimension, like rows and columns -> Option CQuick Check:
Multi-dimensional array = rows and columns [OK]
- Confusing multi-dimensional arrays with simple lists
- Thinking arrays can only hold one type of data
- Believing multi-dimensional arrays are unrelated variables
Solution
Step 1: Recall C# multi-dimensional array syntax
In C#, a 2D array is declared with commas inside square brackets: int[,] arr = new int[rows, columns];Step 2: Check each option
int[,] arr = new int[2,3]; uses correct syntax. int[][] arr = new int[2][3]; is jagged array syntax, not multi-dimensional. Options A and C are invalid syntax.Final Answer:
int[,] arr = new int[2,3]; -> Option AQuick Check:
Use comma inside brackets for multi-dimensional arrays [OK]
- Using jagged array syntax instead of multi-dimensional
- Missing 'new' keyword or wrong brackets
- Confusing array declaration with other languages
int[,] matrix = { {1, 2, 3}, {4, 5, 6} };
Console.WriteLine(matrix[1, 2]);Solution
Step 1: Understand array indexing
matrix[1, 2] means row 1, column 2. Rows and columns start at 0.Step 2: Locate the value at matrix[1, 2]
Row 1 is {4, 5, 6}, column 2 is the third element, which is 6.Final Answer:
6 -> Option DQuick Check:
matrix[1,2] = 6 [OK]
- Confusing row and column indexes
- Starting count from 1 instead of 0
- Expecting the value 3 or 5 instead of 6
int[,] grid = new int[3, 3]; grid[3, 0] = 10;
Solution
Step 1: Check array size and valid indexes
The array grid has size 3x3, so valid row indexes are 0, 1, 2.Step 2: Analyze the assignment grid[3, 0]
Index 3 for row is out of range, causing an IndexOutOfRangeException at runtime.Final Answer:
Index out of range error at grid[3, 0] -> Option AQuick Check:
Index 3 invalid for size 3 array [OK]
- Using index equal to array size
- Confusing syntax error with runtime error
- Assuming arrays auto-expand on invalid index
int[,] board representing a tic-tac-toe game, which code snippet correctly counts how many cells contain the value 1?int count = 0;
// Which loop is correct?
A) for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (board[i, j] == 1) count++;
B) for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
if (board[i, j] == 1) count++;
C) for (int i = 0; i < 3; i++)
for (int j = 0; j < 2; j++)
if (board[i, j] == 1) count++;
D) for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (board[j, i] == 1) count++;Solution
Step 1: Understand array indexing and loop ranges
Indexes start at 0 and go up to size-1, so loops must run from 0 to less than 3.Step 2: Analyze each option's loops
Causes index out of range error correctly counts all cells with value 1 loops correctly over all rows and columns. Causes index out of range error causes index out of range error uses 1 to 3, causing out of range. Misses last column in counting misses last column by looping j to 2. Counts transposed cells, not original positions swaps indexes, counting transposed cells.Final Answer:
Causes index out of range error -> Option BQuick Check:
Loop from 0 to less than size for rows and columns [OK]
- Starting loops at 1 instead of 0
- Using wrong loop limits causing missed elements
- Swapping row and column indexes accidentally
Ready to prove it?
Pass the test and this pattern moves unknown
