C-MULTIDIMENSIONAL ARRAYS

Dev Frank
6 min readFeb 12, 2024

--

A multidimensional array is like having several arrays combined. Arrays can come in different shapes and sizes, and in this section, we’ll focus on one of the most common multidimensional arrays: the two-dimensional arrays (2D arrays) and three-dimensional arrays (3D arrays).

Think of a regular array like a list or a sequence of elements. Now, a multidimensional array is like having multiple lists arranged in a grid or a table.

A multi-dimensional array is like a set of boxes arranged in rows and columns. Each box contains a number, and you can find a specific box by telling its row number and column number.

TWO-DIMENSIONAL ARRAYS (MATRIX)

A 2D array, often referred to as a matrix, resembles a table with rows and columns.

Imagine a table with rows and columns.

Each element in this table has two indices: row number and column number. So, if you want to find something in this table, you say “row 2, column 3” to get to a specific element.

Let’s explore how to create a 2D array of integers by examining the following example.

int matrix[2][3] = { {2, 3, 4,}, {5, 6, 7} }; 
// A 2D array representing a 2x3 matrix

Think of it like setting up a table. The first number ( [2] )tells us how many rows there are (let’s say 2 rows), and the second number ( [3] )tells us how many columns there are in each row (let’s say 3 columns). The values are arranged as if you were reading across each row.

Accessing the Elements of a 2D Array

Accessing the elements of a 2D array involves specifying the row and column indices for the particular value you want to retrieve. In layman’s terms, it’s like pinpointing a specific spot in a grid or table.

For example, if you have a 2D array representing a table of numbers:

#include <stdio.h>
int main(void)
{
int table[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

To access the number in the second row and third column (7 in this case), you would say: “Go to the second row, then move to the third column.” In C code, it looks like this:

int number = table[1][2];  // row 2, column 3
printf("This is number %d and it is in r0w 2 column 3\n", number);

// Outputs This is number 7 and it is in row 2 column 3

return 0;
}

So, accessing elements of a 2D array involves specifying the row and column indices to get the value located at that specific position in the array.

Change Elements in a 2D Array

Changing elements in a 2D array means updating or putting a new value in a specific spot in the table, refer to the index number of the element in each of the dimensions.

In simpler terms, it’s like erasing the old number in one of the boxes of the table and writing a new number in its place.

The following example will change the value of the element in the first row (0) and first column (0):

#include <stdio.h>

int main
{
int TwoDimArray[2][3] = { {1, 4, 2}, {3, 6, 8} };
TwoDimArray[0][0] = 9;

printf("%d", TwoDimArray[0][0]); // Now outputs 9 instead of 1

return 0;
}

Loop Through a 2D Array

To loop through a 2D array means going through each element in a systematic way, you need one loop for each of the array’s dimension.

int Table[2][3] = { {16, 4, 21}, {63, 86, 48} };

int i, j;
for (i = 0; i < 2; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", Table[i][j]);
}
}
1 4 2
3 6 8

Here’s the breakdown:

The outer loop (for (i = 0; i < 2; i++)) iterates over the rows (2 rows in total).
Inside the outer loop, printf(“\n”); prints a newline character before each row for better formatting.
The inner loop (for (j = 0; j < 3; j++)) iterates over the columns (3 columns in total).
Inside the inner loop, printf(“%d ”, matrix[i][j]); prints each element of the 2D array with a whitespace without adding a newline character, so the elements appear on the same line.

THREE-DIMENSIONAL ARRAYS (MATRIX)

A Three Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It can be thought of as a set of multiple two-dimensional arrays arranged vertically.

Declaration of Three-Dimensional Array in C

You can create a 3D array in C using the following syntax, specifying the type of data, the array’s name, the number of 2D arrays (x), the number of rows in each 2D array (y), and the number of columns in each 2D array (z):

data_type array_name[x][y][z];

data_type: The type of data each element will store.
array_name: The chosen name for the array.
x: The number of 2D arrays.
y: The number of rows in each 2D array.
z: The number of columns in each 2D array

Example

int array[3][3][3];

Initialization of Three-Dimensional Array in C

Initialization of a Three-Dimensional Array in C is done similarly to a 2D array. The difference comes with the increase in dimensions, requiring more nested braces.

Initialization methods include:
1. Using an Initializer List:

int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};

2. A More Readable Method:

int x[2][3][4] = 
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};

3. Using Loops:

int x[2][3][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
x[i][j][k] = (some_value);
}
}
}

Accessing elements in a Three-Dimensional Array in C involves three loops to navigate through the additional dimension. The syntax is array_name[x][y][z], where is the index of the 2D array, y is the index of the 2D array row, and z is the index of the 2D array column.

Example:

#include <stdio.h>

int main(void)
{
int x[2][3][2] = { { { 0, 1 }, { 2, 3 }, { 4, 5 } },
{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };

for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 2; ++k) {
printf("Element at x[%i][%i][%i] = %d\n", i,
j, k, x[i][j][k]);
}
}
}
return (0);
}

Output:

Element at x[0][0][0] = 0
Element at x[0][0][1] = 1
Element at x[0][1][0] = 2
Element at x[0][1][1] = 3
Element at x[0][2][0] = 4
Element at x[0][2][1] = 5
Element at x[1][0][0] = 6
Element at x[1][0][1] = 7
Element at x[1][1][0] = 8
Element at x[1][1][1] = 9
Element at x[1][2][0] = 10
Element at x[1][2][1] = 11

Accessing the Elements of a 3D Array

Accessing elements in a 3D array involves using three indices to pinpoint the specific element you want. The syntax for accessing an element in a 3D array is as follows:

array_name[x][y][z]

Here’s what each index represents:

x: Index of the 2D array.
y: Index of the 2D array row.
z: Index of the 2D array column.

For example, if you have the following 3D array:

int cube[2][3][4] = {
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
},
{
{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}
}
};

To access the element at the second table, first row, and fourth column (16 in this case), you would use:

int element = cube[1][0][3];

This means you go to the second table, then to the third row within that table, and finally, to the fourth column in that row.

In a similar manner, arrays with any number of dimensions can be created. However, complexity increases as the number of dimensions rises. The most commonly used multidimensional array is the Two-Dimensional Array.

Try This !!!

Write a program that initializes a 3x3 matrix and then calculates and prints the sum of each row and each column.

Cheers!!!

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.