我只是尝试这样做linear search
,结果证明它显示了该else
部分。而且我不明白我做错了什么。逻辑...
#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();
}