3 Replies - 465 Views - Last Post: 31 August 2013 - 03:04 PM Rate Topic: -----

#1 ozfader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-August 13

Search an array with "While" loop

Posted 31 August 2013 - 01:55 AM

Hi, I feel a little sheepish about this one because I know it should be easy, but I've been banging my head trying to find the error. Hasten to add I'm a noobie to VB.

I'm trying to search for a "favNum" from a randomly generated array of integers. Assignment asked to use a "While" loop but although I can get the array to print, it all comes unstuck at the loop.

Can anyone give me a clue please? Before I stick my head down the toilet!

Thanks...

Sub Main()
        Dim favNum As Integer
        Dim randomNum As New Random
        Dim i As Integer
        Dim array(99) As Integer
        Dim found As Boolean = False

        Console.WriteLine("What is your favourite number?" & vbNewLine)
        favNum = Console.ReadLine()
        Console.WriteLine("")

        Console.WriteLine("Okay, now press any key to generate 100 random numbers between 1 and 50" & vbNewLine)
        Console.ReadKey()

        For i = 0 To 99
            array(i) = randomNum.Next(1, 50)
            Console.Write(array(i) & vbNullChar)
        Next

        'Goes pear-shaped from here

        While (i <= 100) And (Not found)
            If i = favNum Then
                found = True
                Console.WriteLine("Congratulations! Your number wins...")

            End If
        End While

        If Not found Then
            Console.WriteLine("I'm sorry, your number wasn't successful.")
        End If

        Console.ReadKey()

    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Search an array with "While" loop

#2 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 102
  • View blog
  • Posts: 584
  • Joined: 01-October 08

Re: Search an array with "While" loop

Posted 31 August 2013 - 06:44 AM

Try it like this

Module Module1

    Dim prng As New Random
    Dim favnum As Integer

    Sub Main()
        Dim rn(99) As Integer
        Dim found As Boolean
        Do
            found = False
            favnum = getfav()
            If favnum = Integer.MinValue Then Exit Do
            Console.WriteLine("Okay, now press any key to generate 100 random numbers between 1 and 50" & vbNewLine)
            Console.ReadKey()
            For i As Integer = 0 To rn.Length - 1
                rn(i) = prng.Next(1, 51)
                If rn(i) = favnum Then found = True
            Next
            If found Then
                Console.WriteLine("Congratulations! Your number wins...")
            Else
                Console.WriteLine("I'm sorry, your number wasn't successful.")
            End If
        Loop
    End Sub

    Function getfav() As Integer
        Dim rv As Integer
        Do
            Console.WriteLine("What is your favourite number between 1 and 50? (x to exit)")
            Dim s As String = Console.ReadLine
            If s.ToLower = "x" Then
                rv = Integer.MinValue
                Exit Do
            ElseIf Integer.TryParse(s, rv) AndAlso rv >= 1 AndAlso rv <= 50 Then
                Exit Do
            Else
                'not a legal number and not x
            End If
        Loop
        Return rv
    End Function
End Module

Was This Post Helpful? 0
  • +
  • -

#3 ozfader  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-August 13

Re: Search an array with "While" loop

Posted 31 August 2013 - 01:48 PM

Appreciate your help dbasnett, but the assignment specifically asks for a solution using "For... Next...While" loops. I'm getting confused as to which variables to use within the "While" statement. Output is fine up to there, but solution is incorrect...

Posted Image

While i <= 50 And (Not found)
            If randomNum.Next(i) = favNum Then
                Console.WriteLine("" & vbCrLf)
                Console.WriteLine("Congratulations! Your number " & favNum & " wins...")
                found = True
            End If
        End While
        Console.WriteLine("" & vbCrLf)

        If Not found Then
            Console.WriteLine("I'm sorry, the number " & favNum & " wasn't successful.")
        End If


This post has been edited by andrewsw: 31 August 2013 - 02:01 PM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Search an array with "While" loop

Posted 31 August 2013 - 03:04 PM

You need to learn some basic debugging skills.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1