Monday, September 17, 2012

Code# 47:C Program To Perform Linear search For Multiple Occurrences

| |

#include<stdio.h>

main()
{
   int array[100], search, c, n, count = 0;

   printf("Enter the number of elements in array\n");
   scanf("%d",&n);

   printf("Enter %d numbers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);

   printf("Enter the number to search\n");
   scanf("%d",&search);

   for ( c = 0 ; c < n ; c++ )
   {
      if ( array[c] == search )    
      {
         printf("%d is present at location %d.\n", search, c+1);
count++;
      }
   }
   if ( count == 0 )
      printf("%d is not present in array.\n", search);
   else
      printf("%d is present %d times in array.\n", search, count);

   return 0;
}
Output:
Enter the number of elements in array
4
Enter 4 numbers
8
5
8
7
Enter the number to search
8
8 is present at location 1.
8 is present at location 3.
8 is present 2 times in array

0 comments:

Post a Comment

Powered by Blogger.