Friday, September 5, 2008

C# program to demonstrate jagged arrays.

using System;

class Jagged {
public static void Main() {
int[][] jagged = new int[3][];
jagged[0] = new int[4];
jagged[1] = new int[3];
jagged[2] = new int[5];

int i;

// store values in first array
for(i=0; i < 4; i++)
jagged[0][i] = i;

// store values in second array
for(i=0; i < 3; i++)
jagged[1][i] = i;

// store values in third array
for(i=0; i < 5; i++)
jagged[2][i] = i;

// display values in first array
for(i=0; i < jagged[0].Length; i++)
Console.Write(jagged[0][i] + " ");

Console.WriteLine();

// display values in second array
for(i=0; i < jagged[1].Length; i++)
Console.Write(jagged[1][i] + " ");

Console.WriteLine();

// display values in third array
for(i=0; i < jagged[2].Length; i++)
Console.Write(jagged[2][i] + " ");

Console.WriteLine();

}
}

0 comments: