Combining linear and binary searches

Hello,

I have successfully created a linear search and a binary search that work well independent of each other. Now I need to combine them to get them both to run together. What am I missing? Thank you.
#include <iostream>
#include <string>

using namespace std;

int linearSearch(int[], int, int);
int main()

// variables
{
const int lottery = 10;
int options [lottery] = { 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121 };
int lotteryNum;
int location;

//Get the input
cout << "Please enter a five-digit number: " << endl;
cin >> lotteryNum;

location = linearSearch(options, lottery, lotteryNum);


if ( location >-1)
cout << "Congratulations, you have won the lottery!!! " << location << endl;
else
cout << "Sorry, maybe next time. " << endl;

system("PAUSE");
return 0;
}
int linearSearch(int list[], int size, int key)
{
int x;

for (x = 0; x < size; x++)
{
if (list[x] == key)

return 0;
}
return -1;
}

// Now I need to combine the linear search with the below binary search

{
int options[] = { 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121 };
int lotteryNum;

int last = 10,
first = 0,
middle;

cout << "Please enter a five digit number: " << endl;
cin >> lotteryNum;

last--;
while (first <= last)
{
middle = (first + last) / 2;

if (options[middle]<lotteryNum)
first = middle + 1;

else
{
if (options[middle]>lotteryNum)
last = middle - 1;

else
{
last = -2;
}
}
}

if (last < -1)

cout << "Congratulations, you haave won the lottery!!! " << endl;
else
cout << "Sorry, maybe next time " << endl;

system("PAUSE");
return 0;
}
Last edited on
Now I have one set of errors. Please let me know what I can do to solve this.

Line 47: expected a declaration/ '{': missing function header (old-style formal list?)

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <string>

using namespace std;

int linearSearch(int [], int, int);
int binarySearch(const int[], int, int);
const int SIZE = 10;

int main()

// variables
{
	const int lottery = 10;
	int options [lottery] = { 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121 };
	int lotteryNum;
	int location;

	//Get the input
	cout << "Please enter a five-digit number: " << endl;
	cin >> lotteryNum;

	location = linearSearch(options, lottery, lotteryNum);


	if ( location >-1)
		cout << "Congratulations, you have won the lottery!!! " << location << endl;
	else
		cout << "Sorry, maybe next time. " << endl;

	system("PAUSE");
	return 0;
}
int linearSearch(int list[], int size, int key)
{
	int x;

	for (x = 0; x < size; x++)
	{
		if (list[x] == key)

	return 0;
}
	return -1;
}
int binarySearch(const int array[], int size, int value);
{
	const int lottery = 10;
	int options[SIZE] = { 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121 };
	int lotteryNum;
	int results;

	cout << "Please enter a five digit number: " << endl;
	cin >> lotteryNum;

	results = binarySearch(options, SIZE, lotteryNum);

	if (results == -1)
		cout << "Sorry, maybe next time. " << endl;
	else
	{
		cout << "Congratulations, you have won the lottery!!! " << endl;
	}
	system("PAUSE");
	return 0;
}

int binarySearch(const int array[], int size, int value)
{
	int first = 0,
		last = size - 1,
		middle,
		position = -1;
	bool found = false;

	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle - 1;
		else
			first = middle + 1;
	}
	return position;
}
Remove the semicolon at the end of line 46.
Thank you, you were right but now it says int binary Search already has a body. I am guessing that means it has already been declared? How do I fix that?
Nevermind, I just had to pull the brackets out of const int[] and it compiled and ran just fine. Thank you for the help!
NO - you simply aren't testing binary search.

Thank you, you were right but now it says int binary Search already has a body. I am guessing that means it has already been declared? How do I fix that? 


If you look at your code, you will see that you have tried to define the same function twice (in lines 46 and 68).


Nevermind, I just had to pull the brackets out of const int[] and it compiled ...

It only compiled because by doing this, you actually create TWO distinct functions (even though they have the same name they will have different argument lists:
1
2
3
int binarySearch(const int array[], int size, int value
...
int binarySearch(const int array, int size, int value



... and ran just fine. 

Only because your main() routine NEVER ACTUALLY CALLED THIS FUNCTION! It only called linearSearch, not binarySearch.
Topic archived. No new replies allowed.