Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 04:23 14 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Confused by Dim...

Author Message
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 08:53pm 18 Jul 2020
Copy link to clipboard 
Print this post

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 States
Posts: 734
Posted: 09:25pm 18 Jul 2020
Copy link to clipboard 
Print this post

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 States
Posts: 261
Posted: 09:28pm 18 Jul 2020
Copy link to clipboard 
Print this post

  hitsware2 said  The first pointer is 0

So for 5 items ... dim(4)
x(0), x(1), x(2), x(3), x(4)



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 States
Posts: 261
Posted: 09:39pm 18 Jul 2020
Copy link to clipboard 
Print this post

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 States
Posts: 261
Posted: 09:39pm 18 Jul 2020
Copy link to clipboard 
Print this post

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 States
Posts: 734
Posted: 10:30pm 18 Jul 2020
Copy link to clipboard 
Print this post

You got me on that one  
I ' ve never used ' option base '
my site
 
mkopack73
Senior Member

Joined: 03/07/2020
Location: United States
Posts: 261
Posted: 10:50pm 18 Jul 2020
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 134
Posted: 11:20pm 18 Jul 2020
Copy link to clipboard 
Print this post

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 States
Posts: 261
Posted: 11:25pm 18 Jul 2020
Copy link to clipboard 
Print this post

  Atomizer_Zero said  For me, I remember it as:
Option Base 0 - array starts at index 0
Option Base 1 - array starts at index 1


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 Zealand
Posts: 114
Posted: 06:22am 19 Jul 2020
Copy link to clipboard 
Print this post

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

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.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025