none
determine the index in a listbox RRS feed

  • Question

  • given a sequence, i need to know if this sequence can be balanced and at what position (index)

    for example the sequence (1,1,1,2,1) can be balanced at position (index) 4 because 1+1+1=2+1 

    i need to determine if it can be balanced and determine the position 

    Dim isBalanced As Boolean = True
            Dim sum As Integer
            Dim x As Integer
    
            For x = listbox.items(0) To ListBox1.Items.Count - 1
                sum += x
                If ListBox1.Items(x) Mod 2 = 0 Then
                    isBalanced = True
                    Resultabel.Text = "Balanced"
    
                Else isBalanced = False
    
                    Resultabel.Text = "Not balanced "
                End If
    
            Next

    i think this is how to determine if it can be balanced

    but i still don't know how to determine the position it can be balanced at 

    Sunday, December 10, 2017 10:13 PM

Answers

  • i think this is how to determine if it can be balanced

    but i still don't know how to determine the position it can be balanced at 

    You need to consider where the list box data is coming from, because that's where the calculation will occur.  The list box is just for showing the results.

    For instance, if the list box is populated from an array of integers, you could create a loop that iterates over the array members, keeping two running totals: one from the start of the array and one from the end.  At each iteration you would check to see which total needs something added to it, and add the next sequential value to a total. When the totals are equal, check that all array elements have been included.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ara() As Integer = {1, 1, 1, 2, 1}
    
            Dim I As Integer = 0
            Dim J As Integer = ara.Count - 1
            Dim total1 As Integer = ara(I)
            Dim total2 As Integer = ara(J)
            Do
                If total1 < total2 Then
                    'Move lower boundary and add
                    If J = I + 1 Then
                        MsgBox("Array cannot be balanced")
                        Exit Do
                    End If
                    I += 1
                    total1 += ara(I)
                ElseIf total1 > total2 Then
                    'Move Upper boundary and add
                    If J = I + 1 Then
                        MsgBox("Array cannot be balanced")
                        Exit Do
                    End If
                    J -= 1
                    total2 += ara(J)
                Else 'totals are equal - possible balance
                    If J = I + 1 Then
                        MsgBox("Array is balanced at " & (J + 1).ToString)
                        Exit Do
                    Else 'More adds needed.
                        If ara(I) <= ara(J) Then
                            I += 1
                            total1 += ara(I)
                        ElseIf ara(J) > ara(I) Then
                            J -= 1
                            total2 += ara(J)
                        End If
                    End If
                End If
            Loop
    
        End Sub

    /Edit

    I suspect that the fail check needs to be included in the adds for the balanced but incomplete. That is left as an exercise for the reader.


    • Edited by Acamar Sunday, December 10, 2017 11:01 PM add
    • Marked as answer by FarahLadki Sunday, December 10, 2017 11:43 PM
    Sunday, December 10, 2017 10:52 PM

All replies

  • So you have to do a weighted balance?

    Ever hear the term balance the momentum?

    Or what goes in must come out?

    And you know somebody could lift the world if given a lever long enough?

    If that is the balance you want you use the principal that the weight multiplyed by the distance (or any single factor) from the point of rotation (the lever arm distance) (or any other factor) equals the angular momentum (amount of rotating force). Or the weighted distance.

        r1 = (w * L)

    And if you add all the weights * distance on one side of the rotation point you have the total force downward on one side of the fulcrum teeter-totter.


        rTotalRight = r1 + r2 ... = (w1 * L1) + (w2 * L2) ...


    Finally balance the forces left and right


        rTotalLeft = rTotalRight

    Now you can solve that equation given one unknown.


    Sunday, December 10, 2017 10:35 PM
  • i think this is how to determine if it can be balanced

    but i still don't know how to determine the position it can be balanced at 

    You need to consider where the list box data is coming from, because that's where the calculation will occur.  The list box is just for showing the results.

    For instance, if the list box is populated from an array of integers, you could create a loop that iterates over the array members, keeping two running totals: one from the start of the array and one from the end.  At each iteration you would check to see which total needs something added to it, and add the next sequential value to a total. When the totals are equal, check that all array elements have been included.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ara() As Integer = {1, 1, 1, 2, 1}
    
            Dim I As Integer = 0
            Dim J As Integer = ara.Count - 1
            Dim total1 As Integer = ara(I)
            Dim total2 As Integer = ara(J)
            Do
                If total1 < total2 Then
                    'Move lower boundary and add
                    If J = I + 1 Then
                        MsgBox("Array cannot be balanced")
                        Exit Do
                    End If
                    I += 1
                    total1 += ara(I)
                ElseIf total1 > total2 Then
                    'Move Upper boundary and add
                    If J = I + 1 Then
                        MsgBox("Array cannot be balanced")
                        Exit Do
                    End If
                    J -= 1
                    total2 += ara(J)
                Else 'totals are equal - possible balance
                    If J = I + 1 Then
                        MsgBox("Array is balanced at " & (J + 1).ToString)
                        Exit Do
                    Else 'More adds needed.
                        If ara(I) <= ara(J) Then
                            I += 1
                            total1 += ara(I)
                        ElseIf ara(J) > ara(I) Then
                            J -= 1
                            total2 += ara(J)
                        End If
                    End If
                End If
            Loop
    
        End Sub

    /Edit

    I suspect that the fail check needs to be included in the adds for the balanced but incomplete. That is left as an exercise for the reader.


    • Edited by Acamar Sunday, December 10, 2017 11:01 PM add
    • Marked as answer by FarahLadki Sunday, December 10, 2017 11:43 PM
    Sunday, December 10, 2017 10:52 PM
  • hi thank you it worked

    i just need some clarifications  

    what do you mean by ara(I) and ara (J) you mean like sum of I and sum of J ?

    i'm using a textbox the user will manually insert the numbers in a textbox one by one and then it will be shown in the listbox 

    why did you set J = I + 1 

    also if you can explain this part 

    If ara(I) <= ara(J) Then

                            I += 1

                            total1 += ara(I)

                        ElseIf ara(J) > ara(I) Then

                            J -= 1

                            total2 += ara(J)

                        End If


    • Edited by FarahLadki Monday, December 11, 2017 12:13 AM
    Monday, December 11, 2017 12:05 AM
  • what do you mean by ara(I) and ara (J) you mean like sum of I and sum of J ?

    ara(I) is the value of the array element at position I.  So ara(3) would be equal to 2 (array positions start at 0).

    i'm using a textbox the user will manually insert the numbers in a textbox one by one and then it will be shown in the listbox 

    You will need to parse the textbox (probably using Split), convert the substrings into integers, create an array, and insert the integers into the array.  Then you can do the calculation.  Then you can display the array in the listbox.

    why did you set J = I + 1

    You indicated that the answer was 4.   Array positions start at 0 so the match was detected when J was 3.  If you want 4 reported as the result, you need to add 1.  In VB .Net terms, the answer should be 3. It depends on your definition of 'position'.

    also if you can explain this part 

    You are correct.  I believe that should be

    If ara(I + 1) <= ara(J + 1) Then
            I += 1
            total1 += ara(I)

    ElseIf ara(I + 1) > ara(J - 1) Then

    The logic is "If the totals are equal but the balance point has not been found, move the pointer according to which value will result in the smallest change to its corresponding total (unless the values are equal, in which case just choose the first)".  That portion of code also needs to test for I-J overlapping, as mentioned.

    For your version, a different algorithm is:

    Choose a test balance point X starting at 1 and going up to the last position.
    Add up everything up to the element before X, and everything from X to the end (two totals).
    If those totals are equal, you have found it.
    If they aren't equal move X along by one, and repeat.
    If you get to the end and you haven't found a pair of equal totals, it won't balance. 

    Monday, December 11, 2017 12:39 AM