Download presentation
Presentation is loading. Please wait.
1
Multiple Dimension Arrays
Extending C++ Arrays Copyright © Curt Hill
2
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 © Curt Hill
3
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 © Curt Hill
4
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 © Curt Hill
5
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 © Curt Hill
6
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 bytes of storage The size is the product of dimensions times size of individual Copyright © Curt Hill
7
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 © Curt Hill
8
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 © Curt Hill
9
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 © Curt Hill
10
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 © Curt Hill
11
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 © Curt Hill
12
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 © Curt Hill
13
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 © Curt Hill
14
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 © Curt Hill
15
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 © Curt Hill
16
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 © Curt Hill
17
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 © Curt Hill
18
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 © Curt Hill
19
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 © Curt Hill
20
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 © Curt Hill
21
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 © Curt Hill
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.