Click here to Skip to main content
16,010,488 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello Dear Coders
Here is a code for to find the highest mark(grade) in the class.What about if 2 or more students get the same high mark(grade)? john 90,david 90 ?

C#
using System;
namespace sample1
{
    class Program
    {
        public static void Main(string[] args)
        {
            int c, HighMark=0, place=0;
            String [] A={"John", "David","Clare","Robert","Michael","Peter","Steve","Daniel","Alex","Bart"};
            int [] B={56,78,95,23,95,87,61,77,45,33};
            for (c=0;c<=B.Length-1;c++){
                if(B[c]>= HighMark){
                    HighMark=B[c];
                    place=c; }
            }
            Console.WriteLine("The Person Get The Highest Mark: "+A[place]);
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
Posted
Updated 28-Dec-14 9:37am
v3
Comments
Wendelius 28-Dec-14 15:12pm    
First of all, you should define what is the ordering in case of two or more exact same highest rank. Do you want to retrieve them in alphabetical order, based on the length of the name, something else?
TheC0nquer0r 28-Dec-14 15:28pm    
yes may be.As far as i know,i must use second FOR But it didnt work....
Wendelius 28-Dec-14 15:36pm    
Let me ask again, two persons have the same rank, how should they be ordered and displayed?
TheC0nquer0r 28-Dec-14 15:40pm    
for example john 95 and peter 95.Display John ,Peter
Afzaal Ahmad Zeeshan 28-Dec-14 15:16pm    
Do you want to show them all, or just one item?

One possibility is to gather the highest marks into a separate list and display the list content in the end. Something like
C#
static void Main(string[] args)
{
   System.Collections.Generic.List<int> places = new System.Collections.Generic.List<int>();
   int HighMark = 0;
   String[] A = { "John", "David", "Clare", "Robert", "Michael", "Peter", "Steve", "Daniel", "Alex", "Bart" };
   int[] B = { 56, 78, 95, 23, 95, 87, 61, 77, 45, 33 };
   for (int counter = 0; counter <= B.Length - 1; counter++)
   {
      if (B[counter] > HighMark)
      {
         places.Clear();
         places.Add(counter);
         HighMark = B[counter];
      }
      else if (B[counter] == HighMark)
      {
         places.Add(counter);
      }
   }
   foreach (int place in places)
   {
      Console.WriteLine("The Person Get The Highest Mark: " + A[place]);
   }
   Console.Write("Press any key to continue . . . ");
   Console.ReadKey(true);
}</int></int>
 
Share this answer
 
v2
Using Linq it becomes very easy to produce an ordered set of grades/students with the highest-score:
C#
// required
using System.Collections.Generic;
using System.Linq;

private void ReportGrades()
{
    String[] A = { "John", "David", "Clare", "Robert", "Michael", "Peter", "Steve", "Daniel", "Alex", "Bart" };

    int[] B = { 56, 78, 95, 23, 95, 87, 61, 77, 45, 33 };

    Dictionary<int,List<string>> grades = new Dictionary<int,List<string>>();

    for (int i = 0; i < A.Length; i++)
    {
        if (! grades.Keys.Contains(B[i])) grades[B[i]] = new List<string>();

        grades[B[i]].Add(A[i]);
    }

    var gradesDescending = grades.OrderByDescending(grade => grade.Key).ToList();

    foreach (var gradeResult in gradesDescending)
    {
        Console.Write("Grade: {0}\t", gradeResult.Key.ToString());

        foreach (var name in gradeResult.Value)
        {
            Console.Write("{0} ", name);
        }

        Console.WriteLine();
    }
}

// sample output:

Grade: 95	Clare Michael 
Grade: 87	Peter 
Grade: 78	David 
Grade: 77	Daniel 
Grade: 61	Steve 
Grade: 56	John 
Grade: 45	Alex 
Grade: 33	Bart 
Grade: 23	Robert </string>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900