Section 11l

Introduction to Two Dimensional Arrays


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Arrays of Arrays

There are times that groups of data need to be organized differently than just one long list. For example, in a grade book program, there may be thirty groups of test and homework scores. It would be difficult to simply place all the scores in one long list, and try to manage the location of each individual student's grades. For this reason, the C language supports two or more dimensions in arrays. Using two dimensional arrays allows you to implement much more interesting programs.

Rows and Columns

If you recall from science or math class, a dimension is a measurement or analysis in one direction. For example, if you are measuring a room, you might measure its width, its length, and its height. From these values, you can calculate the area of the floor of the room, or the volume of the whole room. In Computer Science, your analysis in more than one dimension involves lists of data.

The grade book scorekeeping process is a good example. A standard grade book allows a teacher to keep the scores of a student on a page starting on the left side and entering values across to the right side. The book also allows him or her to enter several students from the top of the book to the bottom. If you relate this to the dimensions in a room, the scores from left to right can be considered the width dimension, and the scores from top to bottom, the height dimension.

Very commonly, lists such as this one are managed in a row/column format, also known as row major. The values that are entered from top to bottom are entered into rows, and the values that are entered from left to right are entered into columns. The following graphic shows a simple example of this organization.

Example of two dimensional condition with students and graded items

Sometimes a better way to look at multidimensional arrays is to think of them as "arrays of arrays". For example, in the above case, the top row has a list of grades. The simple one dimensional array is of one student's scores. Now, if you look at the rows as an array of these one "student score" arrays, you can see that columns are effectively one array (i.e., of student scores), and the rows are arrays of "student score arrays".

The declaration of a two dimensional array is not too different from a one dimensional array, and once again, life is easier if you use quality self-documenting constants. Shown below is an example of the above scoring array.

double gradeBookArray[ MAX_ROWS ][ MAX_COLS ];

It would be more appropriate however, to define the array with self-documenting capacities as shown below here.

double gradeBookArray[ MAX_STUDENTS ][ MAX_SCORES ];

Notice that if you look at this as an "X - Y" graph, the X direction is placed in the second set of brackets, and the Y direction is set in the first set of brackets. It is easily possible to place the X direction first, and the Y direction second, but the storage process would not look like the above display. The student's scores would be top to bottom, and the students arrays of scores would be left to right. For this reason, you are usually better off to think in terms of rows and columns than in terms of X and Y, again deferring to the Row Major convention.

Loops to operate these kinds of arrays are almost always nested. The following example shows a way to initialize all the array elements to zero so that the grade book is ready to accept grades.

// this loop runs the column-clearing loop for all the rows
for( rowCounter = 0; rowCounter < MAX_ROWS; rowCounter++ )
{
// this loop places a zero in each column for one row
for( colCounter = 0; colCounter < MAX_COLS; colCounter++ )
{
GradeBookArray[ rowCounter ][ colCounter ] = 0.000;
}
}

Watch this video to see an examples of working with two dimensional arrays.

You will find for some programs that you must go through and zero all the items in the array in the same manner as was shown here. Some compilers will automatically initialize all variables to zero, however many do not. For this reason, and as mentioned previously, it is important to note that even if your compiler/developer software may provide you with zeroed elements, you should still not assume that this will happen with every program. If your array has a requirement to be zeroed or cleared in some other fashion, always make sure you do this yourself.

In the future, when you use object oriented classes, you will have the option to initialize them in a simpler way. However, with fundamental arrays, always assume the responsibility for initializing your arrays.

Multi-Dimensional Arrays

In this short section, you have had an opportunity to learn about arrays that are more effectively organized than the linear list that is a one dimensional array. As mentioned previously, you should remember that the C programming language allows for even more arrays. For example, in a bowling league scoring program, you might have an array of bowlers, each bowler has an array of ten frames (i.e., times to bowl), and each frame allows two or three balls to be thrown. A potential array for this might be defined as shown below.

double[][][] bowlingScores
= new double[ MAX_BOWLERS ][ MAX_FRAMES ][ MAX_BALLS_THROWN ];

The good news about this is that the C language will handle this kind of data without breaking a sweat. The bad news is that it is really easy to get confused about which dimension value needs to be changed or updated to add a new score. As you know, any time program code becomes difficult to read or understand, it is no longer worthwhile programming.

At some point in the future, you will learn about vectors and matrices, which are the object oriented versions of one- and two-dimensinal arrays. However, even beyond these powerful tools, there are still better ways to manage two or more dimensions using quality Computer Science. You will have an opportunity to learn about these later.

Two Dimensional Arrays

Simple arrays that hold a list of data are called one dimensional arrays. Arrays that can manage a table of rows and columns are two dimensional arrays, although you must think about the second dimension as just the rows; the columns were already there in the one dimensional array. Being able to manage data like this will lead you to be able to create some significant programs. This topic provided you some introduction to two-dimensional arrays along with a handful of examples as to how to manipulate and manage them. Make sure you have watched both videos (above) as they provide deep analysis for your consideration.

You will get quite a bit more experience when two dimensional arrays are applied to specific problems in advanced OOP operations. For the time being, you need to familiarize yourself with the basics of two dimensional arrays and practice with the arrays and varying conditions in order to become comfortable with them. Stay with it.