C Program to Traverse a Multi-Dimensional Array
Last Updated :
23 Jul, 2025
Write a C program to traverse a given multi-dimensional array that contains N elements.
Examples
Input: arr[2][3] = {{1, 2, 3}, {4, 5, 6}}
Output: 1 2 3
4 5 6
Input: arr[3][2] = {{-1, -2}, {0, 3}, {5, 7}}
Output: -1 -2
0 3
5 7
Different Ways to Traverse a Multi-Dimensional Array in C
The most common methods to traverse a multi-dimensional array in C are:
1. Using Nested Loops
The simplest method to traverse a multi-dimensional array is by using nested loops. Each loop corresponds to a specific dimension of the array, with the outermost loop representing the outermost dimension and running according to its size. The inner loops handle the subsequent dimensions, iterating based on their respective sizes.
Below is an example of traversing a 2D array.
C Program to Traverse a Multi-Dimensional Array Using Nested Loops
C
// C program to illustrate how to traverse the multidimensional arrays
#include <stdio.h>
void traverse2DArray(int arr[2][3], int rows, int cols) {
// Loop through each row
for (int i = 0; i < rows; i++)
{
// Loop through each column
for (int j = 0; j < cols; j++)
{
printf("%d ", arr[i][j]);
}
}
}
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int rows = 2;
int cols = 3;
// Traverse and print the 2D array
traverse2DArray(arr, rows, cols);
return 0;
}
Time Complexity: O(rows * cols), where rows is the first dimension, cols is the second dimension.
Auxiliary Space: O(1)
2. Using Recursion
Recursion can be used to traverse a multi-dimensional array, but it requires careful handling of the dimensions to avoid confusion. To learn this approach, you need to know how to pass 2D array in C.
Below is the approach to use recursion to traverse the array:
- Define a recursive function that takes the array, the current row, and column as arguments.
- The base case is when all elements are traversed.
- Recursively call the function for the next element until all are printed.
C Program to Traverse a Multi-Dimensional Array Using Recursion
C
#include <stdio.h>
// Recursive function to traverse a 2D array
void traverse2DArrayRecursive(int arr[2][3], int rows,
int cols, int i, int j)
{
// if the current row index i is greater than or equal to
// the totalnumber of rows, return.
if (i >= rows) {
return;
}
// if the current column index j is less than the total
// number of columnsprint the current element arr[i][j] and
// recursively calls itself with the next column index j + 1.
if (j < cols) {
printf("%d ", arr[i][j]);
traverse2DArrayRecursive(arr, rows, cols, i, j + 1);
}
// call for next row index i + 1
else {
traverse2DArrayRecursive(arr, rows, cols, i + 1, 0);
}
}
int main() {
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
int rows = 2;
int cols = 3;
// Traverse and print the 2D array using recursion
traverse2DArrayRecursive(arr, rows, cols, 0, 0);
return 0;
}
Time Complexity: O(rows * cols)
Auxiliary Space: O(rows * cols), due to recursive stack usage.
Similar Reads
C Program to Traverse an Array in Reverse Write a C program to traverse a given array in reverse order that contains N elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3}Output: -3 0 6 5 -1 2Input: arr[] = {4, 0, -2, -9, -7, 1}Output: 1 -7 -9 -2 0 4Different Ways to Traverse an Array in Reverse Order in CWe can traverse/print the array in
2 min read
How to Pass a 3D Array to a Function in C? A 3D array (or three-dimensional array) in C is a multi-dimensional array that contains multiple layers of two-dimensional arrays stacked on top of each other. It stores elements that can be accessed using three indices: the depth index, row index, and column index. In this article, we will learn ho
3 min read
How to Initialize Array of Pointers in C? Arrays are collections of similar data elements that are stored in contiguous memory locations. On the other hand, pointers are variables that store the memory address of another variable. In this article, we will learn how to initialize an array of pointers in C. Initialize Array of Pointers in CWe
2 min read
How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.Initializing a Dynamic Arrays
2 min read
How to Pass Array of Structure to a Function in C? An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin
2 min read
C program to input an array from a sequence of space-separated integers Given a string S consisting of space-separated integers, the task is to write a C program to take the integers as input from the string S and store them in an array arr[]. Examples: Input: S = "1 2 3 4"Output: {1, 2, 3, 4} Input: S = "32 12"Output: {32, 12} Approach: The idea is to solve the given p
2 min read