Jagged Array Program in C#
Posted by Superadmin on August 15 2022 14:03:23

 

Jagged Array Program in C#

 

This is a C# Program to demonstrate jagged arrays.

Problem Description

This C# Program Demonstrates Jagged Arrays.

Problem Solution

Here Jagged Arrays can store efficiently many rows of varying lengths. Any type of data, reference or value, can be used.

Program/Source Code

Here is source code of the C# Program to Demonstrate Jagged Arrays. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Demonstrate Jagged Arrays
 */
using System;
class Program
{
    static void Main()
    {
        int[][] jag = new int[3][];
        jag[0] = new int[2];
        jag[0][0] = 11;
        jag[0][1] = 12;
        jag[1] = new int[1] {11};
        jag[2] = new int[3] { 14,15, 16 };
        for (int i = 0; i < jag.Length; i++)
        {
            int[] innerArray = jag[i];
            for (int a = 0; a < innerArray.Length; a++)
            {
                Console.WriteLine(innerArray[a] + " ");
            }
        }
        Console.Read();
    }
}
Program Explanation

This C# Program is used to Demonstrate Jagged Arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an “array of arrays.” Create a new array in the jagged array, and assign its coefficient values, and form second row and third row by using array initialize.

Here Jagged Arrays can store efficiently many rows of varying lengths. Any type of data, reference or value, can be used. For loop is used to print the jagged array element values.
Runtime Test Cases
 
11
12
11
14
15
16

 


Jagged Array Program in C#