Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Beginner - Trouble Finding Calculation Error

$
0
0
I am writing a program that takes names of students and their grades and calculates each student's average. I will then output the data to a file. Ignore the incorrect totals. I fixed that problem. I can't get the last average to display and I have no idea why. I've worked back through my code at least 5 or 6 times one line at a time and cannot see my error.


Posted Image

Here's my code:
Public Class Form1

    'constant
    Const MAX_SCORES As Integer = 4
    Const MAX_STUDENTS As Integer = 5

    Structure Student
        Dim strName As String
        Dim dblScores() As Double
        Dim dblTotal As Double
        Dim dblAverage As Double
    End Structure
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'counter
        Dim i As Integer = 0
        Dim j As Integer = 0

        'instantiate structure
        Dim student(MAX_STUDENTS) As Student

        'declare array size in structure
        For i = 0 To MAX_STUDENTS
            ReDim student(i).dblScores(MAX_SCORES)
        Next

        '------------------------------------------------------------------------------------------
        '--------------------------------------initialize data-------------------------------------
        '------------------------------------------------------------------------------------------
        'initialize scores to 0
        For i = 0 To MAX_STUDENTS
            For j = 0 To MAX_SCORES
                student(i).dblScores(j) = 0
            Next
        Next

        'initialize averages to 0
        For i = 0 To MAX_STUDENTS
            student(i).dblAverage = 0
        Next

        'initialize totals to 0
        For i = 0 To MAX_STUDENTS
            student(i).dblTotal = 0
        Next

        '------------------------------------------------------------------------------------------
        '-----------------------------------create textbox arrays----------------------------------
        '------------------------------------------------------------------------------------------
        'create textbox array for names
        Dim txtNames() As TextBox = {txtName1, txtName2, txtName3, txtName4, txtName5, txtName6}

        'create textbox array for scores
        Dim txtScores(,) As TextBox = {{txtScores1_1, txtScores1_2, txtScores1_3, txtScores1_4, txtScores1_5},
                                    {txtScores2_1, txtScores2_2, txtScores2_3, txtScores2_4, txtScores2_5},
                                    {txtScores3_1, txtScores3_2, txtScores3_3, txtScores3_4, txtScores3_5},
                                    {txtScores4_1, txtScores4_2, txtScores4_3, txtScores4_4, txtScores4_5},
                                    {txtScores5_1, txtScores5_2, txtScores5_3, txtScores5_4, txtScores5_5},
                                    {txtScores6_1, txtScores6_2, txtScores6_3, txtScores6_4, txtScores6_5}}

        'create textbox array for averages
        Dim lblAverages() As Label = {lblAvg1, lblAvg2, lblAvg3, lblAvg3, lblAvg4, lblAvg5, lblAvg6}

        '------------------------------------------------------------------------------------------
        '---------------------------------send data into structure---------------------------------
        '------------------------------------------------------------------------------------------
        'send names
        For i = 0 To MAX_STUDENTS
            student(i).strName = txtNames(i).Text.ToString()
        Next

        'send scores
        For i = 0 To MAX_STUDENTS
            For j = 0 To MAX_SCORES
                student(i).dblScores(j) = CDbl(txtScores(i, j).Text.ToString())
            Next
        Next

        'calculate averages and send to structure
        For i = 0 To MAX_STUDENTS
            For j = 0 To MAX_SCORES
                'total grades
                student(i).dblTotal = student(i).dblTotal + student(i).dblScores(j)
            Next
            'get average
            student(i).dblAverage = student(i).dblTotal / (MAX_SCORES + 1)
        Next

        '------------------------------------------------------------------------------------------
        '-------------------------------------display averages-------------------------------------
        '------------------------------------------------------------------------------------------
        For i = 0 To MAX_STUDENTS
            lblAverages(i).Text = student(i).dblAverage.ToString()
        Next

    End Sub
End Class



Sorry, I forgot to Edit the title and now I don't know how. I'd delete and remake it, but alas I don't know how to do that either.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>