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 arrays?
Page 1 of 2 | |||||
Author | Message | ||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9307 |
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 KingdomPosts: 3802 |
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: AustraliaPosts: 265 |
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: AustraliaPosts: 189 |
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, Hugh |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9307 |
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. OK. 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 ZealandPosts: 9307 |
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 ZealandPosts: 9307 |
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? 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... Smoke makes things work. When the smoke gets out, it stops! |
||||
Yankee Newbie Joined: 28/01/2013 Location: United StatesPosts: 5 |
Atari dimensioning: http://www.cyberroach.com/analog/an11/strings.htm |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9307 |
...brings back memories... Smoke makes things work. When the smoke gets out, it stops! |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3802 |
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. John |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9307 |
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: AustraliaPosts: 819 |
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 ZealandPosts: 9307 |
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: AustraliaPosts: 6099 |
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: Jim VK7JH MMedit MMBasic Help |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9307 |
Excellent - thanks, Jim. I will download the latest code library, and find the rest of your routine, and have a play around. Smoke makes things work. When the smoke gets out, it stops! |
||||
isochronic Guru Joined: 21/01/2012 Location: AustraliaPosts: 689 |
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. |
||||
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1101 |
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 ZealandPosts: 9307 |
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: AustraliaPosts: 819 |
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 ZealandPosts: 9307 |
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 |