Results 1 to 14 of 14

Thread: array problems

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Canada
    Posts
    69

    array problems

    Hi, I am probably stupid for asking this, but if I have an array and I want to add values into each index. Like this...

    do while counter <= 200
    y(counter) = whatever
    counter = counter + 1
    loop

    But it doesn't work. Basically I want to put the answer of a math question into each of the 200 index for the array. I hope this makes sense, and it is probably obvious.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim y(200) As Integer
    3.     Dim counter As Integer
    4.     Dim i As Integer
    5.    
    6.     'fill array
    7.     Do While counter <= 200
    8.         y(counter) = counter * Rnd
    9.         counter = counter + 1
    10.     Loop
    11.    
    12.     'show whats in the array
    13.     For i = 0 To UBound(y)
    14.         Debug.Print y(i)
    15.     Next i
    16. End Sub
    -= a peet post =-

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Canada
    Posts
    69
    I get an overflow error when I try that. There must be something I am doing wrong (or missing).

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    uh.. at what line ?
    -= a peet post =-

  5. #5
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485
    Originally posted by peet
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim y(200) As Integer
    3.     Dim counter As Integer
    4.     Dim i As Integer
    5.    
    6.     'fill array
    7.     Do While counter <= 200
    8.         y(counter) = counter * Rnd
    9.         counter = counter + 1
    10.     Loop
    11.    
    12.     'show whats in the array
    13.     For i = 0 To UBound(y)
    14.         Debug.Print y(i)
    15.     Next i
    16. End Sub
    Or a similar code
    VB Code:
    1. Dim y(200) as integer
    2.   Dim i as integer
    3.  
    4.   For i = 0 to Ubound(y)
    5.     y(i) = i * Rnd
    6.   Next
    There, you save 2 bytes of memory.

    ** Posting for the sake of posting, havent posted anything for quite some time. **

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Canada
    Posts
    69
    your code doesn't cause an error, here is mine that causes the error

    Do While counter <= A
    Randomize
    Y(counter) = Int((6 * Rnd) + 1) 'make a random number
    M = M + Y(counter)
    counter = counter + 1
    Loop

    a is equal to 200

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    and are u using exact the same code as in the sample? or do u have any other calculations that might explain the overflow (div by zero or something unplecant like that )
    -= a peet post =-

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    whats this line for

    M = M + Y(counter)

    and what is M ? and integer ?
    -= a peet post =-

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim y(200) As Integer
    3.     Dim counter As Integer
    4.     Dim i As Integer
    5.     Dim M As Integer
    6.      
    7.  
    8.     'fill array
    9.     Do While counter <= 200
    10.         Randomize Timer
    11.         y(counter) = Int((6 * Rnd) + 1)
    12.         counter = counter + 1
    13.     Loop
    14.  
    15.     'show whats in the array
    16.     For i = 0 To UBound(y)
    17.         Debug.Print y(i)
    18.     Next i
    19. End Sub

    works fine... no errors ?

    u try?
    -= a peet post =-

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    sorry forgot the M

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim y(200) As Integer
    3.     Dim counter As Integer
    4.     Dim i As Integer
    5.     Dim M As Integer
    6.      
    7.  
    8.     'fill array
    9.     Do While counter <= 200
    10.         Randomize Timer
    11.         y(counter) = Int((6 * Rnd) + 1)
    12.         M = M + y(counter)
    13.         counter = counter + 1
    14.     Loop
    15.  
    16.     'show whats in the array
    17.     For i = 0 To UBound(y)
    18.         Debug.Print y(i)
    19.     Next i
    20. End Sub

    still works without any errs
    -= a peet post =-

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Canada
    Posts
    69
    subscript out of range is the error i get at line

    Y(counter) = Int((6 * Rnd) + 1)

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Canada
    Posts
    69
    though this is my y declaration

    dim y() as integer

    could that be the problem?

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Location
    Canada
    Posts
    69
    ya, your code works fine, but my other one doesn't. I don't know why, I guess i'll just keep working on it. It must be something somewhere else. Thanks!

  14. #14
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485
    Originally posted by CarlPerkins
    though this is my y declaration

    dim y() as integer

    could that be the problem?
    If you dimed y() means it has no array.

    Do a
    Dim y(200) as Integer

    or a (if you've already dim y() as integer earlier in the code)
    ReDim y(200) as Integer

    or a (if you've already dim y() as integer earlier in the code)
    ReDim Preserve y(200) as Integer

    incase you already have values in y

    Subscript out of range means you are accessing the index of a array which is not defined or as it speaks, out of range.

    For eg:
    y(200) = y(0) to y(200). y(201) is out of range.

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