Topics to Cover

2D Arrays

We can store any type of object we like in an array. We've seen ints, doubles, strings, etc. already.
We create an array of 6 objects of type T like this:
T* A = new T[6]
For example, we can create an array of 6 ints as follows:
int* A = new int[6];

Array of arrays?

Since we can have arrays of any type of object, why not an array of arrays?
We create an array of 6 objects of type T like this:

T* A = new T[6]
Each object in my array of students is itself an array of ints, i.e. each element 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*[6];

Well, only an array of pointers so far...

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!

We must allocate an array of intas!

For example, consider the following code:

A[3] = new int[5];
This will change the picture as shown below:

Full 2D array contruction

Naive Elegant (Use this way)
The following code will create a full 2D array.


int** A = new int*[6];

A[0] = new int[5];
A[1] = new int[5];
A[2] = new int[5];
A[3] = new int[5];
A[4] = new int[5];
A[5] = new int[5];
We can use a loop to make the code simpler:


int** A = new int*[6];

for(int i=0; i < 6; i++)
  A[i] = new int[5];

Destroying a 2D Array

We know the following: Therefore, with a 2D array, you have to delete all the arrays you created. For example, suppose you have created a 2D array that looks as follows:

The code shown next will change the picture as follows:

delete [] A[5];

Likewise, the following code will further change the picture as follows:

delete [] A[0];
delete [] A[1];
delete [] A[2];
delete [] A[3];
delete [] A[4];

We are not done yet! We need to delete the last array that A points to!

delete [] A;

Summary

Obvioulsly, we can delete a 2D array more elegantly using a loop.

for(int i=0; i < 6; i++)
  delete [] A[i];

delete [] A;

Quick check:

In the above code:
  1. What is the type of A?
     Answer: int**
  2. What is the type of A[i]?
     Answer: int*

Mandatory Practice Problem

We have a class of 6 students, each with 6 homework grades, and the grade info is stored in a file grades.txt, which looks as follows:

58	96	65	72	93	67
96	67	56	74	94	100
88	81	94	59	95	65
76	97	88	69	64	94
61	91	56	62	81	91
89	52	90	67	56	90
In the file each row represents a student. The program answers questions like the following:

how did student 4 do on homework 3?

Sample run

$ ./a.out
(Q)uit or (V)iew? V
Student number [0..5]: 3
Homework number [0..5]: 4
Grade was 64
(Q)uit or (V)iew? V
Student number [0..5]: 0
Homework number [0..5]: 0
Grade was 58
(Q)uit or (V)iew? Q
Solution

Other Practice Problems

  1. tic-tac-toe
  2. Drawing a picture (possibly flipped). See apple.txt

    Note: For this task, you need to read each individual character including whitespaces. fin.get() will do the trick. It will return the ASCII code of a character you read.

    $ ./a.out
    filename: apple.txt
    > show
    
    
                              $$$$
                            $$$$$$
                           $$$$$$
                           $$$$
                           $$
              $$$$$$$$$$$$$ $$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
          $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
             $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
               $$$$$$$$$$$$$$$$$$$$$$$$$
                 $$$$$$$$$$$$$$$$$$$$$
                   $$$$$$$$  $$$$$$$
    
    > vflip
    
                   $$$$$$$$  $$$$$$$
                 $$$$$$$$$$$$$$$$$$$$$
               $$$$$$$$$$$$$$$$$$$$$$$$$
             $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
          $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
         $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
           $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
              $$$$$$$$$$$$$ $$$$$$$$$$$$$
                           $$
                           $$$$
                           $$$$$$
                            $$$$$$
                              $$$$
    
    > quit
    
    Solution