CIS3355:
Business Data Structures |
What Are Multidimensional Arrays And How do they Work?
Contents: - Purpose - Definition - How it works
PURPOSE
It would not make any sense to learn something if we do not understand its purpose or why is it helpful therefore…lets first define why an array is going to make our lives easier!
· It allows to locate an specific data type element in a quick matter.
· It allows the programmer to avoid declaring every single data element.
Now lets see what arrays and multidimensional arrays are:
DEFINITION
“An array is a collection of variables of the same type that are referenced by a common name. A specific element in an array is accessed by an index. In C, the lowest address corresponds to the first element; the highest address corresponds to the last element. Arrays may have from one to several dimensions.” (Schildt, 109)
You want it in SIMPLE TERMS?....Here we go
Arrays: - Set of elements - Must be of the same data type § All integers (2 bytes) § All floats (4 bytes) - Identified by an index (or offset) - Are stored in a contiguous number of bytes in RAM
Figure 1.1 is a representation of an array and they way it would be declared in C supposing the data elements are integers would be:
int variable [7] = {41, -5 ,20 ,0 ,3 ,-11 ,37 }
Figure 1
Note: the offsets are the numbers on the top of the data elements…0 – 6. In C the first index/offset is always 0
Multidimensional Arrays:
§ Arrays that have more than one dimension.
For example, lets assume that you have a two-dimensional array. It represents data stored in two-dimensions… just like a table:
This multidimensional array would be declared as following:
int intvariables [3][4] {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, }
Here [3] and [4] are the offsets or indexes Attention: “unlike some other computer languages, which use commas to separate the array dimensions, C places each dimension in its own set of brackets” (Schildt, 114)
Figures 2 and 3 are representation of three-dimensional arrays:
Figure 2
Figure 3
How does a multidimensional array work? How can we find data elements faster when they are stored as multidimetnional arrays?
Remember that arrays are stored in a contiguous part of RAM memory???? Well… that is the key to understanding how arrays work
Lets analyze this example:
Imagine once you have stored data in a three-dimensional array you want to locate this element:
(By the way, the machine stores the data in RAM in a linear way always. We use the dimensional figures to have a better understanding on how data elements are organized and how are they related to each other)
The information you need to locate an specific data element is:
1.- Base address: Where the first data element is going to be stored. In this case it would be 700
2.- Bytes per element:
We are going to store floats in this example, therefore the bytes per element would be 4.
3.- Array Indexes (or Offsets):
For this particular element the offsets are [1] [0] [2]
NOW, to locate the address we use this formula:
Address = Base Address +
(1rst offset * # of length elements* # of height elements * bytes per element) +
(2nd offset * height elements * bytes per element) +
(3rd offset 8 bytes per element) Then the address for this element would be
Address = 700 + (1*3*3*4) + (0*3*4) + (2*4) = 700+36+12+8 = 756
This same concept is applied to every multidimensional figure.
NOW YOU KNOW HOW TO USE MULTIDIMENSIONAL ARRAYS !!!
Lets test your Wisdom…
Multiple Choice Questions
1.- When declaring an array, for a table with 3 rows and 4 columns, the offsets would be:
A) [3][4] B) [4][3] C) [1][2] D) [2][3]
ANSWER: D
2.- Which one of the following is TRUE:
A) A multidimensional array can be composed of 1 data element. B) Contrary to one dimensional arrays, multidimensional arrays can have different data type elements. C) Multidimensional arrays are stored in contiguous segments of RAM. D) C uses “()” to place the dimensions of a multidimensional array.
ANSWER: C
Short Essay Questions
1.- What is the Base address?
Is the RAM address where the first element of a multidimensional array will be stored.
2.- Why multidimensional arrays make a programmers life easier?
Because by using them, he/she does not have to declare every single data element.
GOOD REFERENCES
http://richardbowles.tripod.com/cpp/cpp13.htm
http://www-306.ibm.com/software/data/db2/db2olap/v81docs/dbag/dinarch.htm
THIS WEB PAGE PROVIDES AN EXCELLENT EXAMPLE OF THE USE OF MULTIDIMENSIONAL ARRAYS
http://www.mathcs.wilkes.edu/~sullivap/cs125/samples/multiArray.html
Bibliography of this tutorial
Schildt, Herbert. Borland C++: The Complete Osborne Mc Graw-Hill. Berkeley California: 1997
Figure 1 Figure 2
From: http://richardbowles.tripod.com/cpp/cpp13.htm
Figure 3 From
http://www-306.ibm.com/software/data/db2/db2olap/v81docs/dbag/dinarch.htm
|