Hello everyone, I have some problems with getting this program to work.
I can't get the loop right and it won't run as i wanted it to.
I can write the basic stuffs but when it comes to loop i dont really know how to get it work right. If you could help me i would really appreciate it.
#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;
/* 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
*/
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");
system("PAUSE");
return 0;
}
try keeping it simple to start off with. In other words do a single if test first and once that's working put a while loop around it. Most people have trouble because they nest all sorts of control loops all at the same time and it is hard to debug. :-)