Binary Search

im trying to make a function for binary search; ive got it working but i get an error saying ""data" not declared", so heres a little modified code, which gives me:

warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
warning C4244: '-=' : conversion from 'double' to 'int', possible loss of data
warning C4244: '-=' : conversion from 'double' to 'int', possible loss of data
warning C4244: '-=' : conversion from 'double' to 'int', possible loss of data
error C2664: 'BinSearch' : cannot convert parameter 1 from 'int' to 'int []'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

int BinSearch(int data[], float numElements, int searchKey, int num)
{
	numElements = 23;
	searchKey = numElements / 2;
	
	if(searchKey % 2 > 0)
	{
		searchKey -= 0.5;
	}


	while(num != data[searchKey])
	{
	if(num < data[searchKey])
	{	
		searchKey /= 2;
		if(searchKey % 2 > 0)
	{
		searchKey -= 0.5;
	}
	}
	
	else //(num > data[searchKey])
	{
		searchKey /= 2;
		if(searchKey % 2 > 0)
	{
		searchKey -= 0.5;
	}
	}
	}
	return data[searchKey];
}




int main()
{
	char inputChar = 'x';
	int num; 
	
	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}; 
	cout << " {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95}" << endl;
	float numElements = 23;
	int searchKey = 0;

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

	if( num == inputChar )
	{
		cout << "Exiting...";
		break;
	}
	else if( num != data[23] )
	{
		cout << num << " not found." << endl;
	}
	else
	{
	cout << num << " is in position " << BinSearch(data[23], numElements, searchKey, num) << "." << endl;  
	}

	}
}


ty in advance!
Last edited on
Code, please.
A bet without the code: You want to send your binsearch function an integer array, but is actually sending it a single integer. But, as helios says: Code, please
sry my bad, ive included it

Corpus: if i try (in line 68) -> BinSearch(data[] ... i get an error
Last edited on
Just try BinSearch(data, ...) - the name of an array is a pointer to its first element...
i get error as i previously got, when i got the program going:

Run-time Check Failure #3 - The variable "data" is being used without being initialized.

(its a pop-up error...)
Last edited on
searchKey -= 0.5;
If I'm not mistaken, this has the same meaning as searchKey--, since searchKey is an int.
Line 57 has a different meaning than the one you think.
I don't think I understand what line 62 does.
numElements and searchKey are useless as parameters, since their values are overwritten as soon the function starts.
The function seems to be unable to finish if the value cannot be found.

Does the runtime error say the line in which it occurs?
1. i know, but if i change int searchKey to float, i get : error C2296: '%' : illegal, left operand has type 'float'

2. how can i make, so that user can input integer or character?

3. i now know that i have to use data instead of data[23]

4. moved line 6,7 to main()

5. no

heres modified code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
using namespace std;

int BinSearch(int data[], float numElements, float searchKey, int num)
{
	if(searchKey % 2 > 0) 
	{
		searchKey -= 0.5;
	}


	while(num != data[searchKey])
	{
	if(num < data[searchKey])
	{	
		searchKey /= 2;
		if(searchKey % 2 > 0)
	{
		searchKey -= 0.5;
	}
	}
	
	else //(num > data[searchKey])
	{
		searchKey /= 2;
		if(searchKey % 2 > 0)
	{
		searchKey -= 0.5;
	}
	}
	}
	return data[searchKey];
}




int main()
{
	char inputChar = 'x';
	int num; 

	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};
	cout << " {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95}" << endl;
	float numElements = 23;
	int searchKey = numElements / 2;


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

	if( num == inputChar )
	{
		cout << "Exiting...";
		break;
	}
	else if( num != data )
	{
		cout << num << " not found." << endl;
	}
	else
	{
	cout << num << " is in position " << BinSearch(data, numElements, searchKey, num) << "." << endl;  
	}
        }
}
Last edited on
1. There shouldn't be any floats in the function. You don't need them. You can't use floats to index arrays.
2. You have to have the user input a string and then parse it with atoi(). There's no other way.
3. Line 60 doesn't even compile. You can't confirm whether a value exists or not with a single comparison.
5. Yes. If the value can't be found, BinSearch() will never return.
i meant no as no for this q: "Does the runtime error say the line in which it occurs?"; but nwm, ive got the code working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

int BinSearch(int sortedArray[], int numElements, int searchKey)
{
int first, last, middle;

first = 0;
last = numElements - 1;

while (first <= last) 
{
middle = (first + last) / 2; 
if( searchKey > sortedArray[middle])
first = middle + 1;
else if (searchKey < sortedArray[middle])
last = middle - 1;
else //(searchKey == sortedArray[middle])
return middle;
}

return -1;
}


int main()
{

char inputChar = 88;
const int numElements = 23;
int sortedArray[numElements] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int searchKey, location;

cout << " {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95}" << endl;

while(true)
{

cout << "Enter search key (or 'x' to exit): ";
cin >> searchKey;

location = BinSearch(sortedArray, numElements, searchKey);
if (location > -1)
cout << searchKey << " is in position " << location << "." << endl;
else if( searchKey == inputChar )
{
cout << "Exiting..." << endl;
break;
}
else
cout << searchKey << " not found." << endl;
}

}



but how do i implement atoi() in my code?
Last edited on
Topic archived. No new replies allowed.