-2

I just tried to this linear search and it turns out that it displays the else part. And I don't get what I have done wrong. The logic...

#include<stdio.h>
#include<conio.h>
main()
{
      int a[10], i, x, size;
      printf("Enter the size of the array: \n");
      scanf("%d",&size);
      printf("Enter the elements into the array: \n");
      for(i=0; i<size; i++)
      {
               scanf("%d",&a[i]);
      }
      printf("Enter the element to be searched for: \n");
      scanf("%d",&x);
      for(i=0; i<size; i++)
      {
               if(x==a[i])
               {
                   printf("The element is at: %d",i);
                   break;
               }
               else
               {
                   printf("The element is not in the array.");
                   break;
               }
      }
getch();
}
Daniel Victor
  • 649
  • 2
  • 7
  • 11

6 Answers6

3

Because when it checks the first element and sees it isn't the one you're looking for it gets out the loop.

BlackBear
  • 22,411
  • 10
  • 48
  • 86
2

You have the break in both the if and the else statements which means you are not traversing the array but always going out the for loop after the first element.

ouah
  • 142,963
  • 15
  • 272
  • 331
1
  1. You just pass through the array and for each element, you tell, if it is the element or not and in each case you decide if it was globaly found or not. It is not correct. Let's declare a boolean variable, to which you set false at the beginning and if you find the element, you set the varibale to true; at the end if this variable is false, you write the message that you wasn't successfull with the search, not after each element. Break should be left only after success.

  2. You have to check, that size is less than the size of array a;

V-X
  • 2,979
  • 18
  • 28
1

Remove the break statement from else part -
your code is only checking if the first array element is the element to be searched

better modify your loop with following:

    int flag = 0;
    for(i=0; i<size; i++)
    {
        if(x==a[i])
        {
            printf("The element is at: %d",i);
            break;
        }
        else
        {           
            flag = 1;  
        }
    }

    //will print if number is not present in the array
    if(flag !=0 ){
        printf("The element is not in the array.");
    }
exexzian
  • 7,782
  • 6
  • 41
  • 52
1

Change your search loop to this:

  int found = 0;

  i=0;
  while(found == 0 && i < size){
    if(x == a[i]){
        found = 1;
        printf("The element is at: %d",i);
        break;  
    }
    i++;
  }
  if(found == 0)
    printf("The element is not in the array.");
Varaquilex
  • 3,447
  • 7
  • 40
  • 60
  • I guess there is no `bool` data type in C – exexzian Mar 02 '13 at 13:19
  • [Check this out](http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type/1608350#1608350) just for the record. The usage of mine here is from C++. As other suggested, one could represent `bool` type using `int` type with values `0` and `1`. – Varaquilex Mar 02 '13 at 13:22
  • then I suggest you to add this to your answer as well 'coz OP is using TurboC compiler – exexzian Mar 02 '13 at 13:25
  • changed `bool` into `int`. It's better this way, thank you for your suggestion. – Varaquilex Mar 02 '13 at 13:28
0

Here's one way you can do it without having a flag!

You need to perform the check for not in the array, after the loop has completed, and not while it's still in it.

      for(i=0; i<size; i++)
      {
               if(x==a[i])
               {
                   printf("The element is at: %d",i);
                   i = size + 2;
               }
      }

      if (i <= size)
      {
          printf("The element is not in the array.");
      }
ruben2020
  • 1,549
  • 14
  • 24