Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
02-02-2004, 02:10 PM
|
#1
|
|
LQ Newbie
Registered: Sep 2003
Location: Texas
Posts: 14
Rep:
|
Recursive Function
How would I change the binarySearch function into a recursive function in the following program?
Thanks,
Jessica
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
using std::setw;
int binarySearch (const int [], int, int, int, int);
void printHeader (int);
void printRow (const int [], int, int, int, int);
int main ()
{
const int arraySize=15;
int a [arraySize];
int key;
for (int i=0; i<arraySize; i++)
a[i]=2*i;
cout<< "Enter a number between 0 and 28: ";
cin>>key;
printHeader (arraySize);
int result =
binarySearch (a, key, 0, arraySize-1, arraySize);
if (result != -1)
cout<< '\n'<<key<< " found in array element "
<<result<<endl;
else
cout<<'\n'<<key<<" not found"<<endl;
return 0;
}
int binarySearch(const int b[], int searchKey, int low, int high, int size)
{
int middle;
while (low <= high)
{
middle= (low+high)/2;
printRow(b, low, middle, high, size);
if (searchKey == b[middle])
return middle;
else
if (searchKey<b[middle])
high=middle-1;
else
low=middle+1;
}
return -1;
}
void printHeader(int size)
{
cout<<"\nSubscripts:\n";
for (int j=0; j<size; j++)
cout <<setw (3)<<j<<' ';
cout<<'\n';
for (int k=1; k<=4*size; k++)
cout<<'-';
cout<< endl;
}
void printRow(const int b[], int low, int mid, int high, int size)
{
for (int m=0; m<size; m++)
if (m<low||m>high)
cout<<" ";
else
if (m==mid)
cout <<setw(3)<<b[m]<<'*';
else
cout<<setw(3)<<b[m]<<' ';
cout<<endl;
}
|
|
|
|
02-02-2004, 02:48 PM
|
#2
|
|
Member
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569
Rep:
|
Smells like homework.
|
|
|
|
02-02-2004, 04:50 PM
|
#3
|
|
Moderator
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,816
Rep: 
|
Code:
int binarySearch(const int b[], int searchKey, int low, int high, int size)
{
int middle;
middle = ( low + high ) / 2;
printRow(b, low, middle, high, size);
if ( searchKey > b[middle] )
return binarySearch( b, searchKey, middle + 1 , high, size );
else if ( searchKey < b[middle] )
return binarySearch( b, searchKey, low , middle - 1, size );
else if ( searchKey == b[middle] )
return middle;
return -1;
}
|
|
|
|
02-02-2004, 08:02 PM
|
#4
|
|
LQ Newbie
Registered: Sep 2003
Location: Texas
Posts: 14
Original Poster
Rep:
|
Thank you!!!
|
|
|
|
02-02-2004, 08:58 PM
|
#5
|
|
Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018
Rep:
|
Recursive functions are almost always a bad idea, though. Stack overflows and stuff can be nasty. If you have a non-recursive algorithm, it'd be better to stick with that.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:01 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|