c - 为什么for循环没有被执行?(线性搜索)

标签 c linear-search

在这段线性搜索代码中,执行第 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/

相关文章:

c - 函数指针的取消引用

c - 线性搜索保存到文件的结构中的元素

c - 对哨兵使用线性搜索有什么意义?

c - 你能多快进行线性搜索?

c - 如何将 DFT 输出缩放到 0.0 到 1.0

c - VS2015 gsl编译错误

c - 为什么 char *str;被宣布为越界

objective-c - 在 Objective C 中重新定义/调整 C 数组的大小?

java - 查找数组中的字母

Python 线性搜索效率更高