Solution:NIIT/GNIIT Sonugiri0032@gmail.com

Tuesday, January 05, 2016

C# Name Sorting Program

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:

Related Posts:

0 comments:

GNIITSOLUTION GNIIT SOLUTION. Powered by Blogger.

Translate

Blog Archive

undefined