|
-
Nov 15th, 2001, 01:33 PM
#1
Thread Starter
Lively Member
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.
-
Nov 15th, 2001, 01:37 PM
#2
-= B u g S l a y e r =-
VB Code:
Private Sub Command1_Click()
Dim y(200) As Integer
Dim counter As Integer
Dim i As Integer
'fill array
Do While counter <= 200
y(counter) = counter * Rnd
counter = counter + 1
Loop
'show whats in the array
For i = 0 To UBound(y)
Debug.Print y(i)
Next i
End Sub
-
Nov 15th, 2001, 01:40 PM
#3
Thread Starter
Lively Member
I get an overflow error when I try that. There must be something I am doing wrong (or missing).
-
Nov 15th, 2001, 01:41 PM
#4
-= B u g S l a y e r =-
-
Nov 15th, 2001, 01:42 PM
#5
Hyperactive Member
Originally posted by peet
VB Code:
Private Sub Command1_Click()
Dim y(200) As Integer
Dim counter As Integer
Dim i As Integer
'fill array
Do While counter <= 200
y(counter) = counter * Rnd
counter = counter + 1
Loop
'show whats in the array
For i = 0 To UBound(y)
Debug.Print y(i)
Next i
End Sub
Or a similar code
VB Code:
Dim y(200) as integer
Dim i as integer
For i = 0 to Ubound(y)
y(i) = i * Rnd
Next
There, you save 2 bytes of memory. 
** Posting for the sake of posting, havent posted anything for quite some time. **
-
Nov 15th, 2001, 01:42 PM
#6
Thread Starter
Lively Member
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
-
Nov 15th, 2001, 01:43 PM
#7
-= B u g S l a y e r =-
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 )
-
Nov 15th, 2001, 01:46 PM
#8
-= B u g S l a y e r =-
whats this line for
M = M + Y(counter)
and what is M ? and integer ?
-
Nov 15th, 2001, 01:48 PM
#9
-= B u g S l a y e r =-
VB Code:
Private Sub Command1_Click()
Dim y(200) As Integer
Dim counter As Integer
Dim i As Integer
Dim M As Integer
'fill array
Do While counter <= 200
Randomize Timer
y(counter) = Int((6 * Rnd) + 1)
counter = counter + 1
Loop
'show whats in the array
For i = 0 To UBound(y)
Debug.Print y(i)
Next i
End Sub
works fine... no errors ?
u try?
-
Nov 15th, 2001, 01:51 PM
#10
-= B u g S l a y e r =-
sorry forgot the M 
VB Code:
Private Sub Command1_Click()
Dim y(200) As Integer
Dim counter As Integer
Dim i As Integer
Dim M As Integer
'fill array
Do While counter <= 200
Randomize Timer
y(counter) = Int((6 * Rnd) + 1)
M = M + y(counter)
counter = counter + 1
Loop
'show whats in the array
For i = 0 To UBound(y)
Debug.Print y(i)
Next i
End Sub
still works without any errs
-
Nov 15th, 2001, 02:01 PM
#11
Thread Starter
Lively Member
subscript out of range is the error i get at line
Y(counter) = Int((6 * Rnd) + 1)
-
Nov 15th, 2001, 02:01 PM
#12
Thread Starter
Lively Member
though this is my y declaration
dim y() as integer
could that be the problem?
-
Nov 15th, 2001, 02:05 PM
#13
Thread Starter
Lively Member
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!
-
Nov 15th, 2001, 02:21 PM
#14
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|