Processing Two-Dimensional Arrays in C++

Nested for loops are often used to process a two-dimensional array.

Suppose an array matrix is declared as follows:

const int ROW_SIZE = 10;

const int COLUMN_SIZE = 10;

int matrix[ROW_SIZE][COLUMN_SIZE];

Here are some examples of processing two-dimensional arrays:

  1. (Initializing arrays with input values) The following loop initializes the array with user input values:

cout << “Enter ” << ROW_SIZE << ” rows and ”

<< COLUMN_SIZE << ” columns: ” << endl;

for (int i = 0; i < ROW_SIZE; i++)

for (int j = 0; j < COLUMN_SIZE; j++)

cin >> matrix[i][j];

  1. (Initializing arrays with random values) The following loop initializes the array with random values between 0 and 99:

for (int row = 0; row < ROW_SIZE; row++)

{

for (int column = 0; column < COLUMN_SIZE; co1umn++)

{

matrix

[column] = rand() % 100;

}

}

  1. (Displaying arrays) To display a two-dimensional array, you have to display each element in the array using a loop like the following:

for (int row = 0; row < ROW_SIZE; row++)

{

for (int column = 0; column < COLUMN_SIZE; column++)

{

cout << matrix

[column] << ” “;

}

cout << endl;

}

  1. (Summing all elements) Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this:

int total = 0;

for (int row = 0; row < ROW_SIZE; row++)

{

for (int column = 0; column < COLUMN_SIZE; column++)

{

total += matrix

[column];

}

}

  1. (Summing elements by column) For each column, use a variable named total to store its sum. Add each element in the column to total using a loop like this:

for (int column = 0; column < COLUMN_SIZE; column++)

{

int total = 0;

for (int row = 0; row < ROW_SIZE; row++)

total += matrix

[column];

cout << “Sum for column ” << column << ” is ” << total << endl;

}

  1. (Which row has the largest sum?) Use variables maxRow and indexOfMaxRow to track the largest sum and index of the row. For each row, compute its sum and update maxRow and indexOfMaxRow if the new sum is greater.

int maxRow = 0;

int indexOfMaxRow = 0;

// Get sum of the first row in maxRow

for (int column = 0; column < COLUMN_SIZE; column++)

maxRow += matrix[0][column];

for (int row = 1; row < ROW_SIZE; row++)

{

int totalOfThisRow = 0;

for (int column = 0;

column < COLUMN_SIZE; co1umn++)

totalOfThisRow += matrix

[column];

if (totalOfThisRow > maxRow)

{

maxRow = tota1OfThisRow;

indexOfMaxRow = row;

}

}

cout << “Row ” << indexOfMaxRow

<< ” has the maximum sum of ” << maxRow << endl;

  1. (Random shuffling) Shuffling the elements in a one-dimensional array was introduced in Section 7.2.4, “Processing Arrays.” How do you shuffle all the elements in a two­dimensional array? To accomplish this, for each element matrix[i][j], randomly generate indices i1 and j1 and swap matrix[i] [j] with matrix[i 1] [j1], as follows:

srand(time(0));

for (int i = 0; i < ROW_SIZE; i++)

{

for (int j = 0; j < COLUMN_SIZE; j++)

{

int i1 = rand() % ROW_SIZE;

int j1 = rand() % COLUMN_SIZE;

// Swap matrix[i][j] with matrix[i1][j1]

double temp = matrix[i][j];

matrix[i][j] = matrix[i1][j1];

matrix[i1][j1] = temp;

}

}

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *