I cannot get the average scores for students 2 through 6. What am I doing wrong?
ID: 3856155 • 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
Your Problem exists in various places.
1) Initialize the variables to 0 which adds with the scores to calculate sum.
Dim dblTotalStd1 As Double = 0
· Dim dblTotalStd2 As Double = 0
· Dim dblTotalStd3 As Double = 0
· Dim dblTotalStd4 As Double = 0
· Dim dblTotalStd5 As Double = 0
· Dim dblTotalStd6 As Double = 0
2) Next error is in the below code
'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)
==> If you are initializing the test score for 1st student then call only the 1st object of student Data structure and use that same object to initialize all the subject scores for that particular student by replacing the indices of Student Data structure object with index value 0 for first student. Replace the above lines with below code.
'initialize test scores for only the 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)
3) Second in the loop to calculate the SUM just call the first student array object and its corresponding scores from 0 to 4. Hence adjust you loop accordingly.
Erroneous Code:
For Each i As StudentData In StudentsArray
· For Each S As Double In i.dblTestScoresArray
· dblTotalStd1 += S
· Next
· Next
Replace the above code with below one,
For Each S As Double In StudentsArray(0).dblTestScoresArray
· dblTotalStd1 += S
· Next
===> If the above steps does not solve the problem then you need to post the form designer code and the program code as well in a next question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.