What is traversal operation in array?
Traversal operation in array or simply traversing an array means, Accessing or printing each element of an array exactly once so that the data items (values) of the array can be checked or used as part of some other operation or process (This accessing and processing is sometimes called “visiting” the array).
📝 Note: Accessing or printing of elements always takes place one by one.
How do you traverse in an array step by step?
Let’s see…
Algorithm for Traversing an Array:
- Step 01: Start
- Step 02: [Initialize counter variable. ] Set
i = LB
. - Step 03: Repeat for
i = LB
toUB
. - Step 04: Apply process to
arr[i]
. - Step 05: [End of loop. ]
- Step 06: Stop
Variables used:
- i : Loop counter or counter variable for the
for
loop. - arr : Array name.
- LB : Lower bound. [ The index value or simply index of the first element of an array is called its lower bound ]
- UB : Upper bound. [ The index of the last element is called its upper bound ]
Flowchart for Traversing an Array:

Implementation in C:
// writing a program in C to perform traverse operation in array
void main()
{
int i, size;
int arr[] = {1, -9, 17, 4, -3}; //declaring and initializing array "arr"
size = sizeof(arr)/sizeof(arr[0]); //sizeof(arr) will give 20 and sizeof(arr[0]) will give 4
printf("The array elements are: ");
for(i = 0; i < size; i++)
printf("\narr[%d]= %d", i, arr[i]);
}
If you compile and run the above program, it will produce the following result:
Output:
The array elements are:
arr[0]= 1
arr[1]= -9
arr[2]= 17
arr[3]= 4
arr[4]= -3
Implementation in C++:
// writing a program in C++ to perform traverse operation in array
#include <iostream>
using namespace std;
int main()
{
int i, size;
int arr[] = {53, 99, -11, 5, 102}; //declaring and initializing array "arr"
size = sizeof(arr)/sizeof(arr[0]);
cout << "The array elements are: ";
for(i = 0; i < size; i++)
cout << "\n" << "arr[" << i << "]= " << arr[i];
return 0;
}
If you compile and run the above program, it will produce the following result:
Output:
The array elements are:
arr[0]= 53
arr[1]= 99
arr[2]= -11
arr[3]= 5
arr[4]= 102
It’s lovely. Thanks for sharing
You’re most welcome!😊
Nice tutorial keep it up guys
Thanks for appreciating my work,
I will be grateful to you Sarvesh.
Stay connected.