In c programming language jagged array is very important role play in part of the array. The array can be different sizes.it is producing rows of jagged edges.
when visualized as output.a jagged array is an array of array. The difference between a rectangular array and jagged array is rectangular array is one object and
two dimensions or each element is integers but in case of a jagged array, all elements are the different length and different size.
create the jagged array in C using pointers
int *const jagged[] = {
(int []) { 0, 1 },
(int []) { 1, 2, 3 }
};
We are create small example of jagged array-:
we want to always size of array sizes :
int jagged[][3] = { {2,4}, {1,2,3} };
int size = sizeof(jagged);
printf("size=%d\n", size);
int* p = (void*)jagged;
for (int i=0;i<(size/(int)sizeof(int));i++)
{
printf("%d=%d\n", i, *p++);
}
output:
size=24
0=2
1=4
2=0
3=1
4=2
5=3
----------------------------------------------Example of c#---------------------------------------------
We are also creating a jagged array program in c#.let's see the example of the jagged array.
class ArrayTest
{
static void Main()
{
// Declare the array of two elements:
int[][] arr = new int[2][];
// Initialize the elements:
arr[0] = new int[5] { 1, 3, 5, 7, 9 };
arr[1] = new int[4] { 2, 4, 6, 8 };
// Display the array elements:
for (int i = 0; i < arr.Length; i++)
{
System.Console.Write("Element({0}): ", i);
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}
System.Console.WriteLine();
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
Output:
Element(0): 1 3 5 7 9
Element(1): 2 4 6 8
0 comments:
Post a comment