Mastering C# Fundamentals: Multidimensional Arrays

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 M…


This content originally appeared on DEV Community and was authored by mohamed Tayel

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 inner for 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:

    1. We create a two-dimensional array named salesData with 3 rows (products) and 4 columns (quarters).
    2. We use nested for loops to iterate through all products and quarters, displaying the sales data for each.
  • 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.


This content originally appeared on DEV Community and was authored by mohamed Tayel


Print Share Comment Cite Upload Translate Updates
APA

mohamed Tayel | Sciencx (2024-09-30T21:08:33+00:00) Mastering C# Fundamentals: Multidimensional Arrays. Retrieved from https://www.scien.cx/2024/09/30/mastering-c-fundamentals-multidimensional-arrays/

MLA
" » Mastering C# Fundamentals: Multidimensional Arrays." mohamed Tayel | Sciencx - Monday September 30, 2024, https://www.scien.cx/2024/09/30/mastering-c-fundamentals-multidimensional-arrays/
HARVARD
mohamed Tayel | Sciencx Monday September 30, 2024 » Mastering C# Fundamentals: Multidimensional Arrays., viewed ,<https://www.scien.cx/2024/09/30/mastering-c-fundamentals-multidimensional-arrays/>
VANCOUVER
mohamed Tayel | Sciencx - » Mastering C# Fundamentals: Multidimensional Arrays. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/30/mastering-c-fundamentals-multidimensional-arrays/
CHICAGO
" » Mastering C# Fundamentals: Multidimensional Arrays." mohamed Tayel | Sciencx - Accessed . https://www.scien.cx/2024/09/30/mastering-c-fundamentals-multidimensional-arrays/
IEEE
" » Mastering C# Fundamentals: Multidimensional Arrays." mohamed Tayel | Sciencx [Online]. Available: https://www.scien.cx/2024/09/30/mastering-c-fundamentals-multidimensional-arrays/. [Accessed: ]
rf:citation
» Mastering C# Fundamentals: Multidimensional Arrays | mohamed Tayel | Sciencx | https://www.scien.cx/2024/09/30/mastering-c-fundamentals-multidimensional-arrays/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.