none
array and listboxes RRS feed

  • Question

  • Revise the Sample Problem by reading ten (10) integer values and storing these values in an array. Determine and display the number of prime numbers entered.
    What is the error in my code?
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim MyArray(9), ctr As Integer
            Dim j, k As Integer
            For ctr = 0 To 9
                MyArray(ctr) = InputBox("Input number :")
                ListBox1.Items.Add(MyArray(ctr))
            Next

            For j = 2 To (MyArray(ctr) - 1)
                If MyArray(ctr) Mod j = 0 Then
                    k = k + 1
                End If
            Next
            TextBox1.Text = k
        End Sub
    End Class
    Monday, November 27, 2017 11:28 AM

All replies

  • Homework, because this kind of assignment are typical for it. 


    Success
    Cor

    Monday, November 27, 2017 12:30 PM
  • Hi

    As Cor says, homework. We tend to try and not provide copy/paste answers for homework.

    However, as you have made a fair attempt to write the code, here is one or two pointers.

    Your For....Next loop is not really suitable as it is saying starting with the second of the user input numbers, but you really want to include all the user inputs. Then, you should rework the upper count for the For...Next loop - perhaps use the MyArray.Length instead. Next, use the For...Next loop counter instead of Ctl (you can't re-use variables in that way), to cycle through all the values in the MyArray list.

    Lastly, please use a code block (from toolbar) to post code.


    Regards Les, Livingston, Scotland

    Monday, November 27, 2017 1:24 PM
  • Hi toteeh,

    I modify your code and you can try the following code:

    Dim MyArray(9), ctr As Integer
            Dim i, j, k As Integer
            For ctr = 0 To 9
                MyArray(ctr) = InputBox("Input number :")
                ListBox1.Items.Add(MyArray(ctr))
            Next
            For i = 0 To ListBox1.Items.Count - 1
                For j = 2 To (ListBox1.Items(i) - 1)
                    If ListBox1.Items(i) Mod j = 0 Then
                        k = k + 1
                        Exit For
                    End If
                Next
                TextBox1.Text = k
            Next

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Bob Ding Thursday, November 30, 2017 9:59 AM
    Tuesday, November 28, 2017 5:38 AM
    Moderator
  • What is the error in my code?

    You should always describe why you think the code is in error.   What input are you using for your test, what result did you expect to get, and what result did you actually get?

    You should describe the procedure for testing whether a given number is prim or not.  If you can describe it in plain language you can probably code it almost directly from the description.  https://en.wikipedia.org/wiki/Prime_number#Testing_primality_and_integer_factorization

    Your test for a prime number needs to be inside the loop where you get the user input.  Currently you are only doing the test once after that loop finishes.

    Tuesday, November 28, 2017 6:10 AM