C# Name Sorting Program

Its is a simple C# program that sort the student name in ascending order. The program take the student name and grade and sort the name in ascending order.

Source Code 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sortStdNames
{
    class Program
    {
        struct student
        {
             public string name;
             public int id;
             public char grade;
         };
        student[] stdInstance = new student[2];
        int size;
        static void Main(string[] args)
        {
            Program m = new Program();
            m.size = 0; 
            welcomeScrean();
            int j = 2;
            Console.Write( "\t  Enter Detail of 2 students " +"\n");
            for (int i=0; i < j; i++)
            {
                getStudentsWithGrade(m);
            }

            displayAllStudents(m);
            sortAndDisplayAllStudents(m);
            String s = Console.ReadLine();
        }

        private static void sortAndDisplayAllStudents(Program m)
        {
            
            String[] temp= new String[m.size];
            List<string> l = new List<string>();
            for ( int h = 0 ; h<m.size ; h++ )               
            {
                try
                {
                    l.Add(m.stdInstance[h].name);
                }
                catch (IndexOutOfRangeException e){ 
                
                }
            }
            l.Sort();
            Console.Write("\n"
                + "\a"
                + "\t  *******************************************************" + "\n"
                + "\t  **                Sorted names are                   **" + "\n"
                + "\t  *******************************************************" + "\n");
            Console.Write("\t  \n");
            foreach (String c in l)
         {
             Console.WriteLine("\t"+c);
         }
        }

        private static void displayAllStudents(Program m)
        {
            Console.Write("\n"
                + "\t  Students added with following data" + "\n"
                + "\t  ======================================" + "\n"
                + "\n");

            for (int n = 0; n < m.size; n++)
            {
                try
                {
                    Console.Write("\n\t  Name: " + m.stdInstance[n].name + "\n"
                        + "\t  Grade: "+m.stdInstance[n].grade);
                }
                catch (IndexOutOfRangeException e)
                {
                    Console.WriteLine("IndexOutOfRangeException caught and " + e);
                }
            }
        }

        private static void getStudentsWithGrade(Program m)
        {
            Console.Write( "\n"
                +"\a"
                +"\t  *******************************************************" +"\n"
                +"\t  **             Adding Student No # " +m.size +"                 **" +"\n"
                +"\t  *******************************************************" +"\n");

            Console.Write( "\n"
             +"\n\tEnter Name : ");


            try
            {
                m.stdInstance[m.size].name = Console.ReadLine();
            
                Console.Write( "\n\tEnter Grade ");
            
                m.stdInstance[m.size].grade = Convert.ToChar(Console.ReadLine());//taking grade
             
                m.stdInstance[m.size].id = m.size;//settind customer id
           }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine("IndexOutOfRangeException caught and " + e);
            }
            m.size++;
            Console.Write( "\n"
             +"\t  Press Enter to continue . . ." +"\n");
            String s= Console.ReadLine();
        }

        private static void welcomeScrean()
        {

        }
    }
}

Output of the Program

Sort student name according to their names

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments:

  1. Joshua vs Klitschko is a professional boxing match to be contested between Anthony Joshua vs Klitschko Live. Joshua vs Klitschko Live Stream is a professional boxing match to be contested between Joshua vs Klitschko Fight. Watch Joshua vs Klitschko event will take place on 29 April 2017 at Wembley Stadium in London, England, with Joshua's IBF and the vacant WBA (Super) and IBO heavyweight titles on the line. The Klitschko vs Joshua Ukrainian has not fought since then and at 41-years-old, many are expecting him to lose back-to-back fights for the first time in his career.

    ReplyDelete