Using Binary Search function

I am trying to use a binary search to find the number 123 in an array of 100 random numbers. It should print whether it has found the digits or not. I also need Binary search to recognize whether a user-input digit is in the array. I have not added the user-input part of the code yet due to the 123 search not compiling. Any help would be appreciated.

EDIT: I have completed the first section, finding the 123 digit in the array (I will post my updated code) and now I just need the user input binary search. Any suggestions on how this is done?
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
91
92
93
94
95
96
//This program uses a binary search to find a number which is constituted of the last 3 digits of my UIN, 224001123. It will also find a value inputted by the user if it is in the array

#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void selectionSort(int [], int);
void showArray(int [], int);
int binarySearch(int [],int,int);
int main()
{
	unsigned seed=time(0);
	srand(seed);
	//size  declaration of array
	const int SIZE=100;
	int array[SIZE];
	//random numbers assigned
	for(int i=0; i<=99; ++i)
		{
			array[i]=rand() %1000+1;
			cout<<array[i]<<" "<<endl;
		}
	//selection sort call
	selectionSort(array, SIZE);
	//values of ascending sort
	cout<< "The ascending sorted values are: \n";
	showArray(array,SIZE);

	//Binary search
	int results;
	results=binarySearch(array, SIZE, 123);
	if(results==-1)
		cout<<"The number 123 was not located in the array"<< endl;
	else
	{
		cout << "123"<<endl;
		cout <<"The number was found"<<endl;
	}
	system("pause");
return 0;
}
//Array function
void showArray(int array[] , int size)
{
	   for (int count = 0; count < size; count++)
	          cout << array[count]<<" "<<endl;
	   cout << endl;
} 

//Binary search for 123
int binarySearch(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;
}
	 
//Selection sort ascending function
void selectionSort(int array[], int size)
	{
	   int startScan, minIndex, minValue;
	   for ( startScan = 0; startScan < (size -1); startScan++)
		{
		minIndex = startScan;
		minValue = array[startScan];
	   
	      for (int index = startScan + 1; index < size; index++)
	       {
	          if (array[index] < minValue)              
	           {
	            minValue = array[index];
	            minIndex = index;
	           }
	       }
	      array[minIndex] = array[startScan];    
	      array[startScan] = minValue;
	   }

}
Last edited on
Topic archived. No new replies allowed.