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
What I need is an auto-click bot. The auto-click bot would ask me for a url(or it would be pre-defined too) so that it loads that specific url and click an ad automatically. This w

Identify 2 to 3 existing algorithms commonly used for object tracking. Algorithms should be in C or written in MATLAB language. Document these algorithms in flowcharts and run thes

write a pseudocode algorithm for a program that accepts a number and prints out its reciprocal(1/n) is required.the program should prevent the user from entering zero by asking t

Manipulators There are several classes in the iostream.h header file. One of them is ios class. This class stores the format state. For example, some bits explain the base in w

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Step 1 Define the program headers and the variables      #include   #include   #include   #include void main()   {       char prompt;     float a,b,c,d,e;     floa

Within C we access external devices by means of a pointer i.e. address. The address could point to any memory mapped device i.e. Ram, Rom, Duart, Disk drives etc. Therefore there i

Recursive Functions Recursion is a process by which a function includes itself with a condition for its safe exit. It is best suitable for a recursive problem. A typical exampl

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of the c

fine the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder''s name (string), account nu