C# Multidimensional / Two-Dimensional Array with Examples

C# Multidimensional / Two-Dimensional Array: In this tutorial, we will learn about the two-dimensional array, how to declare, initialize, access elements, and examples of the two-dimensional array. By IncludeHelp Last updated : April 11, 2023

Before reading ahead, I would recommend to read: C# Single-dimensional arrays.

In Single Dimensional Array, we were able to store elements in a single dimension (Array elements were store contiguous).

If we need to store data in a tabular like format, we cannot do this using single (one) dimensional array.

Two-dimensional Array in C#

A two-dimensional is useful when you want to store data in two dimensions array. It includes single-dimensional arrays as its elements. A two-dimensional array represents the data as a table with rows and columns. Here, the first dimension specifies the number of rows and the second specifies the number of columns.

Two-dimensional Array Declaration

The syntax is to declare a two-dimensional array is:

<data_type>[ , ] variable_name = new <data_type>[ROW, COL];

The following code shows the declaration of a two-dimensional array with 2 rows and 3 columns,

int[, ] arr = new int[2, 3];

Here, new operator is used to allocate memory space for the array. Total number of rows will be 2 and columns 3. Thus, space for 3*2 = 6 integer elements will be created.

Two-dimensional Array Initialization

The following code shows the initialization of a two-dimensional array,

int[ , ] arr = { { 10, 20, 30}, { 70, 80, 90 } };

The number of rows and columns can also be specified during the initialization. For example,

int[ , ] arr = new int[2, 3]{ { 10, 20, 30}, { 70, 80, 90 } };

Example 1: C# program to declare, input, and print a two-dimensional array

using System;

namespace arrayEx {
  class Program {
    static void Main(string[] args) {
      int i = 0;
      int j = 0;
      int[, ] arr;
      
      // Declaration
      arr = new int[2, 3];
      // Input array elements
      Console.Write("Enter Elements : \n");
      for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
          Console.Write("\tElement[" + i + "," + j + "]: ");
          arr[i, j] = Convert.ToInt32(Console.ReadLine());
        }
      }

      // Print array Elements
      Console.Write("\n\nElements are: \n");
      for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
          Console.Write(arr[i, j] + " ");
        }
        Console.WriteLine();
      }

    }
  }
}

Output

Enter Elements :      
        Element[0,0]: 10
        Element[0,1]: 20
        Element[0,2]: 30
        Element[1,0]: 40
        Element[1,1]: 50
        Element[1,2]: 60
 
 
Elements are:         
10 20 30              
40 50 60
Press any key to continue . . .

Example 2: C# program to find sum of each row

using System;

class MatrixDemo {
  public static void Main(string[] args) {
    int i = 0;
    int j = 0;

    int sum = 0;
    int row = 2;
    int col = 2;

    int[, ] Matrix = new int[row, col];

    Console.Write("Enter the elements of matrix: ");
    for (i = 0; i < row; i++) {
      for (j = 0; j < col; j++) {
        Matrix[i, j] = int.Parse(Console.ReadLine());
      }
    }

    Console.WriteLine("\nMatrix: ");
    for (i = 0; i < row; i++) {
      for (j = 0; j < col; j++) {
        Console.Write(Matrix[i, j] + "\t");
      }
      Console.WriteLine();
    }

    for (i = 0; i < row; i++) {
      sum = 0;
      for (j = 0; j < col; j++) {
        sum += Matrix[i, j];
      }
      Console.WriteLine("Sum of row[{0}]: {1}", (i + 1), sum);
    }
  }
}

Output

Enter the elements of matrix: 1
2
3
4

Matrix:
1       2
3       4
Sum of row[1]: 3
Sum of row[2]: 7
Press any key to continue . . .



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.