Tuesday 6 March 2012

Processing Two Dimensional Array


// Read and write 2d Array Dynamically.

using System;
class Program
{
static void Main()
{
int[,] arr;
int rows,cols;

//enter rows and columns of array.
Console.Write("Enter No of Rows Of 2D Array: ");
rows=int.Parse(Console.ReadLine());

Console.Write("Enter No of Columns Of 2D Array: ");
cols=int.Parse(Console.ReadLine());

//Memory Allocation
arr=new int[rows,cols];
Console.WriteLine();

//Reading Array Elements Dynamically
for(int i=0;i<arr.GetLength(0);i++)
{
for(int j=0;j<arr.GetLength(1);j++)
{
Console.Write("Enter Array element: ");
arr[i,j]=int.Parse(Console.ReadLine());
}
}
Console.WriteLine();

//Printing Array Elements Dynamically
for(int i=0;i<arr.GetLength(0);i++)
{
for(int j=0;j<arr.GetLength(1);j++)
{
Console.Write("{0} ", arr[i,j]);
}
Console.WriteLine();
}
}
}

No comments:

Post a Comment