had to do a program that did a binary search for the number 5 but for some reason its not being found...here's my code whats wrong..

#include <iostream>

using namespace std;

const int SIZE=15;

int binarySearch(const int, int, int[]);

int main()
{
	int posints[SIZE]={10, 36, 87, 95, 100, 110, 194, 195, 297, 301, 314, 358, 451,467, 500};

	int searchValue=500;

	int results = binarySearch(SIZE, searchValue, posints);

	return 0;
}

int binarySearch (const int SIZE, int searchValue, int array[] )
{
	int first=0, last= SIZE-1, midpoint=(first+last)/2, position=-1,count=1;
	
	bool found=false;

	while (found=false && first<=last)
	{
		cout<<"Pass"<<count<<endl<<"Left: "<<first<<endl<<"Right: "<<last<<endl<<"Midpoint: "<<midpoint<<endl;
		cout<<"a[midpoint]: "<<array[midpoint]<<endl<<endl;

		if(searchValue == array[midpoint])
		{
			found=true;
			position=midpoint;

			cout<<"Value found!";
		}

		else if(searchValue>array[midpoint])
			last=midpoint-1;

		else 
			first=midpoint+1;

		count++;
	}

	if (position=-1)
		cout<<"Value not found!";

	return position;
}

This is wrong right of the back, "while (found=false && first<=last)".

This is also wrong :

else if(searchValue>array[midpoint])
    last=midpoint-1;
else 
   first=midpoint+1;

Any clue why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.