VB 2010 Array Help-VBForums
Results 1 to 16 of 16

Thread: VB 2010 Array Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Question VB 2010 Array Help

    I need assistance with posting lines from a text file (test scores) into an array. From there, I'll have to average the test scores, find the standard deviation (without using the built in function) and assign grades based on the test scores' deviation. I have already written the code for the user to search and upload a text file. And I should be able to do the standard deviation of the array, if someone can help give me an example of how to send the text file to array and average the lines of the array without using the built in math functions.

    Below is the code I currently have.

    Thank you in advance.

    Public Class Form1

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
    Dim textFile As String
    OpenFileDialog1.ShowDialog()
    '
    textFile = OpenFileDialog1.FileName
    lstOut.DataSource = IO.File.ReadAllLines(textFile)
    lstOut.SelectedItem = Nothing

    End Sub
    End Class

  2. #2
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    vb Code:
    1. dim lines() as string =  IO.File.ReadAllLines(textFile)

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: VB 2010 Array Help

    Thanks, Paul, but then how do I go about calling the numbers to insert them into a function (averaging them, or calculating std deviation)?

  4. #4
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    what's your algorithm for calculating std deviation?

  5. #5
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    ok. try this then:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
    4.         If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    5.             'assuming each line of your text file contains a valid
    6.             'decimal, + there are no empty lines in your file
    7.             Dim numbers() As Decimal = Array.ConvertAll(IO.File.ReadAllLines(OpenFileDialog1.FileName), Function(s) CDec(s))
    8.             Dim avg As Decimal = getAvg(numbers)
    9.             Dim stdDeviation As Decimal = getStdDeviation(numbers)
    10.         End If
    11.     End Sub
    12.  
    13.     Private Function getAvg(ByVal d() As Decimal) As Decimal
    14.         Return d.Average
    15.     End Function
    16.  
    17.     Private Function getStdDeviation(ByVal d() As Decimal) As Decimal
    18.         'run your code
    19.         'Return std deviation
    20.     End Function
    21.  
    22. End Class

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: VB 2010 Array Help

    s = sqrt(((x1-m)^2+(x2-m)^2+(x3-m)^2+...+(xn-m)^2)/n)

  7. #7
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    i'll let someone else help you with that. i got a headache before the =

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: VB 2010 Array Help

    It's just inserting each array value in the respective x1, x2, x3, etc. Which I've come to the realization that I don't know how to do that either.

  9. #9
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    ok. what is the value of m?
    + the value of n?

  10. #10

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: VB 2010 Array Help

    m = mean (the average)

  11. #11
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
    4.         If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    5.             'assuming each line of your text file contains a valid
    6.             'decimal, + there are no empty lines in your file
    7.             Dim numbers() As Decimal = Array.ConvertAll(IO.File.ReadAllLines(OpenFileDialog1.FileName), Function(s) CDec(s))
    8.             Dim avg As Decimal = getAvg(numbers)
    9.             Dim stdDeviation As Decimal = getStdDeviation(numbers, avg)
    10.         End If
    11.     End Sub
    12.  
    13.     Private Function getAvg(ByVal d() As Decimal) As Decimal
    14.         Return d.Average
    15.     End Function
    16.  
    17.     Private Function getStdDeviation(ByVal d() As Decimal, avg as decimal) As Decimal
    18.         Return Math.Sqrt(Array.ConvertAll(d, Function(n) (n - avg) ^ 2).Sum / d.length)
    19.     End Function
    20.  
    21. End Class

  12. #12
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    to get the average without built in functions, you'd loop from 0 to d.getupperbound(0) + add each array element's value to a running total, then divide that by the array length

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: VB 2010 Array Help

    That works really well, Paul. At this point, I need to display the individual test scores, then run them through an if-else or case to display what grade they received based on the standard deviation.

    If score >= avg + 1.4(stdDeviation)
    Then grade = A
    If score >= avg + .4(stdDeviation)
    Then grade = B
    etc.

    Then I can output to list box fairly easily, but it's just pulling up the values from the array that I can't understand.

  14. #14
    vb Coda .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    18,966

    Re: VB 2010 Array Help

    this is the output (copied from a msgbox) from my test app:

    ---------------------------
    Standard Deviation
    ---------------------------
    Numbers:



    2

    4

    4

    4

    5

    5

    7

    9



    Average: 5, Standard Deviation: 2
    ---------------------------
    OK
    ---------------------------
    the array d() = {2,4,4,4,5,5,7,9}

    d(0) = 2
    d(1) = 4
    d(2) = 4
    ...
    d(7) = 9

  15. #15

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    9

    Re: VB 2010 Array Help

    So I just use "d()" to call the values of the array?

  16. #16
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,055

    Re: VB 2010 Array Help

    You aren't really calling the values. D is the array. () is the VB operator for indexing into the array. Therefore, a line like:

    d(5)

    gets the 6th element of array d. Other languages use other array indexers, but it all works about the same.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.