Help in Loops

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
*/
Last edited on
You have an assign/compare mismatch:
1
2
3
       if(item==midpoint) // Note: == here
       {
          found=TRUE;  }     // Note: = here 
It still wont work.... i dont think the loop is working properly.
I dont know why.... It won't work.
It still shows only the first printf.
Yes, the next problem is that item and midpoint are not compatible. item is a value inside the array nums while midpoint is the index of the array nums.

So, to compare two values you need nums like so:
if(item==nums[midpoint]) // Note that midpoint is an index

The next problem is line 18. Change it to int upperIndex=NUMEL-1;

you really need to grasp the difference between index and value
Ok, now i got it like this.

if(item==nums[midpoint])
it won't compile.

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
#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=NUMEL-1;
    int num[midpoint]=(lowerIndex+upperIndex)/2;
    
    while(lowerIndex<=upperIndex && found==FALSE)
    {
       if(item==num[midpoint])
       {
          found=TRUE;  }     
          else if(item>num[midpoint])
          {
               lowerIndex=midpoint+1;
           }
          else if(item<num[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;
    
}
it won't compile.

Typically, in order to aid someone in helping you, you would provide the error generated by the compiler when it fails to compile.

What are you trying to accomplish on line 19?
I was trying to find the midpoint in the index.
Guess it didnt work.
Thanks for the advice.
closed account (48T7M4Gy)
You are referring to num and nums which is causing an error?

Also, isn't it midpoint = ... rather than nums[midpoint] in line 19?
Sorry.... my mistake.
i get some ideas but still confused... i need to find max and min in the index then make it into midpoint.
Topic archived. No new replies allowed.