A multidimensional array in C# is an array that contains more than one index, often used to represent data in rows and columns, like a table. The most common form is the two-dimensional array, which can be visualized as a matrix.
Creating a Multidimensional Array
A two-dimensional array can be created like this:
int[,] matrix = new int[3, 3];
-
Explanation: This creates a 3x3 matrix (a table with 3 rows and 3 columns). Here,
[,]
indicates that this is a two-dimensional array, and[3, 3]
sets the number of rows and columns.
You can also initialize the array with values:
int[,] matrix = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
-
Explanation: This creates a 3x3 matrix with predefined values. The array is populated with rows
{1, 2, 3}
,{4, 5, 6}
, and{7, 8, 9}
.
Accessing Elements in a Multidimensional Array
To access or modify elements in a multidimensional array, you use both indices:
int value = matrix[1, 2]; // Accessing the element in row 2, column 3 (index starts from 0)
matrix[2, 0] = 10; // Setting the value at row 3, column 1 to 10
- Explanation: In a two-dimensional array, you access elements by specifying both the row and the column index.
Looping Through a Multidimensional Array
To loop through all elements of a multidimensional array, you can use nested for
loops—one loop for rows and one for columns.
Example: Looping through a 3x3 matrix:
int[,] matrix = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
Console.WriteLine("Matrix Elements:");
for (int i = 0; i < matrix.GetLength(0); i++) // Loop for rows
{
for (int j = 0; j < matrix.GetLength(1); j++) // Loop for columns
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine(); // New line after each row
}
-
Explanation:
-
matrix.GetLength(0)
returns the number of rows. -
matrix.GetLength(1)
returns the number of columns. - The outer
for
loop iterates over the rows, while the innerfor
loop iterates over the columns.
-
Output:
Matrix Elements:
1 2 3
4 5 6
7 8 9
Example: Real-World Scenario
Consider an example where we have a table representing monthly sales data for 3 products over 4 quarters:
int[,] salesData = new int[3, 4]
{
{ 100, 200, 150, 175 }, // Product 1 sales for 4 quarters
{ 80, 90, 120, 130 }, // Product 2 sales for 4 quarters
{ 200, 300, 250, 400 } // Product 3 sales for 4 quarters
};
// Displaying sales data
Console.WriteLine("Sales Data (Products x Quarters):");
for (int product = 0; product < salesData.GetLength(0); product++) // Loop for products
{
Console.Write($"Product {product + 1}: ");
for (int quarter = 0; quarter < salesData.GetLength(1); quarter++) // Loop for quarters
{
Console.Write(salesData[product, quarter] + " ");
}
Console.WriteLine(); // New line after each product
}
-
Explanation:
- We create a two-dimensional array named
salesData
with 3 rows (products) and 4 columns (quarters). - We use nested
for
loops to iterate through all products and quarters, displaying the sales data for each.
- We create a two-dimensional array named
Output:
Sales Data (Products x Quarters):
Product 1: 100 200 150 175
Product 2: 80 90 120 130
Product 3: 200 300 250 400
Conclusion
Multidimensional arrays in C# are useful for storing and working with data that is naturally organized into rows and columns. By using nested loops, you can easily access and manipulate the elements. This makes them suitable for scenarios like matrices, grids, or tables of data. Practice these examples to understand how to create, populate, and iterate through multidimensional arrays effectively.
Top comments (0)