Sunday 20 April 2014

Jagged array.


Accept and print the value of jagged array.

using System;

namespace JaggedArray
{
    class Program
    {
        static void Main(string[] args)
        {         
            int[][] jagged = new int[3][];

            Console.WriteLine("Enter value for 1st row:");
            jagged[0] = new int[3];
            for (int i = 0; i < 1; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    jagged[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.WriteLine("Enter value for 2nd row:");
            jagged[1] = new int[2];
            for (int i = 1; i < 2; i++)
            {
                for (int j = 0; j <2; j++)
                {
                    jagged[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.WriteLine("Enter value for 3rd row;");
            jagged[2] = new int[4];
            for (int i = 2; i < 3; i++)
            {
                for (int j = 0; j<4; j++)
                {
                    jagged[i][j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            // Print out all elements in the jagged array.
            Console.WriteLine("\n Jagged array:");
            for (int i = 0; i <3; i++)
            {
                int[] newArray = jagged[i];
                for (int j = 0; j < newArray.Length; j++)
                {
                    Console.Write(newArray[j] + " ");   
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

Output:- 

Enter value for 1st row:
0
9
3
Enter value for 2nd row:
8
4
Enter value for 3rd row:
1
5
3
6

Jagged array:
0  9  3
8  4
1  5  3  6

If anyone have any doubt about the program , please comment and you can also mail me on "ibsarmschauhan13@gmail.com" and "makansingh42@yahoo.com".

Thank you so much :)


No comments:

Post a Comment