i created an array of 100 cells, and the user inputs the value into them
now i have to find the average of the array but not all of the cells are used
how do i go to the last value, instead of the last cell ?


any help is appreciated

here is what i have:

Public Class Form1
    '///Global Array
    Dim Score(99) As Integer
    Dim count As Integer = 0


    '///RECORD SCORES
    Sub recScore(ByRef Score() As Integer)
        If count <= 99 Then
            Score(count) = txtGrade.Text
            txtGrade.Clear()
            txtGrade.Focus()
            txtCount.Text = count + 1
            count += 1
        Else
            MessageBox.Show("No space left to save")
        End If
    End Sub
    Private Sub ntnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
        recScore(Score)
    End Sub


    '///Display Score
    Sub displayScore()
        lstScore.Items.Clear()
        Dim num As Integer = 1
        '///Display grades in order
        For i As Integer = 0 To count - 1
            lstScore.Items.Add("Grade number " & num & " is " & Score(i))
            num = num + 1
        Next
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        displayScore()
    End Sub


    '///Calculate Average
    Sub getAverage(ByRef Score() As Integer)
        Dim average As Double

        '///Not sure how to go about this

        lstScore.Items.Add(average)
    End Sub

    Private Sub bntAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bntAverage.Click
        Call getAverage(Score)
    End Sub
End Class

i kept looking online and found the answer, however it would be nice if someone could explain how it works

'///Calculate Average
    Sub getAverage(ByRef Score() As Integer)
        Dim average As Double

        For i As Integer = 0 To Count
            average = average + Score(i) / Count'///its adding itself, but does it leave the division for last ? and how come it's not counting the empty cell?
        Next

        lstScore.Items.Add(average)
    End Sub

Edited 6 Years Ago by juan92: n/a

This question has already been answered. Start a new discussion instead.