Facebook

Traversing an Array

The various operations that are performed on arrays are :

  • Traversal
  • Searching
  • Insertion
  • Deletion
  • Sorting and Merging

Traversing in array

This article explains about how to traverse in an array.

Traversing means to visit/to go/travel across each and every element of an array.

Program

#include<stdio.h>

int main()
{
 int rank[5] = {10,20,30,13,15};
 int i;
 printf("\nThe elements of array are :");
 for(i=0; i<5; i++)
 {
  printf("%d \t", rank[i]); 
 }
return 0;
}
Run

Illustration

  • Integer type array of size 5 is declared with values 10,20,30,13,15(Fig 2).
  • Variable i of int type is declared which is to be used in for loop.
  • The initial value of i=0,rank[0] which is 10 gets traversed.i is incremented by 1.
  • Now i=1,rank[1] = 20 gets traversed.
  • Now i = 2,rank[2] = 30 gets traversed.
  • i = 3,rank[3] = 13 gets traversed.
  • i=4,rank[4] = 15 gets traversed.
  • All the elements of array get displayed on the screen.
Fig 2 : Traversing an Array
Fig 2 : Traversing an Array
Rate this post

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar