C# Real-time example of jagged array

Program


using System;

namespace ClassRoomSamples
{
    class Demo08JaggedArray2
    {
        static void Main(string[] args)
        {
            int cand, elect;

            Console.WriteLine("Enter how many candidates (max 10)");
            int CandCount = int.Parse(Console.ReadLine());

            //A jagged array of n rows specified by CandCount
            int[][] ElectiveMarks = new int[CandCount][]; 


            for (cand=0;cand<CandCount;cand++)
            {
                Console.WriteLine("How many electives for candidate                                                                   {0}",cand+1);
                int electiveCount = int.Parse(Console.ReadLine());
                ElectiveMarks[cand] = new int[electiveCount];

                for (elect =0;elect<electiveCount;elect++)
                {
                    Console.WriteLine("Enter elective {0} mark", elect+1);
                    ElectiveMarks[cand][elect] =                                                                    int.Parse(Console.ReadLine());
                }
            }

            // Output of each array element value
            for (cand = 0; cand < CandCount; cand++) 
            {
                Console.WriteLine("Elective marks of candidate                                                                      {0}",cand+1);
                for(elect= 0;elect<ElectiveMarks[cand].Length;elect++)
                {
                    Console.Write("{0}\t",ElectiveMarks[cand][elect]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}



Output

Enter how many candidates (max 10)
3

How many electives for candidate 1
3
Enter elective 1 mark
96
Enter elective 2 mark
85
Enter elective 3 mark
79
How many electives for candidate 2
2
Enter elective 1 mark
90
Enter elective 2 mark
60
How many electives for candidate 3
1
Enter elective 1 mark
98


Elective marks of candidate 1
96      85      79
Elective marks of candidate 2
90      60
Elective marks of candidate 3
98

No comments:

Post a Comment