Thread: Use of struct to display student who are in top 5

  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
    28,413
    I'm guessing that this is C#, so I am moving this thread to the C# forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,218
    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, 06: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, 07: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