Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi i had an task like have to find the largest of 1st 3 numbers in a group i tried with my code bt getting output as fist largest number alone.. pls help me to solve it...
C#
int[] arr1 = new int[8] { 10, 5, 15, 11, 2, 0, 9, 7 };

            int firstHighestNumber1 = arr1[0];
            int secondHighestNumber1 = arr1[0];
            int thirdHighestNumber = arr1[0];
            for (int i1 = 0; i1 < arr1.Length; i1++)
            {
                if (arr1[i1] > firstHighestNumber1)
                {
                    firstHighestNumber1 = arr1[i1];
                }
            }

            for (int x = 0; x < arr1.Length; x++)
            {
                if (arr1[x] > secondHighestNumber1 && firstHighestNumber1 != arr1[x])
                {
                    if (arr1[x] < secondHighestNumber1)
                    {
                        secondHighestNumber1 = arr1[x];
                    }
                }
            }
            for (int y = 0; y < arr1.Length; y++)
            {
                if (arr1[y] > thirdHighestNumber && secondHighestNumber1 != arr1[y])
                {
                    thirdHighestNumber = arr1[y];
                }
            }




Am expecting the op fr

firstnumber=15
seconfnumber=11
thirdnumber=10



Help me Thank you Have a grt Day :-)
Posted

try following code
C#
int[] arr = { 10, 20, 10, 15, 25, 30, 11 };
arr = arr.Distinct().ToArray();
Array.Sort(arr);
Array.Reverse(arr);
int first = arr[0];
int secont = arr[1];
int third = arr[2];
 
Share this answer
 
v2
Comments
usha C 31-Aug-13 1:52am    
Thanks fr ur update..
Sadique KT 31-Aug-13 1:53am    
welcome ..+5 !!!
1) Sort the numbers
2) Take the last 3 from the sorted array.
 
Share this answer
 

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