Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

overflow with recursively binary search in c++

$
0
0
I am trying to implement binarySerach recursively, but I got stack overflow.

First I have find function that takes 3 parameters. Then I call this function in main and pass adresess from the array. Nut I have got stack overflow!


bool find( const int x, const int* pBegin, const int* pEnd)
{
	
  int medel = (*pBegin +( *pEnd - *pBegin)/2) ;
  if( pBegin <= pEnd) 
  {
  if(x == medel)
	  return true ;

  else if( x > medel) 
{
	int begin = (medel +1);
	return find (x, &begin, pEnd);
}
  else if( x< medel)
{
	int last = (medel-1);
 return find(x,pBegin, &last);
}
  }
  else
	  return false; 

}// find




void main()
{
    

    int arr[10];
    for (int i=0;i<10;++i)
        arr[i] = i;
    bool found= find(7, &arr[0], &arr[10]);
    cout << "hittat = " << found<< endl;


    system("pause");
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>