DS

Search Operation

You can perform a search for array element based on its value or its index.

Example

Consider LA is a linear array with N elements and K is a positive integer such that K<=N.

//Write a program to perform Search operation.
#include <stdio.h>
void main()
{
   int LA[] = {2,4,6,8,9};
   int item = 6, n = 5;
   int i = 0, j = 0;

   printf("The original array elements are:\n");

   for(i = 0; i<n; i++)
   {
      printf("LA[%d] = %d \n", i, LA[i]);
   }

   while( j < n)
   {

      if( LA[j] == item )
      {
         break;
      }

      j = j + 1;
   }

   printf("Found element %d at position %d\n", item, j+1);
}

Output:

Command Prompt
The original array elements are:
LA[0]=2
LA[1]=4
LA[2]=6
LA[3]=8
LA[4]=9
Found element 5 at position 3



Subscribe us on Youtube

Share This Page on


Ask Question