Skip to content

Multi-Dimensional Arrays

Multi-dimensional arrays extend the concept of one-dimensional arrays by adding additional dimensions, allowing you to represent more complex data structures like tables, grids, or matrices. Instead of storing values in a single row, multi-dimensional arrays let you arrange data in rows and columns, or even more dimensions if needed.

In CSCE240, you’ll probably just have to deal with 2-Dimensional arrays at most. These 2D arrays are defined by rows and columns.

You declare a 2D array similarly to a regular array. Instead of just one set of brackets, you add a second set after:

const int kRowSize = 2;
const int kColumnSize = 5;
int array[kRowSize][kColumnSize];

There are a couple of ways to initialize a 2D array. You can keep the row and column lengths, and fill the array with elements:

const int kRowSize = 2;
const int kColumnSize = 5;
int array[kRowSize][kColumnSize] = {
{1, 2, 3, 4, 5}, // row 1 with 5 columns
{5, 4, 3, 2, 1} // row 2 with 5 columns
};

Or you can omit the row size whilst filling the rows and columns:

int array[][kColumnSize] = {
{1, 2, 3, 4, 5}, // row 1 with 5 columns
{5, 4, 3, 2, 1} // row 2 with 5 columns
};

You can use two for loops to iterate a 2D array. You can think of the outer loop as a way to iterate the number of rows you have while the inner loop as a way to iterate how many columns you have. Lets create a for loop to print the array we declared and initialized:

driver.cc
for (int i = 0; i < kRowSize; ++i) {
for (int j = 0; j < kColumnSize; ++j) {
cout << array[i][j] << (j < kColumnSize - 1 ? " -> " : "");
}
cout << endl;
}

Similarly to how you have options when initializing a multi-dimensional array, you are provided with a couple ways in how to use a multi-dimensional array as a function parameter. The following shows overloaded BubbleSort functions for a multi-dimensional array:

void BubbleSort(int array[][5], int rows) {}
void BubbleSort(int array[3][5]) {}

The first function accepts a 2D array that consists of any number of rows and 5 columns. The second function accepts a stricter 2D array that must contain 3 rows and 5 columns.

Lets create a driver file to practice how to implement different functions to a 2D array. Before the main function lets #include what we need for printing, and declare the functions that we will be using:

driver.cc
// Copyright 2024 CSCE240
#include <iostream>
using std::cout;
using std::endl;
void BubbleSort(int[][5], int);
void Bubble(int[], int);
void Swap(int[], int, int);
void Print2D(const int[][5], int);
void Print1D(const int[], int);

Under the main function lets first write the implementation for what we will use for sorting:

driver.cc
void BubbleSort(int array[][5], int rows) {
for (int i = 0; i < rows; ++i) {
Bubble(array[i], 5);
}
}
void Bubble(int array[], int size) {
bool has_swapped = true;
while (has_swapped) {
has_swapped = false;
for (int i = 0; i < size - 1; ++i) {
if (array[i] > array[i + 1]) {
Swap(array, i, i + 1);
has_swapped = true;
}
}
--size;
}
}
void Swap(int array[], int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

Next, lets define the implementation for our printing functions:

driver.cc
void Print2D(const int array[][5], int rows) {
for (int i = 0; i < rows; ++i) {
Print1D(array[i], 5);
}
}
void Print1D(const int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << array[i] << (i < size - 1 ? " -> " : "");
}
cout << endl;
}

Finally in our main function, lets create a 2D array, sort it, and print it:

driver.cc
int main() {
const int kRowSize = 2;
const int kColumnSize = 5;
int array[][kColumnSize] = {{9, 7, 5, 3, 1}, {10, 8, 6, 4, 2}};
BubbleSort(array, kRowSize);
Print2D(array, kRowSize);
return 0;
}