Open In App

C Program to Traverse a Multi-Dimensional Array

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
1 2 3 4 5 6 

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;
}

Output
1 2 3 4 5 6 

Time Complexity: O(rows * cols)
Auxiliary Space: O(rows * cols), due to recursive stack usage.


Next Article

Similar Reads