Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array Median As Double

Posted on 2016-10-02
4
Medium Priority
?
54 Views
Last Modified: 2016-10-02
I have a randomly generated array of integers between 1 and 100, and the array's length is between 5 and 10. The array is sorted in ascending order.

I am trying to determine how I would calculate the median. Part of the criteria is to "declare the median as a double." My current work uses an If...Else loop to get the median, but the median is not declared. I realize I have to start over, but I am unsure where even to begin.
0
Comment
Question by:Marie Hester
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41825722
Please post the code you have come up with as a starting point.
1
 

Author Comment

by:Marie Hester
ID: 41825740
I wound up removing the loop all together, but now the median is not calculating correctly.

Dim rnd As New Random()
        Dim randArray(10) As Integer

        For n As Integer = 5 To 10
            randArray(n) = rnd.Next(1, 100)

        Next

        Array.Sort(randArray)

        For n As Integer = 5 To 10
            txtOutput.AppendText(randArray(n).ToString + Space(4))
        Next


        Dim num1 As Double = randArray(randArray.Length \ 2)
        Dim num2 As Double= randArray((randArray.Length \ 2) - 1)
        Dim median As Double = (num1 + num2) \ 2
        lblMedian.Text = (median.ToString) 

Open in new window


Before the loop I had was similar to this:

If (ubound(array) mod 2) = 0 Then

' odd number of items
median = array(ubound(array) \ 2)

Else

' even number of items
median = (array(ubound(array) \ 2) + array(1 + ubound(array) \ 2)) / 2
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 41825791
Hi Marie;

I have modified your code and should be doing what you need. Please read the comments in the code.
'' This value is 1 less then the number of values wanted, arrays are zero based
Dim numberOfValues As Integer = 9
Dim rnd As New Random()
'' This array had 11 elements in it, index is zero based
'' An index of 9 gives 10 elements 0 - 9
Dim randArray(numberOfValues - 1) As Integer

'' When I read this, "and the array's length is between 5 and 10.",
'' Now this For loop, "For n As Integer = 5 To 10", only uses the last 6
'' elements of the array leaving elements 0 - 4 empty. Is the statement looking to handle
'' any number of integers between 5 and 10 numbers. The modified For loop will always
'' load all elements of the array.
For n As Integer = 0 To (numberOfValues - 1)
    randArray(n) = rnd.Next(1, 100)
Next

Array.Sort(randArray)

'' Modified For loop to match the number of elements in the array
'' Clear the text from last execution of this code
txtOutput.Clear()
lblMedian.Text = ""
For n As Integer = 0 To (numberOfValues - 1)
    txtOutput.AppendText(randArray(n).ToString + Space(4))
Next

'' Using Integer Division
Dim num1 As Integer = (randArray.Length \ 2) - 1
Dim num2 As Integer = num1 + 1
Dim median As Double = 0.0

'' Test to see if the number of elements is even or odd
If numberOfValues Mod 2 = 0 Then
    '' Is Even
    median = (randArray(num1) + randArray(num2)) / 2
Else
    '' Is Odd
    median = randArray(num2)
End If

lblMedian.Text = (median.ToString)

Open in new window

1
 

Author Closing Comment

by:Marie Hester
ID: 41825795
Thank you so much for your assistance.

Your comments were extremely helpful. I realize now where I was going wrong and why I wasn't getting the desired results.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question