Hello

I have hit a wall with this issue, been at it for a few hours now and it is bewildering to me to say the least
With that being said, the below code is giving me some erroneous answer during the execution.

The user enters a number from the Array list or a number that is not on the array list and the program is supposed to tell whether the number exist in the array and where it is located at what index.

The strange thing is that if you notice the 4th index, which contains the number 3302, if you run the program and enter the search value of 3302, the program gives out the appropriate answer...

...says 3302 found at index=4.

If you run the program and enter a number which isn't in the list, it says "not found", which is also CORRECT.

However, the issue arises when you enter any other number into the search value say, 1681 or 7140 or 2309, all of which are in the array list but the program skips over them as if they are not there and says "Not Found".

I have run the debugger and from what I have investigated, somehow, my function definition is not giving me the correct answer. It is saying that my "if(first <= last)" condition is not true, skipping the rest of the function instructions and returning a -1; however it also does that when I enter 3302 yet returns accurately as it is supposed to. Now this is what I have determined from looking at the debugger; however, I know that I could be mistaken on that. Something tells me that the answer is pretty basic and I am just not seeing it.

Can anyone enlighten me on what is going on.

Thanks!

#include<iostream>
using namespace std;

//declare function
int binSearch (int A[], int size, int tarVal);


void main()
{
  const int Array_Size=10;
  int myArray[Array_Size]={1681, 2106, 2309, 3184, 3302, 4190, 4498, 6512, 7140, 9963};

	cout << "The "<< Array_Size << " array elements are: " << endl;
	for (int i=0; i<Array_Size; i++)
		{
			cout << myArray[i] << " ";
		}
	
	
			cout << endl << "Enter the target value: ";
			int tarVal;
			cin >> tarVal;
		
	
	int index = binSearch(myArray, Array_Size, tarVal);

	if (index > 0)
	{
		cout << "Target value= " << tarVal << "  found at array index " << index << endl;
	}
	else
		cout << "Target Value =  " << tarVal << " not found " << endl;
		
}
int binSearch (int A[], int size, int tarVal)
	{
		int first=0, last=size-1, mid;

		if(first <= last)		
			{
				mid = (first + last)/2;
			}
			if (tarVal < A[mid])
			{
				last = mid-1;
			}
			else if (tarVal > A[mid])
			{
				first = mid+1;
			}
			else return mid;
			{
				return -1;
			}
		
					
	}

Edited 5 Years Ago by hardknock: n/a

Binary Search continuously divides the array and keep on searching for the target value whereas in your code the function doesn't do that. It just checks whether the target value is at mid and then changes first and last and then terminates. You would like it to continue searching rather than just doing it once.

Hint: Use loop, rather than conditional statement.

Edited 5 Years Ago by vidit_X: n/a

Thanks, I realized my mistake about 30 minutes ago...i removed the if and placed a do/while loop and it ran accordingly...thanks

This article has been dead for over six months. Start a new discussion instead.