Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all

Below is the code i am using to get the highest and the lowest value from an array.

What i want is it should print 3 highest and 3 lowest values.

Please tell me how to do that.

C#
int n;
            float large, small;
            int[] a = new int[50];
            Console.WriteLine("Enter the size of Array");
            string s = Console.ReadLine();
            n = Int32.Parse(s);
            Console.WriteLine("Enter the array elements");
            for (int i = 0; i < n; i++)
            {
                string s1 = Console.ReadLine();
                a[i] = Int32.Parse(s1);
            }
            Console.Write("");
            large = a[0];
            small = a[0];
            for (int i = 1; i < n; i++)
            {
                if (a[i] > large)
                    large = a[i];
                else if (a[i] < small)
                    small = a[i];
            }
Posted
Updated 30-Jul-15 22:08pm
v2
Comments
Michael_Davies 31-Jul-15 3:43am    
Sort the array then print the first 3 followed by the last 3 entries.
Black_Rose 31-Jul-15 3:46am    
Without sorting.
Michael_Davies 31-Jul-15 3:51am    
Anything else we should know before we do your homework for you?
Black_Rose 31-Jul-15 4:00am    
See i am looking for help here..i need idea not the solution. want to help please help. please let other do. u people have the privilege so u will close the question that's all..
Thanks7872 31-Jul-15 4:42am    
He provided you the idea,not the solution.

I would recommend:
Top 3:
VB
int[] topThree = array.OrderByDescending(i=> i)
                      .Take(3)
                      .ToArray();


Lowest 3:
XML
int[] topThree = array.OrderBy(i=&gt; i)
                      .Take(3)
                      .ToArray();



Some other details:
http://stackoverflow.com/questions/1169759/how-to-get-top-3-elements-in-int-array-using-linq[^]
 
Share this answer
 
v3
Comments
Black_Rose 31-Jul-15 5:44am    
Thanks...
As already suggested, sorting really makes sense in your scenario. However, you explicitely stated 'without sorting'. Of course it could be done that way, however you have to keep sorted the candidates to lowest and highest values:
C#
int[] a = { 10, 21, 35, 2, -4, 27, 51, 60, 1212, 34 };
int[] l = new int[3];
int[] h = new int[3];

l[0] = l[1] = l[2] = int.MaxValue;
h[0] = h[1] = h[2] = int.MinValue;

foreach (int n in a)
{
  for (int j = 0; j < 2; ++j)
  {
    if (n < l[j])
    {
      for (int k = 2; k > j; --k)
        l[k] = l[k - 1];
      l[j] = n;
      break;
    }
  }
  for (int j = 0; j < 2; ++j)
  {
    if (n > h[j])
    {
      for (int k = 2; k > j; --k)
        h[k] = h[k - 1];
      h[j] = n;
      break;
    }
  }
}

  Console.WriteLine("lowest  ones {0}, {1}, {2}", l[0], l[1],l[2]);
  Console.WriteLine("highest ones {0}, {1}, {2}", h[0], h[1], h[2]);
 
Share this answer
 
v2

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