Given an array, Search all the occurences of the element in the array
Implementation:
#include <stdio.h>
#include <stdlib.h>
/* Function search to print all the occurences of element in the array */
int search(int *a,int n,int s)
{
int i,flag=1;
for(i=0;i<n;i++)
{
if(*(a+i)==s)
{
printf("%d is found at index %d\n",s,i);
flag=0;
}
}
/* If flag is not set to zero, it means no such element is found in the array */
if(flag)
printf("%d is not found",s);
return 0;
}
int main(void) {
int *a,i,n,s;
/* allocate the memory for the array */
a=(int*)malloc(sizeof(int)*100);
/* get the number of elements of the array */
scanf("%d",&n);
/* get the elements of the array */
for(i=0;i<n;i++)
scanf("%d",(a+i));
/* get the element to search for */
scanf("%d",&s);
/* linear search */
search(a,n,s);
return 0;
}