Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Arrays of arrays - or 2D arrays. How to program tables.

It's shorthand when you're learning a new programming language to ask "how does it handle multiple dimension arrays" ... but in practise very few languages actually support true multidimensional arrays these days, and in those which do you might be well advised to use an alternative that's more flexible.

What is a true - proper - two dimensional array? It's a series of successive locations in the computer memory which represents a rectangular table. For example, I could have each 'row' representing a room at our hotel (so that's 1 to 5) and each 'column' representing a day of the month (1 to 31 this month). So that would be 155 memory locations, each of which might contain a number which is the amount we're charging for that room on that night.
  Row 1, Column 1. Cell 1. 95.00 (1st of month, room 1)
  Row 2, Column 1. Cell 32. 85.00 (1st of month, room 2)
  Row 4, Column 10. Cell 103. 85.00 (10th of month, room 4)

Diagram - true 2 dimensional arrayWhere you need a fixed number of columns in each row, this system can work very well ... but it turn out that this is rarely the case. If you have a separate row for each member of your staff, and then columns to hold the names of their children, you'll find yourself having to decide very early on (perhaps far too early) what the maximum number of children should be, and then you'll find that you've often got a very great deal of wasted memory. We used to have a staff member with six children, and we have a delegate on a course a couple of years back who was one of 22 siblings - just think how much memory would have to be put aside "just in case" if we were to use a rectangle system, where every row must allow for the same number of columns. Diagram - a true two dimensional array, showing how only a few of the cells are actually used


Let me describe the better alternative - and that is an array of arrays (or a list of lists). In this case, you'll have an array with just a single dimension which holds the memory address at which the actual data for each column starts. In the case of my rooms / rate each day, this first single-dimensioned array would perhaps contain:
  Row 1 starts at Memory address 1000
  Row 2 starts at Memory address 2000
  Row 3 starts at Memory address 2400 etc
Diagram - an array of array to simulate a 2 dimensional arrayand then the size of each of the arrays that they point to can differ. In the case of days of the month, it's unlikely to make any great difference to the 2 dimensional array (in fact it may be marginally less efficient), but in the majority of cases - of which my staff member example is just one - you'll save huge amounts of space.

Languages such as C and C++, in their fundamental form, use true two dimensional arrays, but using functions such as malloc (in C) and heap objects (C++) you can use the "array of arrays" model instead. Not only does it make for a saving of space, but it also means that memory can be dynamically allocated as you write your program, rather than you having to make a decision as to what maximum you should allow at some point in the development process.Diagram - a list of lists showing the saving of memory and the extra flexibility


With most of the other languages that we teach, you'll actually use arrays of arrays all the time - although the syntax used is very often such that it looks like you are using true two dimensional structures. There's an example of that [here] in PHP, and one [here] in Perl. There's one [here] in Java and one [here] in Python.

((There's an example of a true two dimensional array in C [here] and various C examples that use malloc [here].))
(written 2011-01-02, updated 2011-01-03)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C205 - C and C based languages - Arrays
  [4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)
  [4338] Passing arrays into functions in C - (2014-12-02)
  [3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3121] New year, new C Course - (2011-01-05)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
  [1614] When an array is not an array - (2008-04-17)

C212 - C and C based languages - Memory Management
  [4635] Encapsulating logic in functions and structs - the C approach to Object Oriented techniques - (2016-01-30)
  [4634] Regression testing - via a very short C testing framework - (2016-01-29)
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [3416] Storing Tcl source code encoded, and running via your own C program - (2011-09-02)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [2848] C course - final course example puts it all together - (2010-07-02)
  [2669] Efficient use of dynamic memory - C and realloc - (2010-03-10)
  [1845] Passing a table from Lua into C - (2008-10-18)
  [1670] Dynamic Memory Allocation in C - (2008-06-09)
  [1589] Dynamic Memory Allocation in C - calloc, realloc - (2008-03-22)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [1497] Training Season Starts again! - (2008-01-07)

H999 - Additional PHP Material
  [4655] Image indexer / thumbnail display scripts in PHP - (2016-02-25)
  [3210] Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON - (2011-03-22)
  [2684] Exception handling in PHP - (2010-03-18)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [2073] Extra PHP Examples - (2009-03-09)
  [1623] PHP Techniques - a workshop - (2008-04-26)
  [1519] Flipping images on your web page - (2008-01-26)
  [1505] Script to present commonly used images - PHP - (2008-01-13)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1451] More PHP sample and demonstration programs - (2007-12-01)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1390] Converting from postal address to latitude / longitude - (2007-10-13)
  [1389] Controlling and labelling Google maps via PHP - (2007-10-13)
  [1270] PHP Standalone - keyboard to screen - (2007-07-18)
  [1194] Drawing hands on a clock face - PHP - (2007-05-19)
  [1104] Drawing dynamic graphs in PHP - (2007-03-09)
  [1053] Sorting people by name in PHP - (2007-01-26)
  [1020] Parallel processing in PHP - (2007-01-03)
  [1010] Dates, times, clickable diarys in PHP - (2006-12-28)
  [937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
  [917] Syntax checking in PHP - (2006-11-07)
  [839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
  [822] PHP - a team member leaves - (2006-08-04)
  [806] Check your user is human. Have him retype a word in a graphic - (2006-07-17)
  [789] Hot answers in PHP - (2006-07-02)
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
  [665] PHP Image viewing application - (2006-04-01)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [563] Merging pictures using PHP and GD - (2006-01-13)
  [493] Running a Perl script within a PHP page - (2005-11-12)
  [483] Double Dollars in PHP - (2005-11-02)
  [468] Stand alone PHP programs - (2005-10-18)
  [372] Time calculation in PHP - (2005-07-08)
  [337] the array returned by preg_match_all - (2005-06-06)
  [322] More maps - (2005-05-23)
  [320] Ordnance Survey - using a 'Get a map' - (2005-05-22)
  [239] What and why for the epoch - (2005-03-08)
  [54] PHP and natural sorting - (2004-09-19)

J705 - Java - Arrays
  [4428] Using the lead - passing arrays and other collections in Java - (2015-02-16)
  [4413] Binomial Coefficient (Pascal Triangle) objects in Java - (2015-02-03)
  [4347] Arrays in Java - an introduction for newcomers - (2014-12-10)
  [3039] Fresh Paint - Java Arrays - (2010-11-09)
  [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
  [2648] Java arrays - are they true arrays or not? - (2010-02-23)
  [1498] Java is a dynamic language .... (and comparison) - (2008-01-08)

P217 - Perl - More than Simple Lists and Hashes!
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
  [3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
  [3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3105] Adventure with references to lists and lists of references - (2010-12-26)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
  [293] Course follow-ups - (2005-04-27)
  [43] Hash of lists in Perl - (2004-09-09)

Y104 - Python - Lists and Tuples
  [4722] Embedding more complex code into a named block - (2016-11-04)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
  [3763] Spike solutions and refactoring - a Python example - (2012-06-13)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
  [2719] Traffic lights in Python - (2010-04-13)
  [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
  [2284] Strings as collections in Python - (2009-07-12)
  [2280] Creating and iterating through Python lists - (2009-07-12)
  [1789] Looking for a value in a list - Python - (2008-09-08)
  [1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [955] Python collections - mutable and imutable - (2006-11-29)
  [899] Python - extend v append on a list - (2006-10-20)
  [657] The ternary operator in Python - (2006-03-25)
  [383] Overloading of operators on standard objects in Python - (2005-07-19)


Back to
Opportunities for Melksham - new businesses in the town
Previous and next
or
Horse's mouth home
Forward to
TransWilts - some things to see and do
Some other Articles
When is a program complete?
Learning to write good programs in C and C++ - separating out repeated code
TransWilts - some things to see and do
Arrays of arrays - or 2D arrays. How to program tables.
Opportunities for Melksham - new businesses in the town
Are there newspapers on New Years Day? Do the shops open on Easter Sunday? Do trains run at Christmas?
Perl, Python, PHP, Lua, Linux, and more - and business hotel too. Menu for 2011
Transwilts Link - both Wiltshire and beyond
Trowbridge and Melksham to Chippenham - more roadworks, even slower journey over the winter
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3118_Arr ... bles-.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat