C#: find value where 70% of numbers in array are higher

I use the following C# code to find the average value in an array.

How can I find the value where 70% of the numbers are higher that that number?

          double[] xyz = { 3.5, 4.2, 5.8, 6.7, 7.2, 8,9, 9.2, 12.3, 14.5 };
          Print( "Average Value:" + xyz.Average() );
          Print( "70% of values are higher than:" + "???" );

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omgangIT ManagerCommented:
I didn't actually test this but it should give you the idea.  Unless I misunderstood your question.
OM Gang


          double[] xyz = { 3.5, 4.2, 5.8, 6.7, 7.2, 8,9, 9.2, 12.3, 14.5 };
          Print( "Average Value:" + xyz.Average() );
         
          //sort array elements in ascending order
          Array.Sort(xyz);
          //get total number of array elements
          int arrLen = xyz.Length;
          //get number that is (closest to) 30% of total count
          int thirty = (int) (arrLen * .3);
         
          Print( "70% of values are higher than:" + xyz.GetValue(thirty));
Fernando SotoRetiredCommented:
Just adding to omgang solution, need to account that array's start at index 0 so you need to subtract 1 from the index value as shown below.

//get number that is (closest to) 30% of total count
int thirty = (int) (arrLen * .3) - 1;

Debug.Print( "70% of values are higher than: " + xyz.GetValue(thirty));
omgangIT ManagerCommented:
I've tested.  One change
OM Gang

        private void button1_Click(object sender, EventArgs e)
        {
            double[] xyz = { 3.5, 4.2, 5.8, 6.7, 7.2, 8, 9, 9.2, 12.3, 14.5 };
            //Print("Average Value:" + xyz.Average());
            Console.WriteLine("Average Value: " + xyz.Average());

            //sort array elements in ascending order
            Array.Sort(xyz);
            //get total number of array elements
            int arrLen = xyz.Length;
            //get number that is (closest to) 30% of total count
            int thirty = (int)(arrLen * .3);
            Console.WriteLine("thirty : " + thirty);

            //Print("70% of values are higher than:" + xyz.GetValue(thirty));
            Console.WriteLine("70% of values are higher than: " + xyz.GetValue(thirty - 1));
        }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

omgangIT ManagerCommented:
Thanks Fernando!  I noticed the same and posted 12 seconds behind you.  Good catch.
OM Gang
skijAuthor Commented:
Thank you!  


I have posted a related question here:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28643910.html
Fernando SotoRetiredCommented:
@OM Gang, Actually you beat me to it when I saw your post I noticed the off by one. ;=)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.