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.ExamplesInput: arr[2][3] = {{1, 2, 3}, {4, 5, 6}}Output: 1 2 3 4 5 6Input: arr[3][2] = {{-1, -2}, {0, 3}, {5, 7}}Output: -1 -2 0 3 5 7Different Ways to Traverse a Multi-Dimensional Array in CThe most common methods to traverse a multi-dimensional array in C are:Table of ContentUsing Nested LoopsUsing Recursion1. Using Nested LoopsThe 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; } Output1 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 RecursionRecursion 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; } Output1 2 3 4 5 6 Time Complexity: O(rows * cols)Auxiliary Space: O(rows * cols), due to recursive stack usage. Comment More infoAdvertise with us Next Article C Program to Traverse a Multi-Dimensional Array A abhishekcpp Follow Improve Article Tags : C Programs C Language C-Arrays C Basic Programs 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 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 How to Initialize Array to 0 in C? Initializing an array to zero is a common practice in programming to ensure that all elements start with a known value. In C, there are several ways to initialize an array to zero. In this article, we will explore different methods to do so. Initialize Array to 0 in CThere are mainly two ways to ini 2 min read Reverse Array in C Reversing an array means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse an array in C.The simplest method to reverse an array in C program is by using two pointers: one starting at the beginning (left) and 3 min read How to Find the Range of Numbers in an Array in C? The range of numbers within an array is defined as the difference between the maximum and the minimum element present in the array. In this article, we will learn how we can find the range of numbers in an array in C. Example Input:int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }Output: The ra 2 min read Like