almost 9 years ago
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:
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.
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();
}
}
}
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:
0 Comment(s)