# Traverse Operation in Array: Program, Algorithm and Flowchart

• Array, Data Structures ## 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` to `UB`.
• Step 04: Apply process to `arr[i]`.
• Step 05: [End of loop. ]
• Step 06: Stop

Variables used:

1. i : Loop counter or counter variable for the `for` loop.
2. arr : Array name.
3. LB : Lower bound. [ The index value or simply index of the first element of an array is called its lower bound ]
4. UB : Upper bound. [ The index of the last element is called its upper bound ]

### Implementation in C:

```// writing a program in C to perform traverse operation on an array

void main()
{
int i, size;
int arr[]={1, -9, 17, 4, -3};     //declaring and initializing array
size=sizeof(arr)/sizeof(arr);     //sizeof(arr) will give 20 and sizeof(arr) 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= 1
arr= -9
arr= 17
arr= 4
arr= -3
```

### Implementation in C++:

```// writing a program in C++ to perform traverse operation on an array

#include <iostream>
using namespace std;
int main()
{
int i, size;
int arr[]={53, 99, -11, 5, 102};    //declaring and initializing array
size=sizeof(arr)/sizeof(arr);
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= 53
arr= 99
arr= -11
arr= 5
arr= 102
```

## 4 thoughts on “Traverse Operation in Array: Program, Algorithm and Flowchart”

1. It’s lovely. Thanks for sharing

1. You’re most welcome!😊

2. Nice tutorial keep it up guys

1. Thanks for appreciating my work,
I will be grateful to you Sarvesh.
Stay connected.