|
-
Apr 14th, 2010, 08:33 PM
#1
Thread Starter
Junior Member
Help with array
Hi I am making an application that randomly generates numbers. I then have to find out if those numbers are even or odd. Then place them in the corresponding array ( EvenNumbers() or OddNumbers() ). Then display the random numbers array then the even and odd numbers array.
I am up to the point where I have the generated numbers and can tell if they are even or odd, but can't get them into an array.
I am trying to do it like this....
Code:
If intNumArray(i) mod 2 = 0 then
intTemp = intNumArray(i)
EvenNumbers(i) = intTemp
else
intTemp = intNumArray(i)
OddNumbers(i) = intTemp
But I am getting an error that says I am using the EvenNumbers() and OddNumbers() variables before they have a value. But I what does that mean since I am obviously setting them equal to something?
-
Apr 14th, 2010, 09:11 PM
#2
Re: Help with array
This:declares an array variable but it does not create an array, therefore you cannot get or set elements in the array because it doesn't exist. To create an array you have to specify its size. You don't know what the final size of your array will be so you should start by creating an empty array:Specifying an upper bound of -1 means no elements, because the first element is at index 0. You can then use ReDim Preserve to enlarge your array by 1 element time you want to add to it.
That's not the way you'd do it in the real world but, if you're just learning arrays, that's how they want you to do it.
-
Apr 15th, 2010, 02:38 PM
#3
Thread Starter
Junior Member
Re: Help with array
Code:
Dim intIndex, i, intTemp, intItem As Integer
Dim intLow As Integer = 1 '// Doesn't allow anything over 99 to be used
Dim intHigh As Integer = 99 '// Doesn't allow anything over 99 to be used
Randomize() '// Randomize function
For i = 0 To intNumArray.Length - 1 '// Will run the loop for every element in the array
intIndex += 1 '// Adds 1 to intItem every time the loop is run
intNumArray(i) = (intHigh - intLow) * Rnd() '// Generates a random number
If intNumArray(i) Mod 2 = 0 Then
intTemp = intNumArray(i)
intEvenNumbers(intTemp) = intTemp
ElseIf intTemp Mod 2 <> 0 Then
intTemp = intNumArray(i)
intOddNumbers(intTemp) = intTemp
End If
lstOutput.Items.Add(intIndex & vbTab & intNumArray(i)) '// Outputs the index number and the random number
Next i
lstoutput.items.add("The Even Numbers are : " & intEvenNumbers(inttemp) & vbcrlf & "The odd numbers are : " & intOddNumbers(inttemp))
Here is the source code of what I came up with. Whats wrong with it? I run it and when i hit the generate button it just closes out...
-
Apr 15th, 2010, 03:09 PM
#4
Hyperactive Member
Re: Help with array
You are not generating a random number in the right way. Remove your Randomize and Rnd statements and do the following:
vb Code:
Dim rnd As New Random() Dim randomNumber as Integer = rnd.Next()
By the way, does your random number need to be between a certain range?
Remember to click on the scales to the left and rep me if I helped 
-
Apr 15th, 2010, 03:13 PM
#5
Hyperactive Member
Re: Help with array
Oh, from your code it looks like you want a random number between 1 and 99. So do the following:
vb Code:
Dim rnd as New Random() intNumArray(i) = rnd.Next(1, 100)
Remember to click on the scales to the left and rep me if I helped 
-
Apr 15th, 2010, 03:51 PM
#6
Re: Help with array
Code:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'since this looks like homework I left the comments off
Dim theEvens(0) As Integer
Dim theOdds(0) As Integer
Dim r As New Random
'generate 10 random numbers between 1 and 100, place them in correct array
For z As Integer = 1 To 10
Dim i As Integer = r.Next(1, 101)
If (i And 1) = 1 Then
'odd
theOdds(theOdds.Length - 1) = i
Array.Resize(theOdds, theOdds.Length + 1)
Else
'even
theEvens(theEvens.Length - 1) = i
Array.Resize(theEvens, theEvens.Length + 1)
End If
Next
Array.Resize(theOdds, theOdds.Length - 1)
Array.Resize(theEvens, theEvens.Length + 1)
Stop '
End Sub
-
Apr 15th, 2010, 05:57 PM
#7
Thread Starter
Junior Member
Re: Help with array
 Originally Posted by The Fire Snake
Oh, from your code it looks like you want a random number between 1 and 99. So do the following:
vb Code:
Dim rnd as New Random()
intNumArray(i) = rnd.Next(1, 100)
Random number is fine the way I have it, when I take our the For...Next loop it will generate my numbers fine. It's a matter of using modulus division to see if it's even or odd then adding it to the corresponding array.
I would use your method, but we didn't learn arrays like that so 1) I don't understand it that well, an in detail explanation would be cool 2) I don't think the teacher would believe I made it if I couldn't fully explain it
-
Apr 15th, 2010, 06:12 PM
#8
Re: Help with array
If you are trying to be a programmer, then try to explain it, and I will help. If this is just a class you are taking in pursuit of another career then it isn't worth your time.
-
Apr 15th, 2010, 06:18 PM
#9
Thread Starter
Junior Member
Re: Help with array
 Originally Posted by dbasnett
If you are trying to be a programmer, then try to explain it, and I will help. If this is just a class you are taking in pursuit of another career then it isn't worth your time.
I am taking the class to see if I'd be interested in something similar. But sometimes it helps to have someone knowledgeable explain it, you know?
I understand it for the most part, I really just wanted your logic behind it and why you used that method...
-
Apr 15th, 2010, 07:36 PM
#10
Re: Help with array
Randomize / rnd are old, you should use Random.
I like resize over redim in most cases. It just appears more to the point, but that is just me.
My test for odd / even is, I think, more efficient. The compiler might be smart enough not to use division in this case, but why leave it to chance.
Because I made it as simple as I thought prudent, I had to add the resizes at the end to get rid of the empty array entry.
-
Apr 15th, 2010, 10:28 PM
#11
Hyperactive Member
Re: Help with array
 Originally Posted by CollinMcGuire
Random number is fine the way I have it, when I take our the For...Next loop it will generate my numbers fine. It's a matter of using modulus division to see if it's even or odd then adding it to the corresponding array.
What do you mean it is fine the way you have it? I have provided you the correct way to generate random numbers in VB.Net. Up to you if you want to use it or not.
Remember to click on the scales to the left and rep me if I helped 
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
|