Binary search in array: - c program, C/C++ Programming

Binary search in array: - C program:

Write a program in c to define binary search in array.

void main()

                {

                clrscr();

                int a[100] , ch , n;

                cout<<"enter the nit for the array : ";

                cin>>n;

                for (int i=0;i

                                {

                                cout<<"enter element "<

                                cin>>a[i];

                                }

                  Cout<< "Binary search:"

                        bsearch (a,n);

                getch();

                } //main

 

void bsearch (int a[100] , int n)

                {

                int f=0,l=n-1,m,i,ele,pos=-1;

                cout<<"enter the element to be searched : ";

                cin>>ele;

                isort(a,n);

                clrscr();

                if (a[0]

                                {

                                while (f<=l && pos=-1)

                                                {

                                                m=(f+l)/2;

                                                if (ele == a[m])

                                                                {

                                                                pos=m;

                                                                }

                                                if (ele>a[m])

                                                                f=m+1;

 

                                                if (ele

                                                                l=m-1;

                                                }

                                if (pos=-1)

                                                cout<<"elememt not found";

                                else

                                                cout<<"element found at "<

 

                                else

                                                {

                                                while (f<=l && pos=-1)

                                                                {

                                                                m=(f+l)/2;

                                                                if (ele == a[m])

                                                                                {

                                                                                pos=m;

                                                                                }

                                                                if (ele>a[m])

                                                                                l=m+1;

 

                                                                if (ele

                                                                                f=m-1;

                                                                }

 

 

Posted Date: 10/19/2012 3:12:07 AM | Location : United States







Related Discussions:- Binary search in array: - c program, Assignment Help, Ask Question on Binary search in array: - c program, Get Answer, Expert's Help, Binary search in array: - c program Discussions

Write discussion on Binary search in array: - c program
Your posts are moderated
Related Questions
The task consists of two parts which are both preferably implemented in one source file. Towards the end of this document you will find a code skeleton that you must assume. Start

in this problem you are given a board in which some of the elements are placed as shown in below diagram.each element represent a color.fill the other element in the board,such tha

how to write a program using arrays

Dereferencing the Pointer this Sometimes a member function requires to make a copy of the invoking instance so that it can change the copy without affecting the original instan

With this assignment you will build a toy program that manipulates pointers to integers. You will develop the same main program fragment in both C and Assembler. Thus, you'll get t

The program will ask the user to enter: '1' to List the cities(and their state abbrev)  from largest to smallest (by their population) with their population '2'  to List the

Example for external storage class - computer programming? extern double sin (double); In a function prototype, for the sin() function its function definition ~ we can write

Define some features of static storage class in c program? The features of a variable defined to contain a static storage class are as follows. Storage - memory Default


Explain the Symbolic Constants in c language? Symbolic constants are the constants of any type that declared by using the #define compiler directive and it is a preprocessor di