-
Oct 31st, 2011, 08:50 PM
#1
Thread Starter
New Member
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
-
Oct 31st, 2011, 08:55 PM
#2
Re: VB 2010 Array Help
vb Code:
dim lines() as string = IO.File.ReadAllLines(textFile)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 09:13 PM
#3
Thread Starter
New Member
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)?
-
Oct 31st, 2011, 09:14 PM
#4
Re: VB 2010 Array Help
what's your algorithm for calculating std deviation?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 09:26 PM
#5
Re: VB 2010 Array Help
ok. try this then:
vb Code:
Public Class Form1
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'assuming each line of your text file contains a valid
'decimal, + there are no empty lines in your file
Dim numbers() As Decimal = Array.ConvertAll(IO.File.ReadAllLines(OpenFileDialog1.FileName), Function(s) CDec(s))
Dim avg As Decimal = getAvg(numbers)
Dim stdDeviation As Decimal = getStdDeviation(numbers)
End If
End Sub
Private Function getAvg(ByVal d() As Decimal) As Decimal
Return d.Average
End Function
Private Function getStdDeviation(ByVal d() As Decimal) As Decimal
'run your code
'Return std deviation
End Function
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 09:28 PM
#6
Thread Starter
New Member
Re: VB 2010 Array Help
s = sqrt(((x1-m)^2+(x2-m)^2+(x3-m)^2+...+(xn-m)^2)/n)
-
Oct 31st, 2011, 09:30 PM
#7
Re: VB 2010 Array Help
i'll let someone else help you with that. i got a headache before the =
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 09:33 PM
#8
Thread Starter
New Member
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.
-
Oct 31st, 2011, 09:37 PM
#9
Re: VB 2010 Array Help
ok. what is the value of m?
+ the value of n?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 09:42 PM
#10
Thread Starter
New Member
-
Oct 31st, 2011, 09:49 PM
#11
Re: VB 2010 Array Help
try this:
vb Code:
Public Class Form1
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
'assuming each line of your text file contains a valid
'decimal, + there are no empty lines in your file
Dim numbers() As Decimal = Array.ConvertAll(IO.File.ReadAllLines(OpenFileDialog1.FileName), Function(s) CDec(s))
Dim avg As Decimal = getAvg(numbers)
Dim stdDeviation As Decimal = getStdDeviation(numbers, avg)
End If
End Sub
Private Function getAvg(ByVal d() As Decimal) As Decimal
Return d.Average
End Function
Private Function getStdDeviation(ByVal d() As Decimal, avg as decimal) As Decimal
Return Math.Sqrt(Array.ConvertAll(d, Function(n) (n - avg) ^ 2).Sum / d.length)
End Function
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 10:17 PM
#12
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 10:21 PM
#13
Thread Starter
New Member
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.
-
Oct 31st, 2011, 10:28 PM
#14
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 31st, 2011, 10:42 PM
#15
Thread Starter
New Member
Re: VB 2010 Array Help
So I just use "d()" to call the values of the array?
-
Nov 1st, 2011, 09:54 AM
#16
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|