Skip to content

Traverse Operation in Array: Program, Algorithm and Flowchart

Traverse Operation in Array

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.

Visual example

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 ]

Flowchart for Traversing an Array:

Traverse operation in array

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[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 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[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

Share this Post

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

Leave a Reply

Your email address will not be published.