C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 306,833 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,761 people online right now. Registration is fast and FREE... Join Now!




Recursive binary search

 

Recursive binary search

vayber

24 Apr, 2009 - 03:56 PM
Post #1

New D.I.C Head
*

Joined: 24 Apr, 2009
Posts: 4

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??

cpp
#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.gif ***

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

User is offlineProfile CardPM
+Quote Post


skaoth

RE: Recursive Binary Search

24 Apr, 2009 - 04:20 PM
Post #2

D.I.C Addict
Group Icon

Joined: 7 Nov, 2007
Posts: 526



Thanked: 50 times
Dream Kudos: 100
My Contributions
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

CODE

if(last > first) {

}


before trying to access the array with the middle index
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades

RE: Recursive Binary Search

24 Apr, 2009 - 04:59 PM
Post #3

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,311



Thanked: 453 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Moved and merged posts.
User is offlineProfile CardPM
+Quote Post

vayber

RE: Recursive Binary Search

25 Apr, 2009 - 07:59 AM
Post #4

New D.I.C Head
*

Joined: 24 Apr, 2009
Posts: 4

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??
User is offlineProfile CardPM
+Quote Post

janotte

RE: Recursive Binary Search

25 Apr, 2009 - 01:02 PM
Post #5

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,171



Thanked: 153 times
Expert In: C/C++

My Contributions
QUOTE(vayber @ 25 Apr, 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??


I suggest you read posting #2 carefully and think about what it says.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/20/09 10:55PM

Live C++ Help!

Be Social

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

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month