C (programming language) DEFFINATION

C (pronounced like the letter C) is a general-purpose computer programming language developed between
1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system
Although C was designed for implementing system software, it is also widely used for developing
portable application software.C has greatly influenced many other popular programming languages, most notably
C++, which began as an extension to C.

Wednesday, 5 October 2011

WRITE A PROGRAM IN C FOR linear search


Linear search c program



#include<stdio.h>
 
main()
{
   int array[100], search, c, number;
 
   printf("Enter the number of elements in array\n");
   scanf("%d",&number);
 
   printf("Enter %d numbers\n", number);
 
   for ( c = 0 ; c < number ; c++ )
      scanf("%d",&array[c]);
 
   printf("Enter the number to search\n");
   scanf("%d",&search);
 
   for ( c = 0 ; c < number ; c++ )
   {
      if ( array[c] == search )     /* if required element found */
      {
      printf("%d is present at location %d.\n", search, c+1);
      break;
   }
   }
   if ( c == number )
      printf("%d is not present in array.\n", search);     
 
   return 0;
}


C program for binary search
Output of code:
Linear Search program output

Linear search for multiple occurrences

In the code below we will print all the locations at which required element is found and also the number of times it occur in the list.
#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 of code:
Linear Search program output for multiple occurence

C program for linear search using function



#include<stdio.h>
 
int linear_search(int*, int, int);
 
main()
{
   int array[100], search, c, n, position;
 
   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);
 
   position = linear_search(array, n, search);
 
   if ( position == -1 )
      printf("%d is not present in array.\n", search);
   else
      printf("%d is present at location %d.\n", search, position+1);
 
   return 0;
} 
 
int linear_search(int *pointer, int n, int find)
{
   int c;
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( *(pointer+c) == find )
         return c;
   }
 
   return -1;
}

6 comments:

Post a Comment