Im running into trouble calculating the average of this array:
How is an average calculated for this type of an array? can someone give me an example to study and tinker with please?
My errors thrown:
9 instances of this on different lines (the lower portion of the code block)
Error 1 'average' is not a member of 'WindowsApplication1.Form1.StudentData'.
and I think I am getting an error on intCount1 as well...
I have this block of code from an earlier project that is just a 1x10 array, but unsure how to do this for a 6x5 array:
How is an average calculated for this type of an array? can someone give me an example to study and tinker with please?
Code:
Imports System.IO
Public Class Form1
Dim txtFile As StreamWriter ' Object variable
Public Structure StudentData
Dim studentName As String
Dim testScores() As Double
End Structure
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim student() As StudentData
For intCount = 0 To 5
ReDim student(intCount).testScores(5)
Next
Try
student(0).studentName = txtName1.Text
student(0).testScores(0) = CDbl(SS11.Value)
student(0).testScores(1) = CDbl(SS12.Value)
student(0).testScores(2) = CDbl(SS13.Value)
student(0).testScores(3) = CDbl(SS14.Value)
student(0).testScores(4) = CDbl(SS15.Value)
student(1).studentName = txtName2.Text
student(1).testScores(0) = CDbl(SS11.Value)
student(1).testScores(1) = CDbl(SS22.Value)
student(1).testScores(2) = CDbl(SS23.Value)
student(1).testScores(3) = CDbl(SS24.Value)
student(1).testScores(4) = CDbl(SS15.Value)
student(2).studentName = txtName3.Text
student(2).testScores(0) = CDbl(SS31.Value)
student(2).testScores(1) = CDbl(SS32.Value)
student(2).testScores(2) = CDbl(SS33.Value)
student(2).testScores(3) = CDbl(SS34.Value)
student(2).testScores(4) = CDbl(SS35.Value)
student(3).studentName = txtName4.Text
student(3).testScores(0) = CDbl(SS41.Value)
student(3).testScores(1) = CDbl(SS42.Value)
student(3).testScores(2) = CDbl(SS43.Value)
student(3).testScores(3) = CDbl(SS44.Value)
student(3).testScores(4) = CDbl(SS45.Value)
student(4).studentName = txtName5.Text
student(4).testScores(0) = CDbl(SS51.Value)
student(4).testScores(1) = CDbl(SS52.Value)
student(4).testScores(2) = CDbl(SS53.Value)
student(4).testScores(3) = CDbl(SS54.Value)
student(4).testScores(4) = CDbl(SS55.Value)
student(5).studentName = txtName6.Text
student(5).testScores(0) = CDbl(SS61.Value)
student(5).testScores(1) = CDbl(SS62.Value)
student(5).testScores(2) = CDbl(SS63.Value)
student(5).testScores(3) = CDbl(SS64.Value)
student(5).testScores(4) = CDbl(SS65.Value)
Catch ex As Exception
MessageBox.Show("You May Only Enter Values In The Range 0 - 100")
End Try
Dim average As Double
For intCount = 0 To 5
student(intCount).average = 0
For intCount1 = 0 To 4
student(intCount).average += student(intCount).testScores(intCount1)
Next
' calculate average of series
student(intCount1).average /= 5
Next
lblAvg1.Text = student(0).Average.ToString("n2")
lblAvg2.Text = student(1).average.ToString("n2")
lblAvg3.Text = student(2).average.ToString("n2")
lblAvg4.Text = student(3).average.ToString("n2")
lblAvg5.Text = student(4).average.ToString("n2")
lblAvg6.Text = student(5).average.ToString("n2")
'Open the file
txtFile = File.CreateText("StudentData.txt")
For count = 0 To 5
Dim studentRecord As String
studentRecord = student(count).studentName
For count1 = 0 To 4
studentRecord += " " +
student(count).testScores(count1).ToString("n2")
Next
studentRecord += " " + student(count).average.ToString()
txtFile.WriteLine(studentRecord)
Next
End Sub
Public Function IsValid(ByVal num As Integer) As Integer
If num >= 0 And num <= 100 Then
Return (num)
Else
Return ("error")
End If
End Function
End Class
9 instances of this on different lines (the lower portion of the code block)
Quote:
Error 1 'average' is not a member of 'WindowsApplication1.Form1.StudentData'.
I have this block of code from an earlier project that is just a 1x10 array, but unsure how to do this for a 6x5 array:
Code:
Public Class Form1
Const intMAX_SUBSCRIPT As Integer = 9 ' Upper Subscript
Dim intSeries(intMAX_SUBSCRIPT) As Integer ' Declare intMax
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' get user input with array with values
For intCount As Integer = 0 To intMAX_SUBSCRIPT
Dim intResult As Integer
If Integer.TryParse(InputBox("enter an integer"), intResult) Then
intSeries(intCount) = intResult
Else
intCount -= 1
End If
Next
' display the contents of the array
lbl1.Text = intSeries(0).ToString()
lbl2.Text = intSeries(1).ToString()
lbl3.Text = intSeries(2).ToString()
lbl4.Text = intSeries(3).ToString()
lbl5.Text = intSeries(4).ToString()
lbl6.Text = intSeries(5).ToString()
lbl7.Text = intSeries(6).ToString()
lbl8.Text = intSeries(7).ToString()
lbl9.Text = intSeries(8).ToString()
lbl_10.Text = intSeries(9).ToString()
' declare min, max, sum, and average variables
Dim min As Integer = Integer.MaxValue
Dim max As Integer = Integer.MinValue
Dim sum As Integer
Dim average As Double
' find lowest + highest intSeries, sum + average
For intCount As Integer = 0 To 9
' if lower than current min of series
If intSeries(intCount) < min Then min = intSeries(intCount)
' if higher than current max of series
If intSeries(intCount) > max Then max = intSeries(intCount)
' add number to running total of series
sum += intSeries(intCount)
Next
' calculate average of series
average = sum / 10
' display the results in labels
lblLowest.Text = intSeries.Min
lblHighest.Text = intSeries.Max
lblTotal.Text = intSeries.Sum
lblAverage.Text = intSeries.Average
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Close the form
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
' Clear Array Labels
lbl1.Text = String.Empty
lbl2.Text = String.Empty
lbl3.Text = String.Empty
lbl4.Text = String.Empty
lbl5.Text = String.Empty
lbl6.Text = String.Empty
lbl7.Text = String.Empty
lbl8.Text = String.Empty
lbl9.Text = String.Empty
lbl_10.Text = String.Empty
' Clear Analysis Result Labels
lblLowest.Text = String.Empty
lblHighest.Text = String.Empty
lblTotal.Text = String.Empty
lblAverage.Text = String.Empty
End Sub
End Class