Multiple Dimension Arrays Extending C++ Arrays Copyright © 1998-2014 Curt Hill
Suppose We are recording test scores: int t1[MAX], t2[MAX], t3[MAX]; These are parallel arrays The value t1[2] and t2[2] both refer to same student, but different tests Parallel arrays are useful, but usually when the base type is different A multiple dimension array or an array of structs or classes is usually better Copyright © 1998-2014 Curt Hill
What is a multiple dimension array? An array of arrays An array with multiple brackets One dimension is a column or list Two dimensions is a table or rectangle Three dimensions is rectangular solid Four dimensions is hard to visualize Copyright © 1998-2014 Curt Hill
Multidimensional arrays Declaration and allocation: int t [8] [4]; for(m=0;m<8;m++) for(n=0;n<4;n++) t[m][n] = 0; Eight students with four tests Copyright © 1998-2014 Curt Hill
Organization In the above: t is a constant pointer to a table – a two dimensioned array of ints Main use is to pass as a parameter t[1] is a pointer to a row of ints Could be passed anywhere a single dimensioned array of ints could be passed t[1][1] is a simple int Copyright © 1998-2014 Curt Hill
More than 2 No real restrictions on the number of dimensions, other than memory consumption int ar[4][7][11][3][5]; Requires 4*7*11*3*5 = 4620 integers This is 18480 bytes of storage The size is the product of dimensions times size of individual Copyright © 1998-2014 Curt Hill
Initialization Brace notation may be used as well Either a single set of values within braces or braces in braces Either int ar[3][2] = {1,2,3,4,5,6}; Or int ar[3][2] = {{1,2},{3,4},{5,6}}; No significant difference Copyright © 1998-2014 Curt Hill
Storage In most cases a two dimensional array is no different than a single but other cases require that we know how it is stored Consider: int ar[3][2] = {0,1,2,3,4,5}; It is no surprize that the first element is ar[0][0] and it will be initialized to zero But does the one get stored in a[1][0] or a[0][1]? Turns out the first subscript varies most slowly so next element is a[0][1] Copyright © 1998-2014 Curt Hill
Location This is important in several places: Location of initialized values Parameter passage So if int ar[3][2] = {0,1,2,3,4,5}; Then a[0][1] is 1 and a[1][0] is 2 etc It is also acceptable to use the following intialization: int ar[3][2]={{0,1},{2,3},{4,5}}; While this is not: int ar[3][2]={{0,1,2},{3,4,5}}; Copyright © 1998-2014 Curt Hill
Parameter Passage We have a problem A function cannot determine the length of an array How then can it handle a multi-dimension array? First we have to consider address calculation in arrays Copyright © 1998-2014 Curt Hill
Single Dimension Array Consider the following declaration Item var[MAX]; address(var) + sizeof(Item) * ndx It needs the base address of the array This is actually the constant pointer The name of the variable To this it must add the product of The length of one Item thing The subscript Copyright © 1998-2014 Curt Hill
Double Consider the following declaration Item var[M][N]; var[i][j] address(var) + sizeof(Item)*N*i + sizeof(item)*j The starting point is the base address The next is the length of a row Finally the position in a row Copyright © 1998-2014 Curt Hill
Difference Did you notice the difference? Item var[MAX]; var[ndx] address(var) + sizeof(Item) * ndx Item var[M][N]; var[i][j] address(var) + sizeof(Item)*N*i + sizeof(item)*j Where does MAX, M or N occur in the formulas? Copyright © 1998-2014 Curt Hill
Discussion The singly dimensioned array address calculation does not consider the length of the array This allows a function that processes singly dimensioned arrays the freedom to accept any size array The second size does appear in the formula What does this mean? We must always specify the second size Copyright © 1998-2014 Curt Hill
Multiple Dimension Arrays Parameters When passing multidimensional arrays we must include every length except the first The first may be included, but is not required Thus double x(int a[][]); will not work, we must say instead: double x (int a[ ][4]); Const is also available Copyright © 1998-2014 Curt Hill
Calling Suppose: double x (int a[ ][4]); int ar1[10][4], ar2[1000][4]; The legal calls of x include: x(ar1); x(ar2); In this case the function x must have some means of knowing sizes other than by parameters Copyright © 1998-2014 Curt Hill
Libraries One of the beauties of C/C++ was the ability to make libraries of functions that could manipulate arrays of any size This seems to not be the case in regards to multiple dimension arrays Or is it? Lying is now in order Copyright © 1998-2014 Curt Hill
Matrix Manipulation Matrices are very important concept We want C libraries that handle them So we lie We pass things as a single dimension array We also pass the physical sizes as well as the used sizes Other multiple dimension arrays library routines use similar techniques Copyright © 1998-2014 Curt Hill
Matrix routines written to take: Any two dimensional double array Using any subset of the available sizes To do this they declare the array as a single dimension They take in as parameters the actual and used dimensions Do the actual address calculations themselves Copyright © 1998-2014 Curt Hill
Example We look at a project that does the lie thing with matrices It will do declarations like this: void mat_display ( const double m[], int actual, int used){… With calls like this: mat_display(&small_ar[0][0], SMALL,4); Copyright © 1998-2014 Curt Hill
Strongly Worded Warning You can lie and make a two dimensioned array look like a singly dimensioned one However, for all assignments in this class: don’t do it Any assignment in the class expects you to pass multiple dimensioned arrays by specifying all the subscripts but the first one! Copyright © 1998-2014 Curt Hill