8 Replies - 2082 Views - Last Post: 10 December 2013 - 01:59 AM Rate Topic: -----

#1 Gameordie99  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 21-November 13

Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 03:24 AM

Hi guys,
I am having trouble finding the sum, product and average of this array. It is simply an array of the numbers 1-25. And it seems to be kicking my face in...

As you can see I don't know if I need to list out the array in curly braces or if it's fine if I use a Constant as the upper subscript and dim it as an integer. When I listed it out with the curly braces I was able to do the coding addition style and have it display correctly, but I am supposed to use a for...next loop to initialize all the elements to their position (index) plus 1 in the array, and to calculate the sum and the product of all the elements in my array. Thanks in advance and any help is appreciated.


Option Strict On
Option Explicit On
Public Class Form1

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

        'Dim Array_App() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
        '                               11, 12, 13, 14, 15, 16, 17, 18, 19,
        '                               20, 21, 22, 23, 24, 25}


        Const intMAX_SUBSCRIPT As Integer = 24        'Using Constant as upper subscript
        Dim intUnits(intMAX_SUBSCRIPT) As Integer    'dim array and upper subscript as integer

        Dim intTotal As Integer = 0
        Dim intCount As Integer

        For intCount = 0 To (intUnits.Length - 1)
            intTotal += intUnits(intCount)
        Next


        ''Dim Array_App As Integer


        'Dim sum As Integer

        'sum = Array_App(0) + Array_App(1) + Array_App(2) + Array_App(3)

        'Display the sum of the elements
        lstDisplay.Items.Add(" ")
        lstDisplay.Items.Add("The sum of this array is: " & intTotal)



        ''Display the product of the elements
        'Dim product As Integer

        'product = Array_App(0) * Array_App(1) * Array_App(2) * Array_App(3)

        'lstDisplay.Items.Add(" ")
        'lstDisplay.Items.Add("The product of all the elements in this array is: " & product)

        ''Display the average of the elements




    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble using For...Next Loop to find sum of array...

#2 Minimalist  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 240
  • Joined: 13-November 13

Re: Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 05:59 AM

You better start off by declaring the variables you need and to initialize the array like dim intArr(25) as Integer so it can hold you 25 numbers.
Was This Post Helpful? 0
  • +
  • -

#3 Gameordie99  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 21-November 13

Re: Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 01:31 PM

I used both examples that were in my text and I am still not seeing how this should work...can someone show me how to use the for...next loop to initialize all the elements to their position (index) plus 1 in the array, and to calculate the sum and the product of all the elements in my array?
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 02:35 PM

Minimalist that'll hold 26 items 0 ... 25.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,527
  • Joined: 12-December 12

Re: Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 03:26 PM

For x = 0 To 24
    someArray(x) = 'what will you put here..?
Next x

Was This Post Helpful? 1
  • +
  • -

#6 Minimalist  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 240
  • Joined: 13-November 13

Re: Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 03:27 PM

Yep, you are correct.
Was This Post Helpful? 0
  • +
  • -

#7 Gameordie99  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 21-November 13

Re: Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 04:04 PM

Wow, this is frustrating...I have read through the chapter in the text again and referred back to our discussion here, both seem to be puzzling.
andrewsw...are you able give some explanation with the three lines of code? If someone can show me how to use a for...next loop to initialize all the elements to their position (index) plus 1 in the array, and to calculate the sum, I would be able to figure out how to find the product as well as the average of the elements in the array...
Sorry, but I a, new to programming.... :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Trouble using For...Next Loop to find sum of array...

Posted 08 December 2013 - 04:20 PM

We already have. You just need to think, what is the value of x for each iteration of the loop.
Was This Post Helpful? 0
  • +
  • -

#9 Gameordie99  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 21-November 13

Re: Trouble using For...Next Loop to find sum of array...

Posted 10 December 2013 - 01:59 AM

Well, thank you for the vague and unhelpful hints... here is what I came up with for those searching:
Option Strict On
Option Explicit On
Public Class Form1

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click


        Dim joshsarray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                                       11, 12, 13, 14, 15, 16, 17, 18, 19,
                                       20, 21, 22, 23, 24, 25}

        'find sum
        Dim sum As Integer = 0

        For index = 0 To 24
            sum += joshsarray(index)
        Next

        lstDisplay.Items.Add(" ")
        lstDisplay.Items.Add("The sum of this array is: " & sum)

        'find product
        Dim intProduct As Decimal = 1

        For counter = 0 To 24
            intProduct = intProduct * joshsarray(counter)
        Next

        lstDisplay.Items.Add(" ")
        lstDisplay.Items.Add("The product of this array is: " & intProduct)


        'find average
        Dim arrayAve As Integer
        arrayAve = CInt(sum / 25)

        lstDisplay.Items.Add(" ")
        lstDisplay.Items.Add("The average of this array is: " & arrayAve)


    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

Was This Post Helpful? -1
  • +
  • -

Page 1 of 1