2D Arrays

  1. Multidimensional arrays
    1. So far we have only worked with what we call a one dimensional (1D) array
    2. Sometimes it is useful to have an array of arrays (2D array) or even higher
    3. C++ supports 2D, 3D, 4D, and higher dimentional arrays
      // 1D array that hold 5 scores
      int scores[5];
      
      // 2D array that holds 4 * 5 scores
      int scores[4][5];
      
      // 3D array that holds 3 * 4 * 5 scores
      int scores[3][4][5];
      
      // 4D array that holds 6 * 3 * 4 * 5 scores
      int scores[6][3][4][5];
      
    4. Programs that use arrays larger than 2D are rare, so we will focus on 2D arrays only in this course
  2. Initializing a 2D array
    1. Use two sets of brackets to declare: int arrayName[ROWS][COLS];
      // 2D array that has 4 rows and 5 columns
      int scores[4][5];
      
    2. Just like 1D array, row slots range from 0 to ROWS-1 and columns range from 0 to COLS-1
      // Place 75 in the first row and first column
      scores[0][0] = 75;
      
      // Place 80 in the last row and last column
      scores[3][4] = 80;
      
    3. You may initialize a 2D array when declaring it using nested {}
      int scores[4][5] = { { 50, 60, 70, 80, 90 },    // row 0
                           { 95, 65, 55, 85, 75 },    // row 1
      					 { 80, 90, 70, 50, 60 },    // row 2
      					 { 99, 89, 79, 69, 59 } };  // row 3
      
  3. Looping through a 2D array
    1. Nested loops are often used to iterate through a 2D array
      for each row {
         for each column in the row {
            do something with array[row][col]
         }
      }	
      
      // or
      
      for each column {
         for each row in the column {
            do something with array[row][col]
         }
      }
      
    2. Example setting each slot in the array to a random number
      // Loop through each row
      for (int r = 0; r < 4; r++) {
      	// Loop through each column in the row
      	for (int c = 0; c < 5; c++) {
      		scores[r][c] = rand() % 101;
      	}
      }
      
    3. Example displaying all the numbers in the array
      // Loop through each row
      for (int r = 0; r < 4; r++) {
      	// Loop through each column in the row
      	for (int c = 0; c < 5; c++) {
      		cout << scores[r][c] << " ";
      	}
      	
      	// Display the next row on the next line
      	cout << endl;
      }
      
      41 85 72 38 80
      69 65 68 96 22
      49 67 51 61 63
      87 66 24 80 83
      
    4. Example finding the average of the numbers in each row
      // Loop through each row
      for (int r = 0; r < 4; r++) {
      	// Find the sum of the numbers in this row
      	int total = 0;
      	for (int c = 0; c < 5; c++) {
      		total += scores[r][c];
      	}
      		
      	// Display the average
      	cout << "Average of row " << r << " is " 
      		 << total / 5.0 << endl;
      }
      
      Average of row 0 is 63.2
      Average of row 1 is 64
      Average of row 2 is 58.2
      Average of row 3 is 68	
      
    5. Example finding the average of the numbers in each column
      // Loop through each column
      for (int c = 0; c < 5; c++) {
      	// Find the sum of the numbers in this column
      	int total = 0;
      	for (int r = 0; r < 4; r++) {
      		total += scores[r][c];
      	}
      		
      	// Display the average
      	cout << "Average of col " << c << " is " 
      		 << total / 4.0 << endl;
      }
      
      Average of col 0 is 61.5
      Average of col 1 is 70.75
      Average of col 2 is 53.75
      Average of col 3 is 68.75
      Average of col 4 is 62
      
  4. 2D arrays and functions
    1. You can pass any number in a 2D array to a function expecting a single number
      // Swap the first two numbers on the first row
      Swap(scores[0][0], scores[0][1]);
      
    2. When passing 2D arrays to functions, same rules apply as passing 1D arrays to functions
      1. 2D arrays are always passed by reference
      2. Use const to write-protect the array parameter
      3. Function parameter has two sets of brackets, and second bracket must have the column size (used in an equation by the computer to determine where the array value is in RAM)
        void Test(int nums[][5]) {
        	etc...
        }
        
      4. Function argument must have no brackets
        int nums[4][5];
        Test(nums);
        
    3. Example function which displays the contents of the array
      void DisplayArray(const int nums[][5]) {
      	// Loop through each row
      	for (int r = 0; r < 4; r++) {
      		// Loop through each column in the row
      		for (int c = 0; c < 5; c++) {
      			cout << nums[r][c] << " ";
      		}
      		
      		// Display the next row on the next line
      		cout << endl;
      	}
      }
      
    4. Functions that operate on 1D arrays can accept a single row of a 2D array as an argument
      1. Example function which returns the average of a 1D array
        double FindAverage(const int nums[], int size) {
        	int total = 0;
        	for (int i = 0; i < size; i++)
        		total += nums[i];
        		
        	return double(total) / size;
        }
        
      2. Pass each row individually to the function using one []
        int scores[4][5];
        
        // Assume this function initializes the array
        InitializeArray(scores);
        
        for (int r = 0; r < 4; r++) {
        	// Pass this row to the function
        	double ave = FindAverage(scores[r], 5);
        	
        	cout << "Average of row " << r 
        	     << " is " << ave << endl;
        }