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 Reverse Array of Strings Given an array of string literals, reverse the array. Examples: Input : arr[] = {"Coding", "Never", "Fail", "Me"} Output : arr[] = {"Me", "Fail", "Never", "Coding"} Input : arr[] = {"welcome", "to", "geeksforgeeks"} Output : arr[] = {"geeksforgeeks", "to", "welcome"} The idea is to create an array o
1 min read
Maximum Size of an Array in C Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the dat
4 min read
Is an Array Name a Pointer? In C, arrays and pointers are closely related and are often considered same by many people but this a common misconception. Array names are not a pointer. It is actually a label that refers to a contiguous block of memory where the elements are stored. In this article, we will learn more about the a
4 min read
How to pass an array by value in C ? In C programming, arrays are always passed as pointers to the function. There are no direct ways to pass the array by value. However, there is trick that allows you to simulate the passing of array by value by enclosing it inside a structure and then passing that structure by value. This will also p
2 min read
How to store words in an array in C? We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index
2 min read
Array of Pointers in C In C, a pointer array is a homogeneous collection of indexed pointer variables that are references to a memory location. It is generally used in C Programming when we want to point at multiple memory locations of a similar data type in our C program. We can access the data by dereferencing the point
6 min read