C# Array of structure elements or blocks

Program
using System;
namespace ClassRoomSamples
{
    class Demo13ArrayOfStructureElements
    {
        struct trainee
        {
            public string traineeName;
            public int score;
            public string result;
        };

        static void Main(string[] args)
        {
            //Like int[] a = new int[5];

            //Array of Structure variables
            trainee[] t = new trainee[5];

            Console.WriteLine("How many Records?");
            int count = int.Parse(Console.ReadLine());

            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("Enter Name, score of participants");
                t[i].traineeName = Console.ReadLine();
                t[i].score = int.Parse(Console.ReadLine());

                if (t[i].score >= 7)
                    t[i].result = "Selected";
                else
                    t[i].result = "Not Selected";
            }

            Console.WriteLine("=====================================");
            Console.WriteLine("Name\tScore\tResult");
            Console.WriteLine("=====================================");
            for (int i = 0; i < count; i++)
            {
                Console.Write(t[i].traineeName + "\t");
                Console.Write(t[i].score + "\t");
                Console.WriteLine(t[i].result);
            }

            Console.ReadKey();
        }
    }
}

Output

How many Records?
3
Enter Name, score of participants
Subanila
8
Enter Name, score of participants
Mathusudhani
7
Enter Name, score of participants
Chitra
5

=====================================
Name         Score      Result
=====================================
Subanila        8       Selected
Mathusudhani    7       Selected
Chitra          5       Not Selected

No comments:

Post a Comment