• 0

[VB6] Adding Values Within An Array


Question

I'm trying to figure out a way to find the sum of the values within an array and store it in a variable

Let's say I have an integer array called intPoints(intCounter). The array currently holds the values [1, 2, 3] and I want to add these values and store them in another variable. How would I do this?

Thanks in advance.

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

thevink is almost there, but you want to treat the array as if you don't know its dimensions, so:

Dim sum As Integer : sum = 0
Dim i As Integer
For i = LBound(intPoints) To UBound(intPoints)
	sum = sum + intPoints(i)
Next

Link to comment
Share on other sites

  • 0

Thanks Antaris, this is almost works.

The way I have it is it loads data from a text file and stores specific lines in specific arrays. Then the form is set up like you are viewing certain records where you can go back and forth between data (like a database). Your code works, but only after you initially load the stats, move to the last record and load the stats again.

Any ideas?

Link to comment
Share on other sites

  • 0

Could you post some more code, specifically how you are handling the loading of data, and at which point you think you need to do summing?

Link to comment
Share on other sites

  • 0

This is how I'm opening the file (sub routine called LoadStats). I do convert the strings to integers before I do calculations, all others work:

    strFileName = strTeamName & ".h" & strUniformNumber
    cdlOpenSave.FileName = strFileName

    Open cdlOpenSave.FileName For Input As #intPlayerFile
        Line Input #intPlayerFile, strTeamName
        Line Input #intPlayerFile, strUniformNumber
        Line Input #intPlayerFile, strFirstName
        Line Input #intPlayerFile, strLastName

        intCounter = 0

        Do
            intCounter = intCounter + 1
            Line Input #intPlayerFile, strOpponentDate(intCounter)
            Line Input #intPlayerFile, strFGA(intCounter)
            Line Input #intPlayerFile, strFGM(intCounter)
            Line Input #intPlayerFile, strFTA(intCounter)
            Line Input #intPlayerFile, strFTM(intCounter)
            Line Input #intPlayerFile, strTPA(intCounter)
            Line Input #intPlayerFile, strTPM(intCounter)
            Line Input #intPlayerFile, strDR(intCounter)
            Line Input #intPlayerFile, strOR(intCounter)
            Line Input #intPlayerFile, strSteals(intCounter)
            Line Input #intPlayerFile, strTurnovers(intCounter)
            Line Input #intPlayerFile, strOF(intCounter)
            Line Input #intPlayerFile, strDF(intCounter)
        intCounter2 = intCounter
        Loop Until EOF(intPlayerFile)
    Close #intPlayerFile

The summing should be done as the user clicks the Load Stats button (calls the LoadStats sub routine), and each time the data is modified (which also calls LoadStats in the end as they click Finish).

The summing partially works now, however when you initially load the stats it shows "1" (which is the first number in the intFGA array), then as you move to the next record and click Load Stats it shows "13" (which is the first added to the second), and then as you move to the last record and click Load Stats again it shows "15" (which is the sum of the array).

I'm thinking it has something to do with where I'm calling the LoadStats sub routine, I've tried placing it in different areas but it doesn't seem to work.

Here's all the code if that helps:

Private Sub cmdAdd_Click()
    cmdLoad.Visible = False
    cmdAdd.Visible = False
    cmdDelete.Visible = False
    cmdModify.Visible = False
    cmdNewPlayer.Visible = False
    cmdNext.Visible = False
    cmdSave.Visible = True
    fraGameTotals.Visible = False
    fraOverallTotals.Visible = False
    cmdFinish.Visible = True
    txtFGA.Locked = False
    txtFGM.Locked = False
    txtFTA.Locked = False
    txtFTM.Locked = False
    txtTPA.Locked = False
    txtTPM.Locked = False
    txtDR.Locked = False
    txtOR.Locked = False
    txtSteals.Locked = False
    txtTurnovers.Locked = False
    txtOF.Locked = False
    txtDF.Locked = False

    strOpponentDateSave = InputBox("Enter the opponent and date of the game.", "Opponent/Date")

    lblOpponentDate.Caption = ""
    txtFGA.Text = ""
    txtFGM.Text = ""
    txtFTA.Text = ""
    txtFTM.Text = ""
    txtTPA.Text = ""
    txtTPM.Text = ""
    txtDR.Text = ""
    txtOR.Text = ""
    txtSteals.Text = ""
    txtTurnovers.Text = ""
    txtOF.Text = ""
    txtDF.Text = ""
End Sub

Private Sub cmdFinish_Click()
    cmdLoad.Visible = True
    cmdAdd.Visible = True
    cmdDelete.Visible = True
    cmdModify.Visible = True
    cmdNewPlayer.Visible = True
    cmdNext.Visible = True
    cmdSave.Visible = False
    fraGameTotals.Visible = True
    fraOverallTotals.Visible = True
    txtFGA.Locked = True
    txtFGM.Locked = True
    txtFTA.Locked = True
    txtFTM.Locked = True
    txtTPA.Locked = True
    txtTPM.Locked = True
    txtDR.Locked = True
    txtOR.Locked = True
    txtSteals.Locked = True
    txtTurnovers.Locked = True
    txtOF.Locked = True
    txtDF.Locked = True

    Call LoadStats
End Sub

Private Sub cmdLoad_Click()
    Call LoadStats
End Sub

Private Sub cmdNext_Click()
    cmdLoad.Visible = True
    If intCounter >= intCounter2 Then
        intCounter = 1
    Else
        intCounter = intCounter + 1
    End If

    lblOpponentDate.Caption = strOpponentDate(intCounter)
    txtFGA.Text = strFGA(intCounter)
    txtFGM.Text = strFGM(intCounter)
    txtFTA.Text = strFTA(intCounter)
    txtFTM.Text = strFTM(intCounter)
    txtTPA.Text = strTPA(intCounter)
    txtTPM.Text = strTPM(intCounter)
    txtDR.Text = strDR(intCounter)
    txtOR.Text = strOR(intCounter)
    txtSteals.Text = strSteals(intCounter)
    txtTurnovers.Text = strTurnovers(intCounter)
    txtOF.Text = strOF(intCounter)
    txtDF.Text = strDF(intCounter)

    Call Calculate
End Sub

Private Sub cmdSave_Click()
    cmdLoad.Visible = True
    cdlOpenSave.Flags = cdlOFNPathMustExist
    intUniformNumber = txtUniformNumber.Text
    strUniformNumber = intUniformNumber
    strFileName = strTeamName & ".h" & strUniformNumber
    cdlOpenSave.FileName = strFileName

    intPlayerFile = FreeFile

    strFGASave = txtFGA.Text
    strFGMSave = txtFGM.Text
    strFTASave = txtFTA.Text
    strFTMSave = txtFTM.Text
    strTPASave = txtTPA.Text
    strTPMSave = txtTPM.Text
    strDRSave = txtDR.Text
    strORSave = txtOR.Text
    strStealsSave = txtSteals.Text
    strTurnoversSave = txtTurnovers.Text
    strOFSave = txtOF.Text
    strDFSave = txtDF.Text

    Open cdlOpenSave.FileName For Append As #intPlayerFile
        If intCounter > 29 Then
            MsgBox "There is a maximum of 30 games per team. If you wish to add more stats, you must first delete a set of existing ones."
            Close #intPlayerFile
        Else
            Print #intPlayerFile, strOpponentDateSave
            Print #intPlayerFile, strFGASave
            Print #intPlayerFile, strFGMSave
            Print #intPlayerFile, strFTASave
            Print #intPlayerFile, strFTMSave
            Print #intPlayerFile, strTPASave
            Print #intPlayerFile, strTPMSave
            Print #intPlayerFile, strDRSave
            Print #intPlayerFile, strORSave
            Print #intPlayerFile, strStealsSave
            Print #intPlayerFile, strTurnoversSave
            Print #intPlayerFile, strOFSave
            Print #intPlayerFile, strDFSave
            intCounter = intCounter + 1
    Close #intPlayerFile
        End If

    lblOpponentDate.Caption = ""
    txtFGA.Text = ""
    txtFGM.Text = ""
    txtFTA.Text = ""
    txtFTM.Text = ""
    txtTPA.Text = ""
    txtTPM.Text = ""
    txtDR.Text = ""
    txtOR.Text = ""
    txtSteals.Text = ""
    txtTurnovers.Text = ""
    txtOF.Text = ""
    txtDF.Text = ""

    Call Calculate
    Call OverallTotals

End Sub

Private Sub Form_Activate()
    intUniformNumber = frmTeamList.txtUniformNumber.Text
    strUniformNumber = intUniformNumber
    strFileName = frmTeamList.lblTeamName.Caption & ".h" & strUniformNumber
    cdlOpenSave.FileName = strFileName

    intPlayerFile = FreeFile

    Open cdlOpenSave.FileName For Input As #intPlayerFile
       Line Input #intPlayerFile, strTeamName
       Line Input #intPlayerFile, intUniformNumber
       Line Input #intPlayerFile, strFirstName
       Line Input #intPlayerFile, strLastName
    Close #intPlayerFile

    txtFirstName.Text = strFirstName
    txtLastName.Text = strLastName
    txtUniformNumber.Text = intUniformNumber
End Sub

Private Sub LoadStats()
    cmdFinish.Visible = False

    intPlayerFile = FreeFile

    strFileName = strTeamName & ".h" & strUniformNumber
    cdlOpenSave.FileName = strFileName

    Open cdlOpenSave.FileName For Input As #intPlayerFile
        Line Input #intPlayerFile, strTeamName
        Line Input #intPlayerFile, strUniformNumber
        Line Input #intPlayerFile, strFirstName
        Line Input #intPlayerFile, strLastName

        intCounter = 0

        Do
            intCounter = intCounter + 1
            Line Input #intPlayerFile, strOpponentDate(intCounter)
            Line Input #intPlayerFile, strFGA(intCounter)
            Line Input #intPlayerFile, strFGM(intCounter)
            Line Input #intPlayerFile, strFTA(intCounter)
            Line Input #intPlayerFile, strFTM(intCounter)
            Line Input #intPlayerFile, strTPA(intCounter)
            Line Input #intPlayerFile, strTPM(intCounter)
            Line Input #intPlayerFile, strDR(intCounter)
            Line Input #intPlayerFile, strOR(intCounter)
            Line Input #intPlayerFile, strSteals(intCounter)
            Line Input #intPlayerFile, strTurnovers(intCounter)
            Line Input #intPlayerFile, strOF(intCounter)
            Line Input #intPlayerFile, strDF(intCounter)
        intCounter2 = intCounter
        Loop Until EOF(intPlayerFile)
    Close #intPlayerFile

    lblOpponentDate.Caption = strOpponentDate(intCounter)
    txtFGA.Text = strFGA(intCounter)
    txtFGM.Text = strFGM(intCounter)
    txtFTA.Text = strFTA(intCounter)
    txtFTM.Text = strFTM(intCounter)
    txtTPA.Text = strTPA(intCounter)
    txtTPM.Text = strTPM(intCounter)
    txtDR.Text = strDR(intCounter)
    txtOR.Text = strOR(intCounter)
    txtSteals.Text = strSteals(intCounter)
    txtTurnovers.Text = strTurnovers(intCounter)
    txtOF.Text = strOF(intCounter)
    txtDF.Text = strDF(intCounter)

    Call Calculate
    Call OverallTotals
End Sub

Private Sub Calculate()
    strFGA(intCounter) = txtFGA.Text
    strFGM(intCounter) = txtFGM.Text
    strFTA(intCounter) = txtFTA.Text
    strFTM(intCounter) = txtFTM.Text
    strTPA(intCounter) = txtTPA.Text
    strTPM(intCounter) = txtTPM.Text

    intFGA(intCounter) = Val(strFGA(intCounter))
    intFGM(intCounter) = Val(strFGM(intCounter))
    intFTA(intCounter) = Val(strFTA(intCounter))
    intFTM(intCounter) = Val(strFTM(intCounter))
    intTPA(intCounter) = Val(strTPA(intCounter))
    intTPM(intCounter) = Val(strTPM(intCounter))

    intTotalPoints(intCounter) = intFTM(intCounter) + (2 * intFGM(intCounter)) + (3 * intTPM(intCounter))
    sngFGPercent(intCounter) = intFGM(intCounter) / intFGA(intCounter)
    sngFTPercent(intCounter) = intFTM(intCounter) / intFTA(intCounter)
    sngTPPercent(intCounter) = intTPM(intCounter) / intTPA(intCounter)

    txtTotalPoints.Text = intTotalPoints(intCounter)
    txtFGPercent.Text = Format(sngFGPercent(intCounter), "Percent")
    txtFTPercent.Text = Format(sngFTPercent(intCounter), "Percent")
    txtTPPercent.Text = Format(sngTPPercent(intCounter), "Percent")
End Sub

Private Sub OverallTotals()
    intTotalFGA = 0

    For intCounter = LBound(intFGA) To intCounter2
        intTotalFGA = intTotalFGA + intFGA(intCounter)
    Next intCounter

    txtTest.Text = intCounter2

    lblTotalGames.Caption = intCounter2
    txtTotalFGA.Text = intTotalFGA
    txtTotalFGM.Text = intTotalFGM
    txtTotalFTA.Text = intTotalFTA
    txtTotalFTM.Text = intTotalFTM
    txtTotalTPA.Text = intTotalTPA
    txtTotalTPM.Text = intTotalTPM
    txtTotalDR.Text = intTotalDR
    txtTotalOR.Text = intTotalOR
    txtTotalSteals.Text = intTotalSteals
    txtTotalTurnovers.Text = intTotalTurnovers
    txtTotalOF.Text = intTotalOF
    txtTotalDF.Text = intTotalDF
    txtPointsAverage.Text = intPointsAverage
    txtTotalFGPercent.Text = Format(sngTotalFGPercent, "Percent")
    txtTotalFTPercent.Text = Format(sngTotalFTPercent, "Percent")
    txtTotalTPPercent.Text = Format(sngTotalTPPercent, "Percent")
End Sub

Link to comment
Share on other sites

  • 0

Should you only be summing on the data of the current record? Are you clearing out intPoints when you load more data?

Link to comment
Share on other sites

  • 0

Why not introduce a variable called totalSum, so after summing per record is complete, you add that total to the totalSum variable?

Link to comment
Share on other sites

  • 0

Would that work with the following scenario:

Stats file is loaded

Player 1 is being displayed with 3 points

Player 2 has 4 points, is loaded but not displayed

Player 3 has 2 points, is loaded but not displayed

While viewing any of these players, the total points (9) should be displayed along with their other specific data.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.