Multi-dimensional Arrays

We can store any type of object we like in an array. We've seen ints, doubles, strings, etc. already.

Since we can have arrays of any type of object, why not an array of arrays? For example, suppose we have a class of 5 students, each with 6 homework grades, and the grade info is stored in a file grades.txt. I might want to read in the data and store it, so that I can then answer questions for the user - questions like how did student 4 do on homework 3?

Clearly, I'd like an array of 5 objects (i.e., one object for each student). That is, each object in the array should represent all the homework grades for a given student.

But what type of object can I use to store the 6 homework grades that correspond to a given student?

Answer: An array of 6 ints, of course!

Remember, we create an array of 5 objects of type T like this:

T* a = new T[5]
Each object in my array of students is itself an array of ints, i.e. each object is an int*.
// Replace T with int* 
So, if the type of object that gets stored in the array is int*, we create our array like this:
int** a = new int*[5];
Quick check:

  1. What is the type of a?
     Answer: int**
  2. What is the type of a[i]?
     Answer: int*
Now, having done that, we have an array full of uninitialized pointers; in other words we have an array of pointers that don't yet point to anything! Thus, for each of these 5 pointers we must allocate an array for them to point to! Remember, array[i] is an object of type int*. So wherever you see a[i] just think of it as being a normal old pointer:

int** a = new int*[5];
for(int i = 0; i < 5; i++)
  a[i] = new int[6];
What gets created and how you index elements in it are depicted in this diagram on the right:

This is how you can create "multi-dimensional" arrays in C++. You just have arrays of arrays! Okay, now let's read the grade information and store it in our 2-dimensional array:


ifstream IN("grades.txt");
for(int s = 0; s < 5; s++)
  for(int g = 0; g < 6; g++)
    IN >> a[s][g];
With this, I can get the grade for student stu on homework assignment hwa as array[stu][hwa]. This program is a simple one that reads the grade info and allows the user to ask questions about grades. This version makes nice use of functions, and gives you an idea of how we pass multi-dimensional arrays around for this sort of thing. Here are three extensions of the functionality of this program you might like to consider:

Destroying Multidimeninsional Arrays

As we've mentioned before, arrays allocated with new live on until the end of your program, or until deleted with the delete [] command. With multi-dimensional arrays, you have to remember to delete each array you created. That means, if we refer back to the grades problem from above, that all of the arrays pointed to by the elements of the array grade must be deleted before we can delete the array grade itself.

  for(int s = 0; s < 5; s++)
    delete [] a[s];
  delete [] a;

Problems

  1. Picking random teams. The file names.txt contains a list of names (the number of names is given on the first line of the file). Read those names in, and get from the user a number of teams to be made from those names. The number of teams must evenly divide the number of names. The program should randomly assign names to teams, and display the result. Here is a sample run of the program.
    ~/$ ./ex1
    There are 24 people.
    How many teams would you like? (make it evenly divide n) 3
    Team 0: Mike Dan Chris Joni Christy Seung-Geol Cathy Susan
    Team 1: Gavin Nate Paul Adina Jeff Carl Karen Eric
    Team 2: Phong Betty Madeline Marianne Don Shirley Tim Steve
  2. tic-tac-toe