View Single Post
Old 10-09-2007, 07:58 PM   #100 (permalink)
Quiz_Master
* Teh Flirt King *
 
Quiz_Master's Avatar
 
Join Date: Dec 2005
Location: Originally From : Ratlam M.P., Currently in: Hyderabad
Posts: 972
Default Re: Post ur C/C++ Programs Here

This was today's homework.

Write a C program to find an element from an Array using Binary Search Method.

Code:
#include <stdio.h>
#define M 10

void bsearch(int list[],int n,int element)
{
   int l,u,m, flag = 0;
   l = 0;
   u = n-1;
   while(l <= u)
   {
      m = (l+u)/2;
      if( list[m] == element)
      {
	       printf("The element whose value is %d is present at position %d in list\n",element,m);
                 flag =1;
                 break;
      }
      else
            if(list[m] < element)
                   l = m+1;
            else
                   u = m-1;
   }
   if( flag == 0)
   printf("The element whose value is %d is not present in the list\n",element);
}

void readlist(int list[],int n)
{
   int i;
   printf("Enter the elements\n");
   for(i=0;i<n;i++)
       scanf("%d",&list[i]);
}

void printlist(int list[],int n)
{
    int i;
   printf("The elements of the list are: \n");
   for(i=0;i<n;i++)
       printf("%d\t",list[i]);
}

void main()
{
   int list[M], n, element;
   printf("Enter the number of elements in the list max = 10\n");
   scanf("%d",&n);
   readlist(list,n);
   printf("\nThe list before sorting is:\n");
   printlist(list,n);
   printf("\nEnter the element to be searched\n");
   scanf("%d",&element);
   bsearch(list,n,element);
}
Couldn't test it as I am on Linux right now and Have no Compiler installed here.
__________________
World is just a Quizzical Reality : Quiz_Master//Ashwin :D

Blog: http://ashwinsaxena.com/blog - Tech, Life and Other Things.
Quiz_Master is offline