Wednesday, December 11, 2019

Binary Search in C++

This program was written by my close friend and fellow software engineer Sir Ernel Fadriquilan this program is a binary search program written in C++ programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.
My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.com

I am also a book author you can purchase my books on computer programming and information technology in the following links below.

https://www.mindshaperspublishing.com/
https://www.unlimitedbooksph.com/

If you like my video tutorials kindly click the like button and subscribe for more video tutorials on my channel.

Thank you very much for your help and support.


Program Listing

binary_search.cpp

#include < iostream.h>

int main()
{
int c, first, last, middle, n, search, array[100];

cout<<"Enter number of elements\n";
cin>>n;

cout<<"Enter"<< n <<"integers\n";

for ( c = 0 ; c < n ; c++ )
cin>>array[c];

cout<<"Enter value to find\n";
cin>>search;

/*calculating first, last and middle position*/
first = 0;
last = n - 1;
middle = (first+last)/2;


while( first < = last )
{
if ( array[middle] == search )
{
cout<<search<< "found at location"<<middle+1<<endl;
break;
}
else if ( array[middle] < search )
first = middle + 1;
else
last = middle - 1;

middle = (first + last)/2;
}

if ( first > last )
cout<<"Not found!"<<search<< "is not present in the list.\n";

return 0;
}

}

No comments:

Post a Comment