I cannot get the average scores for students 2 through 6. What am I doing wrong?
ID: 3856154 • Letter: I
Question
I cannot get the average scores for students 2 through 6. What am I doing wrong? The only one that works is for the first student. This is from Starting out with Visual Basic 2016, Chapther 9, Programming Challenges, #3 Student Test Scores
· Imports System.IO
·
·
· Public Class Form1
· Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
· ' Close(program)
· Me.Close()
· End Sub
·
·
· Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
· StudentScoreDataInput()
· End Sub
·
· End Class
·
· Module StudentTestScoresModule
·
· Const intMAX_SUBSCRIPT_STUDENT As Integer = 6
· Const intMAX_SUBSCRIPT_STUDENT_SCORES As Integer = 5
·
·
· 'create structure
·
· Public Structure StudentData
· Dim strName As String
· Dim dblTestScoresArray() As Double
· Dim dblAverage As Double
· End Structure
·
· Dim dblTotalStd1 As Double
· Dim dblTotalStd2 As Double
· Dim dblTotalStd3 As Double
· Dim dblTotalStd4 As Double
· Dim dblTotalStd5 As Double
· Dim dblTotalStd6 As Double
·
· Dim dblScore As Double
·
· Dim StudentsArray(intMAX_SUBSCRIPT_STUDENT) As StudentData
·
· Sub StudentNameDataInput()
· StudentsArray(0).strName = Form1.txtName1.Text
· StudentsArray(1).strName = Form1.txtName2.Text
· StudentsArray(2).strName = Form1.txtName3.Text
· StudentsArray(3).strName = Form1.txtName4.Text
· StudentsArray(4).strName = Form1.txtName5.Text
· StudentsArray(5).strName = Form1.txtName6.Text
· End Sub
·
· Sub StudentScoreDataInput()
· Dim dblAverage As Double
· For intIndex = 0 To intMAX_SUBSCRIPT_STUDENT
· ReDim StudentsArray(intIndex).dblTestScoresArray(4)
· Next
·
· 'initialize test scores for first student in the array
· StudentsArray(0).dblTestScoresArray(0) = CDbl(Form1.txtS11.Text)
· StudentsArray(1).dblTestScoresArray(1) = CDbl(Form1.txtS12.Text)
· StudentsArray(2).dblTestScoresArray(2) = CDbl(Form1.txtS13.Text)
· StudentsArray(3).dblTestScoresArray(3) = CDbl(Form1.txtS14.Text)
· StudentsArray(4).dblTestScoresArray(4) = CDbl(Form1.txtS15.Text)
·
·
·
· For Each i As StudentData In StudentsArray
· For Each S As Double In i.dblTestScoresArray
· dblTotalStd1 += S
· Next
· Next
· dblAverage = dblTotalStd1 / intMAX_SUBSCRIPT_STUDENT_SCORES
· Form1.lblAvg1.Text = (dblAverage.ToString)
· End Sub
·
· Sub CalculateAverage()
· End Sub
·
· End Module
Explanation / Answer
Since I don't have screenshot of your form, I presume that for each student you have 5 text boxes for the scores.
For e.g. for student 2, they would be txtS21 , txtS22, txtS23 etc.
With this assumption, you were loading only the first student's scores in the Sub StudentScoreDataInput(). Also you were wrongly storing in subscipts 1, 2, 3 ,4, 5 which is meant for other students and not 1st student. i.e you should always use StudentsArray(0) in all the line where scores are being set into the scores array. Then you should do similar thing for StudentsArray(1) and so on. After scores for each of the student is loade, you calculate the average for each student and store it in its structure field dlbAverage. Once all students are done in the loop, you set the average for each student in their corresponding labels accessing the structure member dblAverage.
I have cleaned up the module code to remove unwanted dblTotalStd variables and used a single dbtTotal inside the Sub where average is calculated.
Post a comment in case there are any issues. I will need to see the form screen shot and need text boxes names in case the code given below does not work. If the answer works for you, please rate it. Thank you.
The modified code for Module is
Module StudentTestScoresModule
Const intMAX_SUBSCRIPT_STUDENT As Integer = 6
Const intMAX_SUBSCRIPT_STUDENT_SCORES As Integer = 5
'create structure
Public Structure StudentData
Dim strName As String
Dim dblTestScoresArray() As Double
Dim dblAverage As Double
End Structure
Dim StudentsArray(intMAX_SUBSCRIPT_STUDENT) As StudentData
Sub StudentNameDataInput()
StudentsArray(0).strName = Form1.txtName1.Text
StudentsArray(1).strName = Form1.txtName2.Text
StudentsArray(2).strName = Form1.txtName3.Text
StudentsArray(3).strName = Form1.txtName4.Text
StudentsArray(4).strName = Form1.txtName5.Text
StudentsArray(5).strName = Form1.txtName6.Text
End Sub
Sub StudentScoreDataInput()
Dim dblAverage(intMAX_SUBSCRIPT_STUDENT) As Double
Dim dlbTotal as Double
For intIndex = 0 To intMAX_SUBSCRIPT_STUDENT
ReDim StudentsArray(intIndex).dblTestScoresArray(intMAX_SUBSCRIPT_STUDENT_SCORES)
Next
'initialize test scores for first student in the array
StudentsArray(0).dblTestScoresArray(0) = CDbl(Form1.txtS11.Text)
StudentsArray(0).dblTestScoresArray(1) = CDbl(Form1.txtS12.Text)
StudentsArray(0).dblTestScoresArray(2) = CDbl(Form1.txtS13.Text)
StudentsArray(0).dblTestScoresArray(3) = CDbl(Form1.txtS14.Text)
StudentsArray(0).dblTestScoresArray(4) = CDbl(Form1.txtS15.Text)
'initialize test scores for second student in the array
StudentsArray(1).dblTestScoresArray(0) = CDbl(Form1.txtS21.Text)
StudentsArray(1).dblTestScoresArray(1) = CDbl(Form1.txtS22.Text)
StudentsArray(1).dblTestScoresArray(2) = CDbl(Form1.txtS23.Text)
StudentsArray(1).dblTestScoresArray(3) = CDbl(Form1.txtS24.Text)
StudentsArray(1).dblTestScoresArray(4) = CDbl(Form1.txtS25.Text)
'initialize test scores for 3rd student in the array
StudentsArray(2).dblTestScoresArray(0) = CDbl(Form1.txtS31.Text)
StudentsArray(2).dblTestScoresArray(1) = CDbl(Form1.txtS32.Text)
StudentsArray(2).dblTestScoresArray(2) = CDbl(Form1.txtS33.Text)
StudentsArray(2).dblTestScoresArray(3) = CDbl(Form1.txtS34.Text)
StudentsArray(2).dblTestScoresArray(4) = CDbl(Form1.txtS35.Text)
'initialize test scores for 4th student in the array
StudentsArray(3).dblTestScoresArray(0) = CDbl(Form1.txtS41.Text)
StudentsArray(3).dblTestScoresArray(1) = CDbl(Form1.txtS42.Text)
StudentsArray(3).dblTestScoresArray(2) = CDbl(Form1.txtS43.Text)
StudentsArray(3).dblTestScoresArray(3) = CDbl(Form1.txtS44.Text)
StudentsArray(3).dblTestScoresArray(4) = CDbl(Form1.txtS45.Text)
'initialize test scores for 5th student in the array
StudentsArray(4).dblTestScoresArray(0) = CDbl(Form1.txtS51.Text)
StudentsArray(4).dblTestScoresArray(1) = CDbl(Form1.txtS52.Text)
StudentsArray(4).dblTestScoresArray(2) = CDbl(Form1.txtS53.Text)
StudentsArray(4).dblTestScoresArray(3) = CDbl(Form1.txtS54.Text)
StudentsArray(4).dblTestScoresArray(4) = CDbl(Form1.txtS55.Text)
'initialize test scores for 6th student in the array
StudentsArray(5).dblTestScoresArray(0) = CDbl(Form1.txtS61.Text)
StudentsArray(5).dblTestScoresArray(1) = CDbl(Form1.txtS62.Text)
StudentsArray(5).dblTestScoresArray(2) = CDbl(Form1.txtS63.Text)
StudentsArray(5).dblTestScoresArray(3) = CDbl(Form1.txtS64.Text)
StudentsArray(5).dblTestScoresArray(4) = CDbl(Form1.txtS65.Text)
For Each i As StudentData In StudentsArray
dblTotal = 0 'initialize to 0 for each student
For Each S As Double In i.dblTestScoresArray
dblTotal += S
Next
'set the average for the current student
i.dblAverage = dblTotalStd1 / intMAX_SUBSCRIPT_STUDENT_SCORES
Next
'now set the values in the lables
Form1.lblAvg1.Text = (StudentsArray(0).dblAverage.ToString)
Form1.lblAvg2.Text = (StudentsArray(1).dblAverage.ToString)
Form1.lblAvg3.Text = (StudentsArray(2).dblAverage.ToString)
Form1.lblAvg4.Text = (StudentsArray(3).dblAverage.ToString)
Form1.lblAvg5.Text = (StudentsArray(4).dblAverage.ToString)
Form1.lblAvg6.Text = (StudentsArray(5).dblAverage.ToString)
End Sub
End Module
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.