Sign in to follow this  
chockydavid1983

Passing my arguments to my function

Recommended Posts

Hi everyone I'm trying to do a binary search. My program compiles and links but when I come to execute it, when I enter a searchKey, nothing happens, presumably because my arguments aren't being passed to my function. What do I need to change please? Here's my code:

#include <iostream>
#include <string>
#include <limits>
using namespace std;

int BinSearch(int data[], int numElements, int searchKey);

int main()
{
	int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79 ,89 ,95};
	int numElements = 23;

	cout << "(";

	for(int i = 0; i < 22; ++i)
	{
		if(i == 21)
		{
			cout << data [i];
		}
		else
		{
			cout << data[i] << ",";
		}
	}

	cout << ")" << endl << endl;

	string input = "0";

	while (true)
	{
		cout << "Enter search key (or 'x' to exit): ";
		cin >> input;
		cout << endl;

		if( input == "x" )
		{
			cout << "Exiting..." << endl;
			break;
		}
		else
		{
			int n = atoi(input.c_str());
			int searchKey = n;
			cout << searchKey << " is in position " << BinSearch(data, numElements, searchKey);
		}
	}
}

int BinSearch(int data[], int numElements, int searchKey)
{
	int first = 1;
	int last = numElements;
	int mid = (numElements + first) / 2;

	bool quit = false;

	while( !quit )
	{
		if( searchKey == data[mid] )
		{
			return mid;
			quit = true;
		}
		else if( searchKey < data[mid] )
		{
			last = mid - 1;
		}
		else
		{
			first = mid + 1;
		}
	}
}


I also get the following warning:

------ Build started: Project: 3.7.7, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\chris moore\my documents\visual studio 2005\projects\3.7.7\3.7.7\main.cpp(75) : warning C4715: 'BinSearch' : not all control paths return a value
Build log was saved at "file://c:\Documents and Settings\Chris Moore\My Documents\Visual Studio 2005\Projects\3.7.7\3.7.7\Debug\BuildLog.htm"
3.7.7 - 0 error(s), 1 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


I think this is to do with the fact that I haven't specified what to do if the user enters a number that's not in the list. If this is the case, I won't worry about this for now, I just want to get it working for when the user quits or enters a number that is in the list. Thanks, Chris.

Share this post


Link to post
Share on other sites
Well, that algorithm isn't going to exit.

You never change your 'mid' variable in the while loop. so you will never find the key, unless it is accidentaly at the index where mid is pointing to in the beginning.

Edit:

Also, listen to the warnings. In this case the return will work correct, but it is better to put your return at the end of your function.
Your quit=true statement will never be reached because you return before it.

Another edit:

This can sound rude but I think you haven't been programming in C++ for a long time.
It could be your intention to assign 1 to first and numElements to last but in C++ (I think in most if not all low level languages) an array starts with index 0 and ends with numElements-1 .

[Edited by - delta user on August 11, 2007 6:57:48 AM]

Share this post


Link to post
Share on other sites
Hi, thank you for your reply.

My program is now working for entries equal to a value in the list or x to exit. How can I easily deal with string that don't convert to integers in the list? I imagine I could do this specifically for this list by including an if statement inside my function saying:

if (searchKey == data[0] || ...etc)
then do what is already there

else
output some kind of error message, return this message and loop back to the start

But is there a way I can do this more concisely and generally without having to specify exact values the searchKey must be equal to?

Here's my revised code:



#include <iostream>
#include <string>
#include <limits>
using namespace std;

int BinSearch(int data[], int numElements, int searchKey);

int main()
{
int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42,
46, 50, 53, 57, 62, 63, 65, 74, 79 ,89 ,95};

int numElements = 23;

cout << "(";

for(int i = 0; i < 22; ++i)
{
if(i == 21)
{
cout << data [i];
}
else
{
cout << data[i] << ",";
}
}

cout << ")" << endl << endl;

string input = "0";

while (true)
{
cout << "Enter search key (or 'x' to exit): ";
cin >> input;
cout << endl;

if( input == "x" )
{
cout << "Exiting..." << endl;
break;
}
else
{
int n = atoi(input.c_str());
int searchKey = n;
cout << searchKey << " is in position ";
cout << BinSearch(data, numElements, searchKey) + 1 << endl << endl;
}
}
}

int BinSearch(int data[], int numElements, int searchKey)
{
int first = 1;
int last = numElements;
int mid = (numElements + first) / 2;

bool quit = false;

while( !quit )
{
if( searchKey == data[mid] )
{
quit = true;
return mid;
}
else if( searchKey < data[mid] )
{
last = mid - 1;
mid = (last + first) / 2;
}
else
{
first = mid + 1;
mid = (last + first) / 2;
}
}
}




Thanks,
Chris.

Share this post


Link to post
Share on other sites
Don't use atoi() with C++. Use stringstreams to convert, then you can query them to check for a failure:


#include <sstream>
#include <string>
#include <iostream>

void f()
{
std::string s;
std::cin >> s;

std::istringstream is(s);

int n;

is >> n;

if(is.fail()) HandleTheError();
else
{
// n is now an int that has been validly converted
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
Don't use atoi() with C++. Use stringstreams to convert, then you can query them to check for a failure:

*** Source Snippet Removed ***


Its easier to use this for error checking:

void f()
{
std::string s;
std::cin >> s;

std::istringstream is(s);

int n;

if( is >> n ) {
// play with int
} else {
HandleTheError();
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this