Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:29 27 Nov 2024 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 : Help me understand array’s?

     Page 1 of 2    
Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 09:50pm 02 Feb 2013
Copy link to clipboard 
Print this post

I am just wanting to understand how array's work a bit more.

In Atari BASIC years ago, there was also a DIM command, but I think it was not as sophisticated as the MMBASIC.

In the Atari, DIM A$(100) would reserve 100 bytes of RAM for the Dimensional Array, but from what I remember back in those days, this "Array" was not really a dimensional array. The 100 bytes reserved could be accessed kinda like general purpose RAM - you could put things in the reserved RAM, and you could read it back.

However, something I hear being tossed about a lot on these forums is Dynamic Array and true Dimensional array.

So, can someone just explain to me what the DIM command really does more then what is in the manual, which is just basic information on the command, but not really some examples of how you use it's advanced features.

Can someone here give some examples of where a dimensional array would be useful?

It will help me understand it more.
Smoke makes things work. When the smoke gets out, it stops!
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3802
Posted: 05:56am 03 Feb 2013
Copy link to clipboard 
Print this post

First thing is, if that's what Atari did then it was Atari-specific and not what most other BASICs did or do.

DIM A(100) says you want 100 storage areas - think of houses all along a street boringly named A. Then each one is chosen by putting its number in parens, so
A(5) would be specifying the 5th house.

DIM A$(100) is the same and you're saying you want each to be a string. (In this BASIC each string uses up 255 or so bytes no matter what you put in it - kinda wasteful but simple).

So, A$(5) would be a string. So would A$(7) and they'd not be sharing any memory or whatever.

I never knew Atari did something so non-standard!

John
 
James_From_Canb

Senior Member

Joined: 19/06/2011
Location: Australia
Posts: 265
Posted: 11:50am 03 Feb 2013
Copy link to clipboard 
Print this post

How could an array be useful?
Well, how about reading entries from a file into an array so that they're faster to access? It also reduces wear and tear on the flash memory card.

James
My mind is aglow with whirling, transient nodes of thought careening through a cosmic vapor of invention.

Hedley Lamarr, Blazing Saddles (1974)
 
shoebuckle
Senior Member

Joined: 21/01/2012
Location: Australia
Posts: 189
Posted: 12:32pm 03 Feb 2013
Copy link to clipboard 
Print this post

I think of Arrays as a set of pigeon holes.
- A one dimensional array e.g. A$(5) has five text elements side by side
- Add another dimension e.g. A$(5,3) and you have stacked two more rows on top of the first, a bit like a very small spreadsheet consisting of 3 rows, each with 5 columns.
- Going to a 3 dimensional Array e.g. A$(5,3,4) is like adding 3 more of the same behind our two dimensional set.

One can go on adding dimensions, but the analogy is hard to visualise in a 3 dimensional world. Arrays can contain either text, such as A$(5,6), or numeric data, such as A(5,6).

Each dimension represents a characteristic of the data. For instance an array Sales(10,12,5) might contain the sales over 10 years (1st dimension) in each month (2nd dimension) in 5 sales areas (3rd dimension).

So DIM Sales(10,12,5) defines and reserves the space required for all the sales data over a 10 year period and Sales(3,4,1)=b would store the sales figure for area 1, April, in 3rd year. Likewise c=Sales(3,4,1) would retrieve that same value.

Hope this helps. Arrays can be very useful in storing such data.
Cheers,
HughEdited by shoebuckle 2013-02-04
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 12:44pm 03 Feb 2013
Copy link to clipboard 
Print this post

  JohnS said   First thing is, if that's what Atari did then it was Atari-specific and not what most other BASICs did or do.


Correct.
I seem to recall, even all those years ago, that others in the know were critical of this command in Atari BASIC, as it suggests it can do something it can't. Atari had two commands that did the same thing: DIM and COM, so you could also have a line which read COM A$(100), which was precisecly the same thing as DIM A$(100). To this day, I have no idea why they would have two different commands that do the same thing in their BASIC.

  JohnS said  DIM A(100) says you want 100 storage areas - think of houses all along a street boringly named A. Then each one is chosen by putting its number in parens, so A(5) would be specifying the 5th house.


OK.

  JohnS said  DIM A$(100) is the same and you're saying you want each to be a string. (In this BASIC each string uses up 255 or so bytes no matter what you put in it - kinda wasteful but simple).


Interesting. So does that mean that if you were to write HELLO WORLD 1 into the array at A$(1), and then write HELLO WORLD 2 into the array at A$(2), that they are totally separate, and one would not overwrite the other?

Do I have that right?

The Atari DIM or COM command worked more like an EEPROM memory chip - you could define the string as A$(100) for example, and you could write something like HELLO WORLD 1 into the array starting at a location, say, 1, but if I was to write something into A$ at position 5, then I would overwrite the data already there. In the example just given, if HELLO WORLD 1 was written at location 1, then dashes in location 5 through 20, the string would look like: HELLO---------------

I write all that, just so you and others can see how the Atari used to do it.
Academic, as we are not writing Atari BASIC here, but just so you can get an idea in your head(s) how that worked back then. It really is exactly how EEPROM chips work.

Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 12:47pm 03 Feb 2013
Copy link to clipboard 
Print this post

  James_From_Canb said   How could an array be useful?
Well, how about reading entries from a file into an array so that they're faster to access? It also reduces wear and tear on the flash memory card.

James


Hmmmmmmm - yes, that could be useful - in fact I plan to write a program that looks in the SD card for a file, and reads it, so perhaps I can just store all this in a large-ish array instead - system reads the file into the array at fire-up....
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 12:52pm 03 Feb 2013
Copy link to clipboard 
Print this post

  shoebuckle said   I think of Arrays as a set of pigeon holes.
- A one dimensional array e.g. A$(5) has five text elements side by side
- Add another dimension e.g. A$(5,3) and you have stacked two more rows on top of the first, a bit like a very small spreadsheet consisting of 3 rows, each with 5 columns.
- Going to a 3 dimensional Array e.g. A$(5,3,4) is like adding 3 more of the same behind our two dimensional set.


So, you are saying that A$(5,3) means that there are TWO lumps of RAM reserved here, each ONE lump is 5-bytes in size.

Pseudo visual is like this:

Element 1: -----
Element 2: -----
Element 3: -----

Each one is addressable easily using the DIM command - I think I understand that corectly, yes?

  shoebuckle said  One can go on adding dimensions, but the analogy is hard to visualise in a 3 dimensional world. Arrays can contain either text, such as A$(5,6), or numeric data, such as A(5,6).

Each dimension represents a characteristic of the data. For instance an array Sales(10,12,5) might contain the sales over 10 years (1st dimension) in each month (2nd dimension) in 5 sales areas (3rd dimension).

So DIM Sales(10,12,5) defines and reserves the space required for all the sales data over a 10 year period and Sales(3,4,1)=b would store the sales figure for area 1, April, in 3rd year. Likewise c=Sales(3,4,1) would retrieve that same value.

Hope this helps. Arrays can be very useful in storing such data.
Cheers,
Hugh


I'm a bit lost with that above three-dimension array, but I will read the post several times, so I can try to get it clear in my head.

Naturally, I will do some program experiments to see how this works on a practical level...Edited by Grogster 2013-02-04
Smoke makes things work. When the smoke gets out, it stops!
 
Yankee
Newbie

Joined: 28/01/2013
Location: United States
Posts: 5
Posted: 12:54pm 03 Feb 2013
Copy link to clipboard 
Print this post

Atari dimensioning: http://www.cyberroach.com/analog/an11/strings.htm
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 01:00pm 03 Feb 2013
Copy link to clipboard 
Print this post

  Yankee said   Atari dimensioning: http://www.cyberroach.com/analog/an11/strings.htm


...brings back memories...
Smoke makes things work. When the smoke gets out, it stops!
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3802
Posted: 01:43pm 03 Feb 2013
Copy link to clipboard 
Print this post

DIM A$(5,3) should be like a table (in maths aka a spreadsheet or grid): 5 x 3 so a total of 15 items (strings, here). Each in MMBasic will use about 255 bytes so RAM is gonna vanish fast!

However many, they're each individual (separate) so putting HELLO WORLD in two of them doesn't save any memory (at least, I think MMBasic doesn't; many other Basics would share the string).

Arrays have lots of uses. Imagine reading an ADC, you might like to keep the values and do some sort of averaging or the like and could store successive values in an array.

Or a string array, maybe you let the user input names and then sort them alphabetically.

A string is rather like an array (of characters). Some languages (such as C) actually don't have strings but have arrays of chars instead. Basics tend to have them like MMBasic in the sense that you can do
string + string
and it results in the first with the second tacked on the end. A new meaning of the + operator, when you think about it, but makes pretty good sense and looks good on the page.

JohnEdited by JohnS 2013-02-04
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 01:56pm 03 Feb 2013
Copy link to clipboard 
Print this post

Excellent, thanks.

I will now have a play around with the command in the MM to see what I can make of it.

I have a feeling, it could be a great way to store all my text messages in a system that then sends these messages to pagers. I can store the messages in the array, rather then on the SD card one-by-one.

I will have a play and let you know how I get on.

No doubt I will have problems and questions, and will post them here.
Smoke makes things work. When the smoke gets out, it stops!
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 02:46pm 03 Feb 2013
Copy link to clipboard 
Print this post

Assuming no RAM is used for any other purpose, how big can:

1 an integer array

2 a byte array

3 a floating point array

4 a string array, be in MMBasic?

assuming of course that all above array types can be dimensioned, or do we only have numeric and string. Are all elements in numeric arrays e.g. Dim A(5) treated as floating point and in string arrays, are all elements 255 bytes even though they may be single characters?






David M.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 03:03pm 03 Feb 2013
Copy link to clipboard 
Print this post

Not sure about the numerical arrays, but my understand from other posts above, is that with respect to string arrays, they are automatically made to be 255 bytes in size no matter how many of those 255 bytes you are actually using.

Therefore, DIM A$(1) will consume 255 bytes, even if you only put ABCD in the string at that point in the array.

That is my understanding of it anyway - please feel free to correct me if I am wrong.
Smoke makes things work. When the smoke gets out, it stops!
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6099
Posted: 04:05pm 03 Feb 2013
Copy link to clipboard 
Print this post

String arrays use 256 bytes per location plus 32 bytes for the name and some more for the pointers etc.

Numeric arrays use 4 bytes per location plus 32 bytes for the name and some more for the pointers etc.
The only numbers are floating point so the is no integer array. If there was, it would be a 32 bit integer so 4 bytes again.

You use less memory by using an array instead of individual variables.

If you have a lot of short strings to work with, have a look at my short strings program available from the MM library.

Here is the readme:
  Quote  Small Strings
An MMBasic program by TassyJim - Oct 2012

Save space by using short strings. MMBasic strings are a fixed 255 bytes long.
If you have a lot of short strings, you can save a lot of valuable memory by using short strings.
The code creates an array of any ( < 255 ) length strings.
It would be a bit slow for intensive string manipulation but it can save heaps of space.

Strings use 256 bytes. The first byte is the length of the string and the remaining 255 bytes are where the actual string is stored. I used the same method so if you need strings 32 bytes long, you will have to specify 33 as the size. In MMBasic you can specify the starting array number as zero or one. I have stayed with one because that is the way I have always done it.
I use the 'zero' location to store the new array size to save on 2 variables.

An example that might be relevant to you.

I have a list of 652 Aussie towns.
The longest name in the list is "Kingston South East" which is 19 characters long.
An array of 652 X 20 bytes is needed. 652*20/256 = 51
My method creates an array with 51 elements which uses about 13k
Full length strings would need 163k of memory
I also have a list of 65k place names but that's too much for the Maximite to chew on!

I deliberately kept to single dimension arrays but it is easy to use the short array as 2 dimensions. Most times a single dim array for the string and another standard numeric array for the rest of the data. In the Towns example this numeric array will store the latitude and longitude of the towns.

It is thanks to Geoff implementing PEEK and POKE and giving us the location of the variable table that I was able to do this.

The code makes use of PEEK and POKE so it does have the potential to cause havoc.

function makeSmall( aa,bb)
First we work out the number of normal string elements needed to store out small array.
We then create the holding array and find its memory address.
The first 2 memory locations are used to store the new array dimensions.

function PutSmall(b, a$)
We pass the array element and the string for storing.
If the string is too long, it is truncated without any error message.
The function returns -1 if an error occurs or the length of the string.

function GetSmall$(b, a$)
Pass the array element and an optional error string
Return the string or the error string if array is out of bounds

The code has been tested on various hardware but not as part of a big program.
Jim


Jim

VK7JH
MMedit   MMBasic Help
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 05:11pm 03 Feb 2013
Copy link to clipboard 
Print this post

Excellent - thanks, Jim.

I will download the latest code library, and find the rest of your routine, and have a play around.

Edited by Grogster 2013-02-05
Smoke makes things work. When the smoke gets out, it stops!
 
isochronic
Guru

Joined: 21/01/2012
Location: Australia
Posts: 689
Posted: 05:33pm 03 Feb 2013
Copy link to clipboard 
Print this post

If it helps any :

DIM is an abbreviation of DIMENSION and is an artifact from Fortran.
The idea being,(for a bunched set of data elements) to define the dimensions used and each extent.
Thus the idea of a vector (one dimension), array (two or more),etc. I have heard "book" used as an analogy for a three dimensional array. I think up to seven-dimensional arrays were offered even in the early Fortran versions.
The elements' memory locations could also be specified as COMMON across program/subroutines etc as required, as otherwise (at that stage) the subroutines etc used additional memory which was expensive. (A long time ago).
After some time Basic was introduced as "computing for the rest of us"
with an easier approach to help beginners.
DIM is a bit redundant now, most languages allow the datatype, dimensionality and extent to be all included in the declaration
eg

int coords[10,10];

which is much better IMHO.
Edited by chronic 2013-02-05
 
panky

Guru

Joined: 02/10/2012
Location: Australia
Posts: 1101
Posted: 11:59pm 03 Feb 2013
Copy link to clipboard 
Print this post

Hi Grogster,

You might like to have a look at my recent post also called MMBasic Stacks, Queues and Lists. Again my thanks to Jim for the idea of sub-strings.

Cheers, Doug.


... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 12:05am 04 Feb 2013
Copy link to clipboard 
Print this post

Cheers, matey - will find it and check it out as part of my research.

My Altronics CMM finally arrived today - yay!!!
Smoke makes things work. When the smoke gets out, it stops!
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 12:21am 04 Feb 2013
Copy link to clipboard 
Print this post

  Quote  My Altronics CMM finally arrived today - yay!!!


I'm happy for you. I know what it is like to drive into town every day to check the Po Box hoping for my "array" (there's that word!!) of electronic parts to arrive from China. I'm beginning to think it was a big mistake ordering around Cinese New Year.

Now, dont't forget to fit that heat sink and fan.
David M.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9307
Posted: 12:37am 04 Feb 2013
Copy link to clipboard 
Print this post

Oh yes - full water-cooling, pump and tubing I expect...

I do see what Nick means about the tiny 0805 cap though.

I have not built it yet - just checking over the contents so I can form a plan of attack. That attack will be 10uF ceramic FIRST.
Smoke makes things work. When the smoke gets out, it stops!
 
     Page 1 of 2    
Print this page
© JAQ Software 2024