Arrays of Objects

rectArray.cpp shows how to make arrays of objects, rectangles in this case. Every array element box[i] is a rectangle, and so you can call its member functions.

Objects Containing Arrays

setOfRects.cpp defines the setOfRects class, which contains an array of rectangles. There is a variable, rectCount, to keep track of how mayny rectangles there are in the array. At any time, the entries 0 through rectCount-1 are valid rectangles. Also note that the array's size SETSIZE is a global constant, and that the add function will not add a rect past the end of the array. If I wanted, I could have written the add function to return a 0 or 1 if it hadn't or had successfully added the rectangle in question. I could also have used a member function to return whether the array was full. Check out the draw, erase, and grow functions. They use for-loops to call the respective member functions of all the rects.

As a final note, run the program, and check out the cool rubber-banding when you draw the rectangles. If you want to use this, look at the main function in setOfRects.

Multidimensional Arrays

Arrays can have arrays as elements. For example, a declaration
int bozo[4][5];
creates an array looking like this:

As you see, the first number indicates the row, and the second indicates the column. These arrays can be tricky to master, but they are very handy. For example, we can finally solve the problem of recursive choose being really slow. Here is Pascal's Triangle::

1
11
121
1331
14641
15101051

If some of the numbers seem familiar, they ought to. If you go to the nth row and kth column of Pascal's triangle, you get n choose k. Do you see the recursive pattern in the rows and columns as well? Each element is the sum of the element above it, and the one above and to the left. The demo PascalsTriangle.cpp shows how to quickly calculate n choose k for n up to 33.

How multidimensional arrays are stored (when all dimensions are known at compile time)

Row-major order means that the elements are stored row by row, and left to right within each row, much like a typewriter goes along. To find any element in a 2D array, let base be the address of the [0][0] entry, M be the number of columns, and eltSize the number of bytes per array element. The address of the [i][j] element is then base + (eltSize * ((i*M) + j)).

In the example above, say you wanted to find the address of bozo[2][3]. That's 1000 + (4 * ((2 * 5) + 3)) = 1052. If it helps, you can think of it as the base, plus (size * (i * M)) [that's the number of bytes in all rows above i] + (size * j) [number of bytes in row i before column j]. Note that the total number of rows does not matter, just as the total number of entries didn't matter in a one-dimensional array. In general, all dimensions except the leftmost one matter when you're calculating where the entries are. When you pass an array as a parameter, then, you must pass all dimensions except the leftmost. (The leftmost is ignored even if you do pass it.)

          int a[10][20][30];
          ...
          void bozo(int b[][20][30], int n);
          ...
          bozo (a, 10);

So we typically pass the leftmost dimension as a parameter, as in the above example, so we can make sure that we don't access any array positions beyond the end of the array. For a more concrete example, look at fillTriangle() in PascalsTriangle2.cpp.

To Index Previous Next