Use of struct to display student who are in top 5

This is a discussion on Use of struct to display student who are in top 5 within the C# Programming forums, part of the General Programming Boards category; i wnt to display student who get highest marks among them...i have code for finding maximum marks from them but ...

  1. #1
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13

    Question Use of struct to display student who are in top 5

    i wnt to display student who get highest marks among them...i have code for finding maximum marks from them but don't kne how to print only top 5 student...so plz help me

    Code:
    static void Main(string[] args)
            {
                Console.WriteLine("Please Enter Number of Students :");
               int n = Convert.ToInt32(Console.ReadLine());
                StudentDetails[] student = new StudentDetails[n]; 
    
    
                for (int row = 0; row < n; row++)
                {
                        Console.WriteLine("Please Enter Student name :");
                        student[row].name = Console.ReadLine();
                        Console.WriteLine("Please Enter Student Marks :");
                        student[row].mark = Convert.ToInt32(Console.ReadLine());
                    
                }
    
    
                int max = student[0].mark;
                for (int row = 0; row < n; row++)
                {
                    if ( student[row].mark > max)
                        max = student[row].mark;
              
                }
    
    
                for (int row = 0; row < n; row++)
                {
                    Console.WriteLine();
                    Console.WriteLine("-----------------------------");
                    Console.WriteLine("Details of Student: " + (row + 1));
    
    
                    Console.WriteLine("Student Name= " + student[row].name);
                    Console.WriteLine("Student Mark= " + student[row].mark);
                }
                Console.ReadLine();
                }
               
        }
    (dis code in Visual Studio)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Saving only the max student, based on their score, obviously won't give you the top 5 students.

    Why not sort the students by their score (all the students), and then take the top five students, from there?

    A Bubble sort is perhaps the simplest, but there are LOTS of sorting algorithms. Why not look up Bubble sort on Wikipedia, and use it, if your teacher has shown you no others?

  3. #3
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    sry i forget to write structure code
    Code:
    public struct StudentDetails
        {
            public string name;
            public int mark;
         }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    I'm guessing that this is C#, so I am moving this thread to the C# forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    ok i'l try...

  6. #6
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    thnxxxx Adak...i got d ans....

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    It's actually really simple in C#:

    Code:
    foreach(StudentDetails student in students.OrderByDescending(s => s.mark).Take(5))
    {
        // Print details here
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 05-02-2012, 04:17 PM
  2. How to display the values of a struct
    By m88g88 in forum C Programming
    Replies: 4
    Last Post: 02-15-2010, 05:07 PM
  3. Struct && vector display empty .....
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2006, 03:03 PM
  4. display records held in a struct
    By colinuk in forum C Programming
    Replies: 3
    Last Post: 02-02-2005, 06:51 AM
  5. how do i display this struct i have??
    By n00b-for-rent in forum C++ Programming
    Replies: 6
    Last Post: 05-03-2002, 02:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21