the program wont run properly.... it shows only the first sentence and thats it.... nothing happen.
I think the loop is wrong. but i dont know where.
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
|
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define NUMEL 10
int main()
{
int nums[NUMEL] = {5, 10, 22, 32, 45, 67, 73, 98, 99, 101};
int item, location;
printf("Enter the item you are searching for: ");
scanf("%d", &item);
int arrayIndex=0;
int index = -1;
int found = FALSE;
int lowerIndex=0;
int upperIndex=nums[NUMEL]-1;
int midpoint=(lowerIndex+upperIndex)/2;
while(lowerIndex<=upperIndex && found==FALSE)
{
if(item=midpoint)
{
found==TRUE; }
else if(item>midpoint)
{
lowerIndex=midpoint+1;
}
else if(item<midpoint)
{
upperIndex=midpoint-1;
}
}
location=arrayIndex;
if(location>-1)
{printf("The item was found at index location %d\n", location);}
else
{ printf("The item was not found in the list\n");}
return 0;
}
|
The loop should be like this...
/* Set the lower index to 0
Set the upper index to one less than the size of the list
While the lower index is less than or equal to the upper index and a match is not yet found...
Set midpoint index to the integer average of the lower and upper index values
Compare the desired item to the midpoint elements
If the desired elements equals to the midpoint element
the item has been found, Set "Found flag to TRUE"
Else if the desired element is greater than the midpoint element
Set the lower index value to the midpoint value plus 1
Else if the desired element is less than the midpoint element
Set the upper index value to the midpoint value less 1
EndIF
EndWhile
*/