An array stores multiple elements of the same data type in contiguous memory locations. We can print array elements using loops, recursion, iterators, or STL algorithms. Each approach provides a different way to access and display the elements.
- Loops allow sequential traversal using indices or direct element access.
- Recursion prints elements through repeated function calls.
- STL algorithms provide built-in operations for traversing and printing arrays.
Example
Input: arr[] = {10, 20, 30, 40, 50}
Output: 10 20 30 40 50
Ways to Print an Array in C++
There are several ways to print an array in C++. The following methods demonstrate how to traverse and display its elements.
1. Using for Loop
A for loop is a simple way to print array elements. It accesses each element using its index, starting from 0 to n - 1.
#include <iostream>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
// Print array elements using for loop
for (int i = 0; i < n; i++){
cout << arr[i] << " ";
}
return 0;
}
Output
10 20 30 40 50
Explanation: The loop starts at index 0 and prints each element using arr[i] until the last element is reached.
2. Using while Loop
A while loop prints array elements by repeatedly accessing them until the index reaches the array size.
#include <iostream>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int i = 0;
// Print elements using while loop
while (i < n) {
cout << arr[i] << " ";
i++;
}
return 0;
}
Output
10 20 30 40 50
Explanation: The loop prints the current element and increments the index until all elements are displayed.
3. Using Range-Based for Loop
A range-based for loop, introduced in C++11, allows direct access to array elements without manually managing indices.
#include <iostream>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50};
// Print using range-based for loop
for (int x : arr) {
cout << x << " ";
}
return 0;
}
Output
10 20 30 40 50
Explanation: The range-based for loop automatically traverses the array and prints each element.
4. Using Recursion
Recursion is a technique in which a function calls itself to solve a problem. To print an array recursively, the function prints the current element and calls itself for the next index until all elements are printed.
#include <iostream>
using namespace std;
// Recursive function to print array
void printArray(int arr[], int n, int index){
// Base condition
if (index == n)
return;
// Print current element
cout << arr[index] << " ";
// Recursive call for next element
printArray(arr, n, index + 1);
}
int main(){
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
// Start recursion from index 0
printArray(arr, n, 0);
return 0;
}
Output
10 20 30 40 50
Explanation: The function prints the element at the current index and recursively moves to the next index. Recursion stops when index == n.
5. Using Iterators
The begin() and end() functions provide iterators pointing to the first element and one position past the last element of an array, respectively.
#include <iostream>
#include <iterator>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50};
// Traverse array using iterators
for (auto it = begin(arr); it != end(arr); ++it) {
cout << *it << " ";
}
return 0;
}
Output
10 20 30 40 50
Explanation: The iterator moves through the array, and the dereference operator * accesses each element.
6. Using for_each() Function
The for_each() algorithm from the <algorithm> header applies a specified operation to every element in a range. We can use a lambda expression to print each element.
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50};
// Print elements using for_each()
for_each(begin(arr), end(arr), [](int x) {
cout << x << " ";
});
return 0;
}
Output
10 20 30 40 50
Explanation: The for_each() algorithm executes the lambda expression for every array element, printing its value.
Which Method Should You Use?
- Use a for loop when array indices are required.
- Use a while loop when traversal depends on a condition.
- Use a range-based for loop for simple and readable traversal.
- Use recursion when implementing recursive algorithms, keeping in mind the additional stack space.
- Use iterators when working with iterator-based operations.
- Use for_each() when applying an operation to every element.