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?
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));
0
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));
0
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));
        }
0

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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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


I have posted a related question here:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_28643910.html
0
Fernando SotoRetiredCommented:
@OM Gang, Actually you beat me to it when I saw your post I noticed the off by one. ;=)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.