C Program to Traverse a Multi-Dimensional Array
Last Updated :
26 Aug, 2024
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 Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro
3 min read
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
C Program to Copy an Array to Another Array In this article, we will learn how to copy all the elements of one array to another array in C.The simplest method to copy an array is by using the memcpy() function. Let's take a look at an example:C#include <stdio.h> #include <string.h> int main() { int arr1[] = {1, 2, 3, 4, 5}; int n
3 min read