Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Traversal operation on array

In array traversal operation is the process of visiting each element once.

In array, index number is the key factor to support any operation.

In array, each element has an unique index number.

C program, traversing each element in an array:

#include<stdio.h>
int main()
{
int a[5] = {2, 4, 6, 8, 10};
for(int i=0;i<5;i++)
{
printf("%d\n",a[i]); 
}
return 0;
}

Explanation of above program:

In above program, an array is created.

int a[5] = {2,4,6,8,10};

Here, name of array is ‘a’ having number of elements 5.

  • Elements 2 has index number 0.
  • Elements 4 has index number 1.
  • Elements 6 has index number 2.
  • Elements 8 has index number 3.
  • Elements 10 has index number 4.

printf(“%d\n”,a[i]); , this code line will print the value of array.

in a[i], i variable contains index number of array.

for(int i=0;i<5;i++), this code line change the value of i from 0 to 4.

  • a[0] print element 2.
  • a[1] print element 4.
  • a[2] print element 6.
  • a[3] print element 8.
  • a[4] print element 10.