Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 4th, 2012, 01:41 AM
 m712
Registered User

Join Date: Oct 2012
Posts: 3
Time spent in forums: 19 m 37 sec
Reputation Power: 0

I'm trying to add two arrays together and getting error code is like this:
For x = 0 to 180
For y = 0 to 180
Total = Array 1(1,x,y) + Array2 (1,x,y)
Next x
Next y

I get a subscript out of range error

I want to add each element of array1 to its corresponding element
In array2, then have a variable "total" that is the sum of all the elements that are the sum of those two
Arrays.

#2
October 4th, 2012, 03:40 PM
 couttsj
Contributing User

Join Date: Aug 2011
Posts: 273
Time spent in forums: 4 Days 2 h 56 sec
Reputation Power: 44
It should not work at all. For a starter, your for/next loops are nested wrong. It should read :

For x = 0 to 180
For y = 0 to 180
Total = Array1(1, x, y) + Array2(1, x, y)
Debug.Print Array1(1, x, y), Array2(1, x, y), Total
Next y
Next x

If you want to save the total in Array2, you would have to add a fourth dimension to it.

J.A. Coutts

#3
October 19th, 2012, 12:36 PM
 Incidentals
Contributing User

Join Date: Nov 2010
Posts: 89
Time spent in forums: 1 Day 4 h 23 m 19 sec
Reputation Power: 157
Your problem stems from youtr looping as suggested above

the x is 0 and the y loop is 0 the x moves throu its values and ends 1 larger than the loop value

this means when you try to look at the nexty value you are looking too far and have an out of bounds error

simply change the order of the next statments

and as long as you have dimensioned the arrays correctly you should have no problems

here to help

 Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Adding two arrays