0

I am extremely new to VB. I am taking up an intro to VB course and I'm currently stuck with a small problem in my code. I have managed to get most of it to work, however I can't seem to figure out the remaining error that I am facing.

As attached in the screenshot below, I am unsure of how to get rid of the "0 is NOT a prime number" line. I have also attached my code below.

'Variable declaration
        Dim n As Integer
        Console.WriteLine("Please enter number of positive integers: ")
        n = Console.ReadLine()

        'Array declaration
        Dim IntegerArray(n) As Integer
        For j = 0 To n - 1
            Console.WriteLine("Please enter integer number " & (j + 1) & ": ")
            IntegerArray(j) = Console.ReadLine()
        Next j
        Console.WriteLine(vbCrLf)

        Dim i As Integer
        Dim Prime As Boolean = True

        'Check to ensure 1 is not prime, as it only has one positive divisor
        For Each number As Integer In IntegerArray
            If number = 1 Then
                Console.WriteLine(number & " is NOT a Prime Number")
            Else
                'Loop to check if number is divisible by any number starting from 2 to n-1
                For i = 2 To (number - 1)
                    If number Mod i = 0 Then
                        Prime = False
                        Exit For
                    End If
                    Prime = True
                Next i

                'Print sorted array along with results
                If Prime Then
                    Console.WriteLine(number & " is a Prime Number")
                Else
                    Console.WriteLine(number & " is NOT a Prime Number")
                End If
            End If
        Next
        Console.ReadKey()
    End Sub
End Module

Output screen 1

agnis_
  • 3
  • 1
  • Use `Dim IntegerArray(n - 1) As Integer`. In VB, when declaring an array, you pass the upper-bound, not the size/length of the array. So, when you use `Dim IntegerArray(n)`, the array actually contains `n + 1` elements (i.e., 6 elements in your example) and since you assign values only to `n` elements, the last element has a value of `0`. – ICloneable Sep 07 '20 at 03:51
  • Does this answer your question? [Size of array in Visual Basic?](https://stackoverflow.com/questions/506207/size-of-array-in-visual-basic) – ICloneable Sep 07 '20 at 03:52
  • Ahh! I see, I've changed it and it's all good and working now! Thank you so much! – agnis_ Sep 07 '20 at 04:00

1 Answers1

0

Turn on Option Strict. ReadLine returns a string. You need an Integer therefore you need a CInt() Of course your program will blow up if the user enters something other than an Integer but that is beyond the scope of your question. Option Strict would have alerted you to this problem.

An array in Visual Basic is declared ArrayName(UpperBound) The upper bound (highest index) of an array with 3 elements is index 2.

Dim IntegerArray(n) As Integer

Should be

Dim IntegerArray(n - 1) As Integer

You had an extra element in your array so this gets rid of that pesky 0 at the end of your print out.

Sort your array if desired before you start testing the values.

Array.Sort(IntegerArray)

Putting it all together...

Sub Main()
    'Variable declaration
    Dim n As Integer
    Console.WriteLine("Please enter number of positive integers: ")
    n = CInt(Console.ReadLine())

    'Array declaration
    Dim IntegerArray(n - 1) As Integer
    For j = 0 To n - 1
        Console.WriteLine("Please enter integer number " & (j + 1) & ": ")
        IntegerArray(j) = CInt(Console.ReadLine())
    Next j
    Console.WriteLine(vbCrLf)
    For Each num In IntegerArray
        Debug.Print(num.ToString)
    Next
    Dim i As Integer
    Dim Prime As Boolean = True
    Array.Sort(IntegerArray) 'Sort the array here
    'Check to ensure 1 is not prime, as it only has one positive divisor
    For Each number As Integer In IntegerArray
        If number = 1 Then
            Console.WriteLine(number & " is NOT a Prime Number")
        Else
            'Loop to check if number is divisible by any number starting from 2 to n-1
            For i = 2 To (number - 1)
                If number Mod i = 0 Then
                    Prime = False
                    Exit For
                End If
                Prime = True
            Next i

            If Prime Then
                Console.WriteLine(number & " is a Prime Number")
            Else
                Console.WriteLine(number & " is NOT a Prime Number")
            End If
        End If
    Next
    Console.ReadLine()
End Sub

You were very close.

Mary
  • 14,926
  • 3
  • 18
  • 27