Monday, April 6, 2015

Arrays 101.01: Intro (Updated April 8, 2015)

Updated Note
Added April 8, 2015

After letting this sit for a day, I decided it did not go far enough when I brainstormed about 30 additional ideas and methods regarding arrays.

Maybe I am out of touch, just a little too old school and not enough object oriented.  Maybe, on the other hand, this is the jumping off point where I go from being a 3GL guy to having the first inner twinge of getting object-oriented programming.

All said, it seems that the concept of arrays would still be important in OO-land, that what I will be describing in future posts as to how I do things now will be re-writable in Java or c#.  What is important now is this stay focused on the basic introduction of the concept of arrays.

Purpose

This is the introduction to arrays, on a conceptual level.  This post will be followed by a series showing different ways to build/rebuild arrays, how to sort them, how they can be used, etc.

What is an array

  • An array is a set of commonly-typed data that conceptually fits together. 
  • Each element of the array can be identified by its position.  For example, if we have the array {$12, $16, $8} we can say "$16" is in the second position.
  • A usable array has a finite number of elements.
  • Arrays can be put into an order (sorted) based on key value(s).
  • An array, for these purposes, sits in memory.

Why Arrays
New 4/8/2015

As I was working through the aforementioned ideas about future posts on arrays, a subset of those were practical and re-usable ways to leverage arrays in applications.  Here is a short list.
  • Code Number to human-understandable-name translation.
  • Drop-down lists for worksheet Data Validation and on user forms.
  • Faster sorting than tables and worksheets.
  • Sorting in support of Subs and Functions without re-arranging worksheets that users expect to be a specific order.
  • Supports binary searching without sorting worksheets.

Array dimensions - Theoretical View

Arrays are described as having a number of dimensions.  
  • A list of temperatures taken during a day would be a one-dimension array, e.g  "Temps(1 to 24)".
  • Temperature readings taken taken at five locations during the hour, would be a two dimension array. Temps(1-5,1-24)  where the "1-5" controls the spot for the temperate reading and "1-24" would allow for the time of day.
  • The same array above, then adding in the dimension of the days of the week, would be a three dimension array, Temps(1-5,1-24, 1-7). 

Array dimensions - Practical View

While, conceptually, arrays can be of any number of dimensions.practically speaking, most arrays are treated as one-dimension by basing them on a complex data type.  For example, the three dimension array above would be treated as 

Type TempReading
    Location as String
    DateTimeStamp as Date (Which, in VBA includes the time!)
    Temperature as Integer
 
Many, if not most, arrays are two-dimensional, meaning there a can be referred to with a single index. Using the above, rather than referring to the nth location, time and temperature as TempReading(n,1), TempReading(n,2), and TempReading(n).Location, TempReading(n).DateTimeStamp, and TempReading(n).Temperature.

What is not an array

There are a number of things that I will admit look and smell like arrays but won't include them in my definition.
  • Sets.  Sets, like arrays, are of a common type or have a common characteristics that "binds" them together.  However, there is no "nth element" of a set.
  • Stacks and Linked-lists, like arrays, have an order, but the order is not index-based.  Linked-lists are sorted by key value(s), Stacks by the point in which the element is added to the Stack. For Linked-lists, sorting is redundant.  with Stacks, it's destructive.
  • Worksheets.  Worksheets rows and columns are based on the idea of a two-dimension array.  However, with the plethora of values around each cell (formatting, for instance) and the visuality versus virtuality, I'll leave these out.
  • Ranges of cells fall in the same line as worksheets, but they also suffer from the possibility of being non-contiguous.
  • Collections.  While members of collections can identified by an index number there is no method for sorting them. 

No comments:

Post a Comment