Having trouble with program code. No errors detected from compiler; however, RUN always return "Target Found" even if target is not within the array. Any help in the right direction will be greatly appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int iterativeBinary(int* arr, int left, int right, int target);
int main()
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int target = 0;
int i = 0;
printf("IND: ");
for (i = 0; i <= 9; i++)
{
printf("[%d] ", i);
}
printf("\nARR: ");
for (i = 0; i <= 9; i++)
{
printf("[%d] ", arr[i]);
}
printf("\nSearch for Target: ");
scanf("%d", &target);
if (iterativeBinary == FALSE)
printf("Target NOT found.\n");
else
printf("Target found.\n");
system("pause");
return 0;
}
int iterativeBinary(int* arr, int left, int right, int target)
{
int middle;
while(left <= right && arr[middle] != target)
{
if(target > arr[middle])
left = middle + 1;
else
right = middle - 1;
}
if (arr[middle] == target)
return TRUE;
else
return FALSE;
return;
}
Thank you.