none
Find the n amount of highest numbers in a double array RRS feed

  • Question

  • Homework

    How to find the n amount of highest values in a double array. The n amount is from the user. For example a user inputs the number 3. I need to need a way to search for the 3 highest values in the array. 

    How would i go about this.

    • Edited by StarkDemise Wednesday, May 16, 2018 6:02 AM
    Wednesday, May 16, 2018 1:46 AM

Answers

  • Hi StarkDemise,

    We do not do any homework. What we can do is to point to an direction how you can achieve the solution. As you said find the n Max value without sorting the array, one way you can achieve is that iterate through each item in the array and find the maximum value. Below code give you the result of single output of maximum value in the array. Rest is up to you to figure out how to modify the below approach for finding n numbers of maximum value from an array. Hope this will help you.

           static void Main(string[] args)
            {
                double[] dArray = new double[10] { 1.2, 2.3, 5.3, 2.6, 7.3, 3.9, 6.2, 1.5, 2.9, 4.5 };
                double largest = -1;
                
                
                for (var i = 0; i < dArray.Length; ++i)
                {
                    if (dArray[i] > largest)
                    {                    
                        largest = dArray[i];
                    }
                }
    
                Console.WriteLine("Largets value is: {0}", largest);
                Console.ReadLine();
            }

    Output:

    arrayMax


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, May 21, 2018 9:39 PM
    Moderator

All replies

  • Hi,

    is this homework?

    Hints: Arrays can be sorted and "n" entries can be taken out.

    Regards,

      Thorsten

    Wednesday, May 16, 2018 1:54 AM
  • Yeah. Should i mention that in the post? The exercise requires us to not sort the array.
    Wednesday, May 16, 2018 2:07 AM
  • Yeah. Should i mention that in the post? The exercise requires us to not sort the array.

    Hi,

    if you must not sort the array, create an array of size "n" and initialize all n entries by 0 or a number thats definitely smaller than the smallest in the source array, then loop over the source array and check each numbers size against all "n" numbers in the new array. Replace them in the new array at given index if needed...

    Regards,

      Thorsten

    Wednesday, May 16, 2018 3:28 AM
  • Yeah. Should i mention that in the post? The exercise requires us to not sort the array.

    Being told "don't sort the array" is pretty vague, I mean what does that mean by sort? Clearly some comparison/selection is essential and basically that's what a sort is - so being told "don't sort the array" is a bit confusing.

    Anyway LINQ is the way to go:

                double[] values = new double[10] { 1.2, 2.3, 5.3, 2.6, 7.3, 3.9, 6.2, 1.5, 2.9, 4.5 };
    
                var set = values.OrderBy(d => d).Take(3);
    

    This gets the lowest three values, you can read up how to alter the code to get the three highest values!

    Sunday, May 20, 2018 5:47 PM
  • Sorry, couldn't help me:

     var set = values.OrderByDescending(d => d).Take(3);

                foreach (var item in set)
                {
                    Console.WriteLine(item.ToString());
                }

    Sunday, May 20, 2018 5:58 PM
  • Hi StarkDemise,

    We do not do any homework. What we can do is to point to an direction how you can achieve the solution. As you said find the n Max value without sorting the array, one way you can achieve is that iterate through each item in the array and find the maximum value. Below code give you the result of single output of maximum value in the array. Rest is up to you to figure out how to modify the below approach for finding n numbers of maximum value from an array. Hope this will help you.

           static void Main(string[] args)
            {
                double[] dArray = new double[10] { 1.2, 2.3, 5.3, 2.6, 7.3, 3.9, 6.2, 1.5, 2.9, 4.5 };
                double largest = -1;
                
                
                for (var i = 0; i < dArray.Length; ++i)
                {
                    if (dArray[i] > largest)
                    {                    
                        largest = dArray[i];
                    }
                }
    
                Console.WriteLine("Largets value is: {0}", largest);
                Console.ReadLine();
            }

    Output:

    arrayMax


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, May 21, 2018 9:39 PM
    Moderator