|
-
Nov 8th, 2006, 03:38 PM
#1
Thread Starter
New Member
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
-
Nov 8th, 2006, 04:13 PM
#2
Re: Help with User Supplied Number Array
What is your error? Why are you using this loop:
VB Code:
Do While intNumberArray(index) < 20
-
Nov 8th, 2006, 04:15 PM
#3
Re: Help with User Supplied Number Array
here i put some code for you.
VB Code:
Sub Main()
Dim intNumberArray(19) As Integer
'Dim index As Integer = 0
'Dim intCount As Integer = 0 ' integer datatype is more faster.
Dim intNewNum As Integer
For i As Integer = 0 To intNumberArray.GetUpperBound(0)
Console.WriteLine("Please enter a number from 10 to 100")
reapit: intNewNum = CInt(Console.ReadLine)
Select Case intNewNum
Case 10 To 100
'loop throug each element in the array and check for the duplicate.
For j As Integer = 0 To intNumberArray.GetUpperBound(0)
If intNewNum = intNumberArray(j) Then ' its a duplicate
Console.WriteLine("This is a duplicate, please try again.")
GoTo reapit
End If
Next
'if duplicate is not found add the number to the array.
intNumberArray(i) = intNewNum
Case Else
Console.WriteLine("That number is not from 10 to 100, please enter a _ correct value")
GoTo reapit
End Select
Next i
'Console.Write("The numbers are: ")
'Console.Write(intArray(index) & " ")
End Sub
Last edited by VBDT; Nov 8th, 2006 at 04:25 PM.
-
Nov 8th, 2006, 04:18 PM
#4
Re: Help with User Supplied Number Array
Here you go
VB Code:
Module Module1
Sub Main()
Dim numbers(19) As Integer
Dim currentNumber, counter As Integer
Dim duplicate As Boolean = False
'Set the counter to 0
counter = 0
'Ask user to enter a number
Console.Write("Enter a number: ")
'Loop until hte numbers array is filled with good values
While counter < numbers.Length
Try
'Read the number entered by user
currentNumber = Integer.Parse(Console.ReadLine)
'Test the number to see if it's within allowable range
If currentNumber >= 10 AndAlso currentNumber <= 100 Then
'Number is in range
'Now check for duplicates
For i As Integer = 0 To numbers.GetUpperBound(0)
If numbers(i) = currentNumber Then
duplicate = True
Exit For
End If
Next
If Not duplicate Then
'Good number... Add it to the array
numbers(counter) = currentNumber
'Increment the counter
counter += 1
If counter < numbers.Length Then
'Ask user to enter another number
Console.Write("Enter a number: ")
Else
Console.WriteLine()
End If
Else
'reset duplicate flag
duplicate = False
Console.WriteLine("Duplicate number detected!")
Console.Write("Enter a number: ")
End If
Else
'Number is out of range
Console.WriteLine("Please enter a number between 10 and 100 only!")
Console.Write("Enter a number: ")
End If
Catch ex As Exception
Console.WriteLine("Please enter numeric values only!")
Console.Write("Enter a number: ")
End Try
End While
'Now display the numbers
Console.WriteLine("Here is the list of numbers you entered:")
For counter = 0 To numbers.GetUpperBound(0)
Console.Write(numbers(counter) & " ")
Next
Console.WriteLine()
Console.WriteLine()
Console.Write("Press any key to exit...")
Console.ReadLine()
End Sub
End Module
Edited: add in checking for duplicate values
Last edited by stanav; Nov 8th, 2006 at 04:31 PM.
-
Nov 8th, 2006, 04:27 PM
#5
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:
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:
Sub Main() 'No need for Duplicate variable
Dim intNumberArray As Integer()
Dim index As Integer = 0
'Dim intCount As Int16 = 0
Dim intNewNum As Integer
Do While index < 20
ReDim Preserve intNumberArray(index)
Console.WriteLine("Please enter a number from 10 to 100")
intNewNum = Convert.ToInt32(Console.ReadLine)
Select Case intNewNum
Case 10 To 100
For intCount As Integer = 0 To index - 1
If intNewNum = intNumberArray(intCount) And index <> 0 Then ' its a duplicate
Console.WriteLine("This is a duplicate, please try again.")
Exit Select
End If
Next intCount
intNumberArray(index) = intNewNum
index = index + 1
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: ")
For index = 0 To intNumberArray.GetUpperBound(0)
Console.Write(intNumberArray(index) & " ") 'now you know why your code wont work.
Next
Console.ReadLine()
End Sub
Last edited by Harsh Gupta; Nov 8th, 2006 at 05:08 PM.
-
Nov 8th, 2006, 04:44 PM
#6
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..
Last edited by VBDT; Nov 8th, 2006 at 04:48 PM.
-
Nov 8th, 2006, 05:11 PM
#7
Re: Help with User Supplied Number Array
 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.
-
Nov 8th, 2006, 06:03 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|