Binary Search

i currently have the below program that uses a sequential search. Pretty much the user enters a number from the first array and the search finds the parallel number from the second array. However, i would like to convert this to binary search and I'm having some issues with the theory of how to do it. I have worked on a function for the binary search itself, its not yet tested but I'm pretty sure the theory on the search its correct. What the function does however is search for whatever target is sent to it. The issue is i need to be searching for the parallel number from the second array and not necessarily a number itself. For example if 4 is entered as the target, i need to return the value 14.6 from the assists array. If anyone can give me some ideas on how to accomplish this i'd be greatful, been working on a solution for several days and haven't had much luck.


below is my binary search 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
	private: int BinSearch(double list[], int n, int target) {

				 //declare low, mid, and high
				 int low = 0, mid, high = n-1;

				 //binary search loop
				 while (low <= high)
				 {
					 //compute mid
					 mid = (low + high) / 2;
					 //check target against list[mid]
					 if (target < list[mid])
						 //if target is below list[mid] reset high
						 high = mid - 1;
					 else if (target > list[mid])
						 //if target is below list[mid]) reset low
						 low = high + 1;
					 else
						 //if target is found set low to exit the loop
						 low = high + 1;
				 }

				 //return true if target found, false if not found
				 if (target == list[mid])
					 return true;
				 else
					 return false;

			 }





This is for the sequential search that i currently have in place and wish to change:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	private: System::Void btnSearch_Click(System::Object^  sender, System::EventArgs^  e) {

				 double found;
				 int mid;
				 int target;
				 int n = 8;
				 int num;
				 double uniform[] = {3, 4, 6, 8, 9, 10, 13, 15};
				 double assists[] = {12.3, 14.6, 2.6, 8.6, 4.7, 6.9, 10.1, 5.5};


				 Int32::TryParse(txtUniform->Text, target);
				 
					//for seq. search
					for (int i = 0; i < 8; i++)
					{
						if (target == uniform[i])
							txtAssists->Text = assists[i].ToString();
					}
				 
			 }
i have resolved this issue
Topic archived. No new replies allowed.