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

Assignment Help:

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;

                                                                }

 

 


Related Discussions:- Binary search in array: - c program

C prg, main() { int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } The value of b[-1]...

main() { int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } The value of b[-1] is

Explain the class invariant, Explain the class invariant. - It's a cond...

Explain the class invariant. - It's a condition that ensures correct working of a class and defines all the valid states for an object. - When an object is created class inv

C/c++ program, luminous jewel polishing necklace,c/c++ programming

luminous jewel polishing necklace,c/c++ programming

Control structures in cpp, Control structures The control structures app...

Control structures The control structures appear in both structured programming languages as well as object oriented programming languages.  The three constructs used are: i)

# Bank Account class, Write a C++ program to test the Bank Account class fo...

Write a C++ program to test the Bank Account class for 10 customers.

State six typical application of primary cells, Normal 0 false ...

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

Big M method, I Want a answer for solving the big M method in the topic of ...

I Want a answer for solving the big M method in the topic of simplex method...

#title areaundercurve.c, Write a program to find the area under the curve y...

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

Define variable declaration in c++, Variable Declaration This declarati...

Variable Declaration This declaration of variables in the C language is permitted only in the starting of their block, prior to executable program statements. In C++ declaratio

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd