C Program to Traverse an Array
Last Updated :
26 Aug, 2024
Write a C program to traverse the given array that contains N number of elements.
Examples
Input: arr[] = {2, -1, 5, 6, 0, -3}
Output: 2 -1 5 6 0 -3
Input: arr[] = {4, 0, -2, -9, -7, 1}
Output: 4 0 -2 -9 -7 1
Different Ways to Traverse an Array in C
Arrays are versatile data structures and C language provides the following ways to traverse the whole array:
Note: It is compulsory to know the size of the array beforehand to traverse it.
1. Using a Loop
The most common and straightforward method to traverse an array is a loop. The idea is to use a loop that runs from 0 to N - 1, where N is the number of elements in the array. We can use any loop of our choice but a for loop is preferred.
C Program to Traverse an Array Using Loops
C
// C Program to Traverse an Array using a for Loop
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int N = sizeof(arr) / sizeof(arr[0]);
// Traverse and print elements using a for loop
printf("Array elements using loop: ");
for (int i = 0; i < N; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OutputArray elements using loop: 1 2 3 4 5
Time Complexity: O(N)
Auxiliary Space: O(1)
2. Using Recursion
Recursion can also be used to traverse an array, although it is less efficient due to function call overhead and memory consumption. For this approach, you need to know how to pass arrays to functions in C.
Below is the approach to use recursion to traverse the array:
- Define a recursive function that takes the array arr and the size of the array N as arguments.
- This function returns when all the elements are traversed.
- It calls itself for N - 1 elements.
- At last, prints the current element.
C Program to Traverse an Array Using Recursion
C
// C Program to Traverse an Array using Recursion
#include <stdio.h>
// Recursive function to print array elements
void traverseArray(int arr[], int N) {
// When all the elements are traversed
if (N <= 0) {
return;
}
traverseArray(arr, N - 1);
printf("%d ", arr[N - 1]);
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int N = sizeof(arr) / sizeof(arr[0]);
// Traverse and print elements using recursion
printf("Array elements using recursion: ");
traverseArray(arr, N);
printf("\n");
return 0;
}
OutputArray elements using recursion: 1 2 3 4 5
Time Complexity: O(N)
Auxiliary Space: O(N), due to the recursive calls
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
C Program to Traverse a Multi-Dimensional Array 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 method
3 min read
C program to sort an array using pointers Given an array of size n, the task is to sort this array using pointers in C. Examples: Input: n = 5, arr[] = {0, 23, 14, 12, 9} Output: {0, 9, 12, 14, 23} Input: n = 3, arr[] = {7, 0, 2} Output: {0, 2, 7} Approach: The array can be fetched with the help of pointers with the pointer variable pointin
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
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