Results 1 to 8 of 8

Thread: Help with User Supplied Number Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    5

    Help with User Supplied Number Array

    Hello everyone,
    If anyone has some time to answer a question I have, I'm working on a problem for school and I thought I had this answered, but it just gives me errors. This is a console app that will ask a user to type 20 numbers between 10 and 100. The program reads the number making sure its between 10 and 100 and then checks to see if its a duplicate. If its not a duplicate it will be added to the array. Can anyone tell me what I've done wrong? Here is the code I have so far.Thank you for any help in advance.

    Private Duplicate As Boolean = False

    Sub Main()

    Dim intNumberArray As Integer() = New Integer(19) {}
    Dim index As Integer = 0
    Dim intCount As Int16 = 0
    Dim intNewNum As Integer

    Do While intNumberArray(index) < 20
    Console.WriteLine("Please enter a number from 10 to 100")
    intNewNum = Val(Console.ReadLine)

    Select Case intNewNum
    Case 10 To 100

    For index = 0 To intNumberArray.GetUpperBound(0)

    If intNewNum = intNumberArray(index) Then ' its a duplicate
    Duplicate = True

    Console.Writeline("This is a duplicate, please try again.")


    ElseIf Duplicate = False Then

    intNewNum += intNumberArray(index)



    End If


    Next


    Case Else
    Console.WriteLine("That number is not from 10 to 100, please enter a _ correct value")

    End Select


    Loop

    Console.Write("The numbers are: ")
    Console.Write(intArray(index) & " ")

    End Sub

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Help with User Supplied Number Array

    What is your error? Why are you using this loop:
    VB Code:
    1. Do While intNumberArray(index) < 20

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Help with User Supplied Number Array

    here i put some code for you.
    VB Code:
    1. Sub Main()
    2.  
    3.         Dim intNumberArray(19) As Integer
    4.         'Dim index As Integer = 0
    5.         'Dim intCount As Integer = 0 ' integer datatype is more faster.
    6.         Dim intNewNum As Integer
    7.  
    8.         For i As Integer = 0 To intNumberArray.GetUpperBound(0)
    9.             Console.WriteLine("Please enter a number from 10 to 100")
    10. reapit:     intNewNum = CInt(Console.ReadLine)
    11.             Select Case intNewNum
    12.                 Case 10 To 100
    13.                     'loop throug each element in the array and check for the duplicate.
    14.                     For j As Integer = 0 To intNumberArray.GetUpperBound(0)
    15.                         If intNewNum = intNumberArray(j) Then ' its a duplicate
    16.                             Console.WriteLine("This is a duplicate, please try again.")
    17.                             GoTo reapit
    18.                         End If
    19.                     Next
    20.                     'if duplicate is not found add the number to the array.
    21.                     intNumberArray(i) = intNewNum
    22.                 Case Else
    23.                     Console.WriteLine("That number is not from 10 to 100, please enter a _ correct value")
    24.                     GoTo reapit
    25.             End Select
    26.         Next i
    27.  
    28.         'Console.Write("The numbers are: ")
    29.         'Console.Write(intArray(index) & " ")
    30.  
    31.     End Sub

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Help with User Supplied Number Array

    Here you go
    VB Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim numbers(19) As Integer
    5.         Dim currentNumber, counter As Integer
    6.         Dim duplicate As Boolean = False
    7.         'Set the counter to 0
    8.         counter = 0
    9.         'Ask user to enter a number
    10.         Console.Write("Enter a number: ")
    11.         'Loop until hte numbers array is filled with good values
    12.         While counter < numbers.Length
    13.             Try
    14.                 'Read the number entered by user
    15.                 currentNumber = Integer.Parse(Console.ReadLine)
    16.                 'Test the number to see if it's within allowable range
    17.                 If currentNumber >= 10 AndAlso currentNumber <= 100 Then
    18.                     'Number is in range
    19.                     'Now check for duplicates
    20.                     For i As Integer = 0 To numbers.GetUpperBound(0)
    21.                         If numbers(i) = currentNumber Then
    22.                             duplicate = True
    23.                             Exit For
    24.                         End If
    25.                     Next
    26.                     If Not duplicate Then
    27.                         'Good number... Add it to the array
    28.                         numbers(counter) = currentNumber
    29.                         'Increment the counter
    30.                         counter += 1
    31.                         If counter < numbers.Length Then
    32.                             'Ask user to enter another number
    33.                             Console.Write("Enter a number: ")
    34.                         Else
    35.                             Console.WriteLine()
    36.                         End If
    37.                     Else
    38.                         'reset duplicate flag
    39.                         duplicate = False
    40.                         Console.WriteLine("Duplicate number detected!")
    41.                         Console.Write("Enter a number: ")
    42.                     End If
    43.                 Else
    44.                     'Number is out of range
    45.                     Console.WriteLine("Please enter a number between 10 and 100 only!")
    46.                     Console.Write("Enter a number: ")
    47.                 End If
    48.             Catch ex As Exception
    49.                 Console.WriteLine("Please enter numeric values only!")
    50.                 Console.Write("Enter a number: ")
    51.             End Try
    52.         End While
    53.  
    54.         'Now display the numbers
    55.         Console.WriteLine("Here is the list of numbers you entered:")
    56.         For counter = 0 To numbers.GetUpperBound(0)
    57.             Console.Write(numbers(counter) & " ")
    58.         Next
    59.         Console.WriteLine()
    60.         Console.WriteLine()
    61.         Console.Write("Press any key to exit...")
    62.         Console.ReadLine()
    63.     End Sub
    64.  
    65. End Module
    Edited: add in checking for duplicate values
    Last edited by stanav; Nov 8th, 2006 at 04:31 PM.

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Help with User Supplied Number Array

    @dan463va, Sorry if I sound too bitter, though I mean not, you should get a book on programming language concepts and go through it entirely.

    @VBDT and dan463va, this loop:
    VB Code:
    1. For j As Integer = 0 To intNumberArray.GetUpperBound(0)
    doesn't make sense since this is checking the entered value against not allocated array fields.

    @dan463va, the code you are using to display the numbers will not work. This I think, give to you from VBF as a homework .

    VB Code:
    1. Sub Main() 'No need for Duplicate variable
    2.         Dim intNumberArray As Integer()
    3.         Dim index As Integer = 0
    4.         'Dim intCount As Int16 = 0
    5.         Dim intNewNum As Integer
    6.  
    7.         Do While index < 20
    8.             ReDim Preserve intNumberArray(index)
    9.             Console.WriteLine("Please enter a number from 10 to 100")
    10.             intNewNum = Convert.ToInt32(Console.ReadLine)
    11.  
    12.             Select Case intNewNum
    13.                 Case 10 To 100
    14.                     For intCount As Integer = 0 To index - 1
    15.  
    16.                         If intNewNum = intNumberArray(intCount) And index <> 0 Then ' its a duplicate
    17.                             Console.WriteLine("This is a duplicate, please try again.")
    18.                             Exit Select
    19.                         End If
    20.                     Next intCount
    21.  
    22.                     intNumberArray(index) = intNewNum
    23.                     index = index + 1
    24.  
    25.                 Case Else
    26.                     Console.WriteLine("That number is not from 10 to 100, please enter a _ correct value")
    27.  
    28.             End Select
    29.         Loop
    30.  
    31.         Console.Write("The numbers are: ")
    32.  
    33.         For index = 0 To intNumberArray.GetUpperBound(0)
    34.             Console.Write(intNumberArray(index) & " ") 'now you know why your code wont work.
    35.         Next
    36.         Console.ReadLine()
    37.     End Sub
    Last edited by Harsh Gupta; Nov 8th, 2006 at 05:08 PM.
    Show Appreciation. Rate Posts.

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Help with User Supplied Number Array

    In my opinion it is not big dill to loop through 20 items! Honestly I would use a collection instead of integer array. Harsh Gupta in your example there is a problem. Let say the number entered by the user is duplicate then according to your code it exits the select case and ReDims the array leaving the previous array filled empty..

  7. #7
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Help with User Supplied Number Array

    Quote Originally Posted by VBDT
    In my opinion it is not big dill to loop through 20 items! Honestly I would use a collection instead of integer array. Harsh Gupta in your example there is a problem. Let say the number entered by the user is duplicate then according to your code it exits the select case and ReDims the array leaving the previous array filled empty..
    Yeah, even I would have used collections for the same thing.

    As for in my example, sorry, didn't come to my mind, but at any point, no index if the array is empty, it will just redim the array with the same index value again. Though I agree, even this is a bad style of programming.
    Show Appreciation. Rate Posts.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Help with User Supplied Number Array

    Well, at least we have thoroughly answered the first question: Whether anyone has time to help with this
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width