Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Two dimensional array in c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 163
    Comment on it

    Two dimensional array is a form of matrix in which users can have rows and columns as per need. We can think of table in which x no.of rows and y no.of columns are available.

    Syntax: 

    int item = a[i,j]

    This is a two dimensional array which contains i no. of rows and j no. of columns, Here a represents the array and i and j are also used to define each array of element,it is used as a subscript.

    Let's take an example to initialize a two dimensional array:

    This array is with 3 rows and each row has 4 columns.

    int [,] a = new int [3,4] {
    col[0][1][2][3]
       {0, 1, 2, 3} ,   /*   row  [0] */
       {4, 5, 6, 7} ,   /*   row  [1] */
       {8, 9, 10, 11}   /*   row  [2] */
    };

    refer screenshot for an example:

    Let's Create a program to handle the 2-dimensional array:

    using System;
    namespace TwodimensionalArrayApplication
    {
       class MytwoDimensionalArray
       {
          static void Main(string[] args)
          {
             /* an array with 4 rows and 2 columns*/
             int[,] a = new int[4, 2] 
     		{ {0,0}, 
    		{1,2}, 
    		{3,4},
     		{5,6} };
             int i, j;
             
             /* output each array element's value */
             for (i = 0; i < 4; i++)
             {
                for (j = 0; j < 2; j++)
                {
                   Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
                }
             }
             Console.ReadKey();
          }
       }
    }

    when program will be executed,output will be:

    a[0,0]: 0
    a[0,1]: 0
    a[1,0]: 1
    a[1,1]: 2
    a[2,0]: 3
    a[2,1]: 4
    a[3,0]: 5
    a[3,1]: 6

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: