C# 2D Array created and populated dynamically


The previous sample we worked has altered like below as that Two-dimensional array created and populated dynamically,

Program

using System;

namespace ClassRoomSamples
{
    class Demo05Array4
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many students?");
            int studentsCount = int.Parse(Console.ReadLine());

            int[,] Marks = new int[studentsCount, 2];
            Console.WriteLine($"Enter marks of {studentsCount} Students");

            for (int student = 0; student < studentsCount; student++) 
            {
                Console.WriteLine($"Enter internal and external marks of                                                 student {student + 1}");
                for (int sub = 0; sub < 2; sub++)
                {
                    Marks[student, sub] = int.Parse(Console.ReadLine());
                }
            }

            Console.WriteLine($"{"Student",-12}{"Internal",10}                                                            {"External",10}");
            Console.WriteLine("===================================");
            for (int student = 0; student < studentsCount; student++) 
            {
                Console.Write($"Student{student + 1:00}");
                for (int sub = 0; sub < 2; sub++)
                {
                    Console.Write($"{Marks[student, sub],10}");
                }
                Console.WriteLine();
            }
            Console.WriteLine("===================================");

            Console.ReadKey();
        }
    }
}


Output

How many students?
2

Enter marks of 2 Students

Enter internal and external marks of student 1
22
70

Enter internal and external marks of student 2
23
65

Student       Internal  External
===================================
Student01        22        70
Student02        23        65

===================================


No comments:

Post a Comment