Tuesday 6 March 2012

Processing jagged Array


// Read and Write jagged Array

using System;
class Program
{
static void Main()
{
int[][] jagArray=new int[3][];

//Memory Allocation
jagArray[0]=new int[5];
jagArray[1]=new int[3];
jagArray[2]=new int[1];

// Reading elements
for(int i=0;i<jagArray.Length;i++)
{
for(int j=0;j<jagArray[i].Length;j++)
{
Console.Write("Enter Element: ");
jagArray[i][j]=int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
// Printing Array elements
for(int i=0;i<jagArray.Length;i++)
{
for(int j=0;j<jagArray[i].Length;j++)
{
Console.Write("{0} ", jagArray[i][j]);
}
Console.WriteLine();
}
}
}

No comments:

Post a Comment