How to average numbers from an array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DePonce
    New Member
    • Jan 2012
    • 4

    How to average numbers from an array?

    Private Sub btnDisplay_Clic k(sender As Object, e As System.EventArg s) Handles btnDisplay.Clic k
    ' calculates average score and displays the result

    Dim intScores() As Integer = {88, 72, 99, 20, 66, 95, 99, 100, 72, 88, 78, 45, 57, 89, 85, 78, 75, 88, 72, 88}

    This is the beginning of the programming, but I don't know how to continue to achieve the end result. I need to find the total and display the average score of the array in a message box.
  • C CSR
    New Member
    • Jan 2012
    • 144

    #2
    1) Get the length of the array.
    2) Total the array values.
    3) Divide the Total Value by the Array length.

    Present results in msgbox; Do you need help with that part of the code?

    Comment

    • DePonce
      New Member
      • Jan 2012
      • 4

      #3
      Originally posted by C CSR
      1) Get the length of the array.
      2) Total the array values.
      3) Divide the Total Value by the Array length.

      Present results in msgbox; Do you need help with that part of the code?
      My problem is the coding, I am confuse on how to word the codes to obtain the result

      Comment

      • C CSR
        New Member
        • Jan 2012
        • 144

        #4
        Using Access Basic:

        Code:
        Sub avgarray1()
        Dim varData As Variant
        varData = Array(1, 2, 3, 4, 5) 'zero based
        x = 0
         For i = 0 To UBound(varData)
           x = x + varData(i)
        Next
        
        y = UBound(varData) + 1 'gives length of array
        varDataAvg = x / y
        
        Debug.Print "Totaled values in array varData = " & x
        Debug.Print "Length of array varData = & y"
        Debug.Print "Average value in array = " & varDataAvg
        
        End Sub
        See if this works.

        Comment

        • C CSR
          New Member
          • Jan 2012
          • 144

          #5
          Simple message box:
          Code:
          myMsg = MsgBox("Results = " & varDataAvg, vbOKOnly, "Example")

          Comment

          • C CSR
            New Member
            • Jan 2012
            • 144

            #6
            Your VB variation:

            Code:
            Dim myIntArray() As Integer = {1, 2, 3, 4, 5}
            Everything else worked in VB 2008 (I wasn't set up for that before)

            Comment

            • C CSR
              New Member
              • Jan 2012
              • 144

              #7
              You also need the code below, and put "Interaction.Ms gBox" instead of just "msgbox" in the previous code.

              Code:
              Public Shared Function MsgBox ( _
                  Prompt As Object, _
                  Buttons As MsgBoxStyle, _
                  Title As Object _
              ) As MsgBoxResult
              I'm a little behind on the real deal. Thanks for the exercise.

              Comment

              • DePonce
                New Member
                • Jan 2012
                • 4

                #8
                Originally posted by C CSR
                You also need the code below, and put "Interaction.Ms gBox" instead of just "msgbox" in the previous code.

                Code:
                Public Shared Function MsgBox ( _
                    Prompt As Object, _
                    Buttons As MsgBoxStyle, _
                    Title As Object _
                ) As MsgBoxResult
                I'm a little behind on the real deal. Thanks for the exercise.


                Thanks you, for some reason on my VIsual Basic those codes do not work. Maybe I am doing something wrong. I will keep trying to see what I am doing wrong and fix it.
                Thanks again

                Comment

                • DePonce
                  New Member
                  • Jan 2012
                  • 4

                  #9
                  Originally posted by C CSR
                  Your VB variation:

                  Code:
                  Dim myIntArray() As Integer = {1, 2, 3, 4, 5}
                  Everything else worked in VB 2008 (I wasn't set up for that before)
                  I have VB 2010 and it not took that codes. Still I can't get the display box to get the sum of all numbers and get the average.

                  Comment

                  • C CSR
                    New Member
                    • Jan 2012
                    • 144

                    #10
                    I hope I didn't mix you up because the very first code I sent was not what you needed. I used almost all of it, except:

                    1} I replaced 1 line the declaration of the array with #6.

                    2) I changed the msgbox line from from #5 and added the "Interactio n" word.

                    3) I also added at the top of my module the code in #7.

                    I only have the 2008 Express version on this machine and to save time all I did was run it as a Console aplication. And I apologize because I'm rusty and because I haven't been building things in there real recently.

                    Somebody Else Needs to HELP US ON THIS, PLEASE.

                    Comment

                    • danp129
                      Recognized Expert Contributor
                      • Jul 2006
                      • 323

                      #11
                      Code:
                              Dim intScores() As Integer = {88, 72, 99, 20, 66, 95, 99, 100, 72, 88, 78, 45, 57, 89, 85, 78, 75, 88, 72, 88}
                              MsgBox(intScores.Average.ToString)
                              MsgBox(intScores.Sum.ToString)

                      Comment

                      • C CSR
                        New Member
                        • Jan 2012
                        • 144

                        #12
                        Thank you danp129.!
                        I knew there was a shortcut out there somewhere. My bad.

                        Comment

                        Working...