Results 1 to 11 of 11

Thread: Help with array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with array

    This:
    vb.net Code:
    1. Dim arr() As SomeType
    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:
    vb.net Code:
    1. Dim arr(-1) As SomeType
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    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...

  4. #4
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    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:
    1. Dim rnd As New Random()
    2.        
    3. 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

  5. #5
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    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:
    1. Dim rnd as New Random()
    2.  
    3. intNumArray(i) = rnd.Next(1, 100)
    Remember to click on the scales to the left and rep me if I helped

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    Re: Help with array

    Quote Originally Posted by The Fire Snake View Post
    Oh, from your code it looks like you want a random number between 1 and 99. So do the following:
    vb Code:
    1. Dim rnd as New Random()
    2.  
    3. 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.


    dbasnett
    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

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    Re: Help with array

    Quote Originally Posted by dbasnett View Post
    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...

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: Help with array

    Quote Originally Posted by CollinMcGuire View Post
    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
  •  



Click Here to Expand Forum to Full Width