|
Forum Index : Microcontroller and PC projects : Confused by Dim...
| Author | Message | ||||
| mkopack73 Senior Member Joined: 03/07/2020 Location: United StatesPosts: 261 |
Hey gang, Ok, my CMM2 arrived today and I've already started digging in writing a couple little test programs... Did one to see how fast a simple bubble sort program is compared to my old C128 (it's LAUGHABLE... C-128 in fast mode takes on avg 3.5 sec to generate and sort 20 integers. CMM2 does it in 10ms!) I'm confused about 1 thing though... When using DIM to set up an array and initialize the array with values such as this: dim integer myarray(5) = (15,25,33,7) Why does the array size (5 in the above example) have to be 1 LESS than the number of items???) That seems VERY weird to me... Or is this because I didn't set Option Base to 1? Either way it seems strange to me because in past BASIC's I've used the myarray(5) would be saying "Set up space for me to put 5 items into the array". Is the variable itself taking up the first spot or something??? |
||||
| hitsware2 Guru Joined: 03/08/2019 Location: United StatesPosts: 734 |
The first pointer is 0 So for 5 items ... dim(4) x(0), x(1), x(2), x(3), x(4) my site |
||||
| mkopack73 Senior Member Joined: 03/07/2020 Location: United StatesPosts: 261 |
Does that change if I set the Option for it to be BASE 1 instead of BASE 0??? |
||||
| mkopack73 Senior Member Joined: 03/07/2020 Location: United StatesPosts: 261 |
Ok yeah, a little testing shows that it depends on how Option Base is set... Option Base 0: dim integer test1(4) = (1,2,3,4,5) - ok dim integer test2(4) = (1,2,3,4) - Error not enough initializing values Option Base 1: dim integer test1(4) = (1,2,3,4,5) - error too many initializing values dim integer test2(4) = (1,2,3,4) - ok So, I would say make it a point to ALWAYS specify the option at the top of your program so you know which way it's going to behave... |
||||
| mkopack73 Senior Member Joined: 03/07/2020 Location: United StatesPosts: 261 |
Ok yeah, a little testing shows that it depends on how Option Base is set... Option Base 0: dim integer test1(4) = (1,2,3,4,5) - ok dim integer test2(4) = (1,2,3,4) - Error not enough initializing values Option Base 1: dim integer test1(4) = (1,2,3,4,5) - error too many initializing values dim integer test2(4) = (1,2,3,4) - ok So, I would say make it a point to ALWAYS specify the option at the top of your program so you know which way it's going to behave... |
||||
| hitsware2 Guru Joined: 03/08/2019 Location: United StatesPosts: 734 |
You got me on that one I ' ve never used ' option base ' my site |
||||
| mkopack73 Senior Member Joined: 03/07/2020 Location: United StatesPosts: 261 |
It defaults to 0... So think of the n in the dimension this way: Option base 0 - n is the last index you want to allow (the number of items in the array will be N+1) Option base 1 - n is the # of items in the array (indexes start at 1) |
||||
| Atomizer_Zero Senior Member Joined: 04/07/2020 Location: United KingdomPosts: 134 |
For me, I remember it as: Option Base 0 - array starts at index 0 Option Base 1 - array starts at index 1 |
||||
| mkopack73 Senior Member Joined: 03/07/2020 Location: United StatesPosts: 261 |
Yes, but you also have to think about what the N in the Dim varname(n) means - it's different depending on which base you use... |
||||
| GregZone Senior Member Joined: 22/05/2020 Location: New ZealandPosts: 114 |
The simpliest way to answer this is that when you declare an Array (with DIM) you specify its *highest index*, not the total number of elements in the array. ie. Don't think of it being an array "size", just think of the highest *index* you will reference. |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |