Re: VB 2008: Array, Grades
how is the textfile structured?
can you post it?
Re: VB 2008: Array, Grades
As you haven't said that this is homework I'll assume that you don't have to do it a specific way but that you just want the quickest and easiest way.
1. Call File.ReadAllLines to get a String array containing the values from the file.
2. Call Array.ConvertAll to create an Integer array containing the number values represented by the strings in the file.
3. Use the Length property of the array to get the number of grades.
4. Call the Average method of the array to get the average value. Note that, if the values are Integers then Average will return an Integer too. If you want decimal places then you have to call Sum to get the total and then divide that by the number of values yourself.
5. There is no inbuilt functionality for that. You may be able to use LINQ although probably not. You should just find the formula for std dev and write some code to implement it. I reckon there'd be examples on the web already.
Re: VB 2008: Array, Grades
Quote:
Originally Posted by
jmcilhinney
You should just find the formula for std dev and write some code to implement it. I reckon there'd be examples on the web already.
there are examples in this forum
Re: VB 2008: Array, Grades
I'm still having trouble figuring out some things. There are "20" grades total. Why is my Mean and Standard Deviation still way off? Here is my coding so far:
lstBox1.Items.Clear()
Dim name As String
Dim total, x, n As Double
Dim sr As IO.StreamReader = IO.File.OpenText("scores.txt")
Dim count As Double
Do While sr.Peek <> -1
name = sr.ReadLine
total += CDbl(sr.ReadLine)
count += 1
Loop
sr.Dispose()
sr = IO.File.OpenText("scores.txt")
Do While sr.Peek <> -1
name = sr.ReadLine
x = CDbl(sr.ReadLine)
n += ((x - total) ^ 2)
Loop
lstBox1.Items.Add("There were " & (CInt(count)) & " exams.")
lstBox1.Items.Add("Mean = " & (total / count))
lstBox1.Items.Add("Standard Deviation = " & Math.Sqrt(n / count))
lstBox1.Items.Add(" ")
Dim fmtStr0 As String = "{0,2}{1,11}"
lstBox1.Items.Add(String.Format(fmtStr0, "Score", "Grade"))
Dim grade0 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade0, "59", "D"))
Dim grade1 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade1, "60", "D"))
Dim grade2 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade2, "65", "C"))
Dim grade3 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade3, "75", "C"))
Dim grade4 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade4, "56", "D"))
Dim grade5 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade5, "90", "B"))
Dim grade6 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade6, "66", "C"))
Dim grade7 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade7, "62", "D"))
Dim grade8 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade8, "98", "B"))
Dim grade9 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade9, "72", "C"))
Dim grade10 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade10, "95", "B"))
Dim grade11 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade11, "71", "C"))
Dim grade12 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade12, "63", "D"))
Dim grade13 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade13, "77", "C"))
Dim grade14 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade14, "65", "C"))
Dim grade15 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade15, "77", "C"))
Dim grade16 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade16, "65", "C"))
Dim grade17 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade17, "50", "F"))
Dim grade18 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade18, "85", "B"))
Dim grade19 As String = "{0,2}{1,12}"
lstBox1.Items.Add(String.Format(grade19, "62", "D"))
Re: VB 2008: Array, Grades
theres nothing wrong with your mean calculation.
the standard deviation:
n += ((x - total) ^ 2)
should be:
n += ((x - mean) ^ 2)