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
    • 210
    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: 

    1. 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.

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

    refer screenshot for an example:

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

    1. using System;
    2. namespace TwodimensionalArrayApplication
    3. {
    4. class MytwoDimensionalArray
    5. {
    6. static void Main(string[] args)
    7. {
    8. /* an array with 4 rows and 2 columns*/
    9. int[,] a = new int[4, 2]
    10.         { {0,0},
    11.         {1,2},
    12.         {3,4},
    13.         {5,6} };
    14. int i, j;
    15. /* output each array element's value */
    16. for (i = 0; i < 4; i++)
    17. {
    18. for (j = 0; j < 2; j++)
    19. {
    20. Console.WriteLine("a[{0},{1}] = {2}", i, j, a[i,j]);
    21. }
    22. }
    23. Console.ReadKey();
    24. }
    25. }
    26. }

    when program will be executed,output will be:

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

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: