School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,138 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,870 people online right now. Registration is fast and FREE... Join Now!



Recursive binary search

Recursive binary search Rate Topic: -----

#1 vayber  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 24-April 09


Dream Kudos: 0

Posted 24 April 2009 - 03:56 PM

I have to create a binary search using a recursion, but when i type in a number that does not exist in the sets my program crashes any way to fix that??

#include <iostream>
#include <iomanip>

using namespace std;

// binary search prototype
int binarySearch( int a[], int first, int last, int key, int count );

int main()
{
	const int size = 18;
	int arr[size] = { 1, 2, 5, 7, 9, 12, 13, 17, 19, 20, 25, 48, 56, 78, 81, 89, 93, 96};
	int searchkey = 0;
		
	// Displays the sorted array list
	for ( int i = 0; i < size; i++ )
	{
		cout << setw(4) << arr[i];
		if ( (i + 1) % 5 == 0 ) cout << endl;
	}
	
	// prompts the user to enter a value
	while ( searchkey != -1 )
	{
		cout << "\nEnter a search key (-1 to quit): ";
		cin >> searchkey;
		if ( searchkey == -1 ) break;
		
		// displays results
		int repetitions = binarySearch( arr, arr[0], size, searchkey, 0 );
			cout << "The key has been found after "<<repetitions<<" repetitions\n"; 	
	}

	return 0;
}

// RECURSIVE BINARY SEARCH FUNCTION
int binarySearch( int a[], int first, int last, int key, int count )
{
		int middle = (first + last) / 2;

		if ( key == a[middle] )
		{
			return count;
		}
		else if ( key < a[middle] )
		{
			count +=1;
			return binarySearch( a, first, middle-1, key, count );
		}
		
		else if ( key > a[middle] )
		{
			return binarySearch( a, middle+1, last, key, count );
			count +=1;
		}
		
	return -1;
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 24 April 2009 - 05:00 PM

Was This Post Helpful? 0
  • +
  • -


#2 skaoth  Icon User is offline

  • D.I.C Addict
  • Icon
  • Group: Contributors
  • Posts: 546
  • Joined: 07-November 07


Dream Kudos: 100

Posted 24 April 2009 - 04:20 PM

The problem that is occurring is an issue with the index you use to access the sorted array.
When an element can't be found the index for the beginning and end of the array will cross
you need to check for this condition

if(last > first) {
 
}



before trying to access the array with the middle index
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,128
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Posted 24 April 2009 - 04:59 PM

Moved and merged posts.
Was This Post Helpful? 0
  • +
  • -

#4 vayber  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 24-April 09


Dream Kudos: 0

Posted 25 April 2009 - 07:59 AM

Now that I added that piece of code I cant get my head around a possible algorithm for when numbers dont exist in the set. Any suggestions??
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • Icon
  • Group: Expert w/DIC++
  • Posts: 2,675
  • Joined: 28-September 06


Dream Kudos: 0

Expert In: C/C++

Posted 25 April 2009 - 01:02 PM

View Postvayber, on 25 Apr, 2009 - 07:59 AM, said:

Now that I added that piece of code I cant get my head around a possible algorithm for when numbers dont exist in the set. Any suggestions??


I suggest you read posting #2 carefully and think about what it says.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month