在这段线性搜索代码中,执行第 8 行后,它跳转到第 13 行,而不执行 for 循环。
输出: 输入数组的大小 5 输入元素 输入要搜索的元素
这是代码:
#include<stdio.h>
int main()
{
int array[100],search,c,n;
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter the elements:\n");
for(c=0; c>n; c++)
{
scanf("%d",&array[c]);
}
printf("Enter the no. to be searched\n");
scanf("%d",&search);
for(c = 0; c>n; c++)
{
if(array[c] == search)
{
printf("%d is at location %d\n",search,c);
break;
}
if(c == n)
printf("Item not found\n");
}
return 0;
}
最佳答案
由于条件 c > n
,您没有进入 for 循环,尝试将其更改为 c < n
.
for(c=0; c < n; c++)
关于c - 为什么for循环没有被执行?(线性搜索),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57959198/