Array traversal, or Traversing an Array in C++, is the process of accessing each element of an array, one at a time, in a sequential manner. It is a common operation in C++ programming and is used to process the elements of an array in a specific order.
In C++, there are two primary methods for array traversal: iterative and recursive.
Iterative array traversal involves using a loop to move through the array from start to finish. The loop will typically iterate through the array, beginning with the first element and ending with the last element. At each iteration, the current element’s value is accessed and processed. This can be accomplished using a for loop.
Recursive array traversal involves using a recursive function to move through the array. This works by calling the same function on the remaining elements of the array until the last element is reached. The function is then exited, and the last element is processed.
Regardless of which method is used, the idea behind array traversal is to access each element of the array, one at a time, in a specific order. This allows us to perform operations on each element or process the entire array.
Example 1: Traversing an Array in C++ Using Loop
#include <iostream> using namespace std; int main() { // Declare the array with the size and the array elements int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Declare the element to be searched int item; cout << "Enter the item to be searched: "; cin >> item; // Declare a flag variable and initialize it to 0 int flag = 0; // Iterate through the array and compare each element of the array to the item for (int i = 0; i < 10; i++) { if (array[i] == item) { // If the item is found, set the flag to 1 flag = 1; break; } } // Check the flag value if (flag == 1) cout << "Item found"; else cout << "Item not found"; return 0; }
OUTPUT:
Enter the item to be searched: 7
Item found
The above program example traverses a given array of 10 elements using a loop. The program begins by declaring an array of size 10 and initializing it with elements from 1 to 10. It then takes an element to be searched by the user and stores it in the item
variable. A flag
variable is initialized to 0.
The program then iterates through the array using a for loop and compares each element of the array to the item. If the item is found, the flag
is set to 1. The loop terminates once the item is found or the array is fully traversed.
Finally, the program checks the flag value. If the flag is set to 1
, the item is found. If the flag is still 0
, the item is not found.
The code from the program for this specific function is as follows:
// Iterate through the array and compare each element of the array to the item
for (int i = 0; i < 10; i++) {
if (array[i] == item) {
// If the item is found, set the flag to 1
flag = 1;
break;
}
}
// Check the flag value
if (flag == 1)
cout << "Item found";
else
cout << "Item not found";
Example 2: Traversing an Array in C++ with Recursive Method
#include<iostream> using namespace std; int recursiveSearch(int arr[], int l, int r, int x) { if (r < l) return -1; if (arr[l] == x) return l; if (arr[r] == x) return r; return recursiveSearch(arr, l+1, r-1, x); } int main() { int arr[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; int x; cout<<"Input the element to be searched: "; cin>>x; int n = sizeof(arr)/sizeof(arr[0]); int result = recursiveSearch(arr, 0, n-1, x); (result == -1)? cout<<"Element is not present in array" : cout<<"Element is present at index " <<result; return 0; }
OUTPUT:
Input the element to be searched: 7
Element is not present in array
Step 1: Initialize the variables.
int arr[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; //Declare an array with 10 elements
int x; //Declare a variable x to store the element to be searched
int n = sizeof(arr)/sizeof(arr[0]); //Calculate the size of the array
int result; //Declare a variable to store the result of the search
Step 2: Read the element to be searched
cout<<"Input the element to be searched: ";
cin>>x;
Step 3: Invoke the recursiveSearch() function
result = recursiveSearch(arr, 0, n-1, x);
Step 4: Check if the element is present in the array or not
if (result == -1)
cout<<"Element is not present in array"
else
cout<<"Element is present at index " <<result;
Step 5: Define the recursiveSearch() function
int recursiveSearch(int arr[], int l, int r, int x)
{
//Base case: If the array is completely traversed and the element is not found
if (r < l)
return -1;
//Check if the element is present at the start of the array
if (arr[l] == x)
return l;
//Check if the element is present at the end of the array
if (arr[r] == x)
return r;
//Recursive case: Divide the array into two halves and search for the element in each half
return recursiveSearch(arr, l+1, r-1, x);
}
Example 3: C++ Example of Array Traversal Using for loop to Display Even Values From The Array
#include <iostream> using namespace std; int main() { int n; cout << "Enter the size of the array: "; cin >> n; int arr[n]; cout << "Enter elements of the array: "; for (int i=0; i<n; i++) cin >> arr[i]; // Display even values using for loop cout << "Even values in the array are: "; for (int i=0; i<n; i++) { if (arr[i] % 2 == 0) cout << arr[i] << " "; } return 0; }
OUTPUT:
Enter the size of the array: 5
Enter elements of the array: 7
2
9
6
5
Even values in the array are: 2 6
The program is used to display the even values in an array. The program begins by asking the user to enter the size of the array. It then takes the elements of the array from the user and stores them in an array arr
of size n
.
Using a for loop, the program checks each element of the array. If the element is even, it is printed. This loop runs for n times, i.e., for each element in the array.
This process of traversing the array is repeated for each element in the array. After each iteration of the loop, the index value of the loop is incremented by one, and the loop condition is checked. This is continued until all the elements of the array have been traversed.