【问题标题】:value searching in an ascending arrays in two different ways以两种不同的方式在升序数组中搜索值
【发布时间】:2017-03-26 06:30:01
【问题描述】:

我正在为我的 C 类做一个项目。我们将在具有 10 个不同数字的升序数组中查找值,然后搜索用户想要的值并返回搜索数字的索引。第一种方法称为线性搜索,它将数组中的每个元素与用户想要的值进行比较。第二种方法称为二进制,您将中间索引与搜索值进行比较。如果 List[middle] = 目标元素,则返回变量 middle,它是元素的索引。如果目标元素大于 List[Middle],则继续数组右半部分的处理。如果它较小,则在数组的左半部分继续相同的过程。减半过程仅在“左”索引变量小于或等于“右”索引变量之前完成。如果没有找到目标元素,则返回-1。

#include<stdio.h>
int main()
{
    int array[10];
    int i = 0,value;
    printf("Please enter 10 distinctive postive numbers with ascending             order.");
    for(i;i<10;i++)
    {
        scanf("%d",&array[i]);
    }
    printf("What value are you seaching for?");
    scanf("%d",value);
    printf("searchLinear(value,*array[10],10)");

    return 0;   
}

int searchLinear(int s,int *list[10],int n){
    list[n];
    int i = 0;
    for(i;i<n;i++)
    {
        if(s == *list[i])
            return *list[i];
    }
    if(i = n)
        return -1;
}   
int searchBinary(int s, int *list[10],int n) {
    list[n];
    int i,left,right,middle;
    left = 0;
    right = n-1;

    for(i = 0;i<n/2;i++)
    {
        middle = (left + right)/2;

        if(*list[middle] > s)   
            right = middle;
        else if(*list[middle] < s)
            left = middle;
        else if(*list[middle] = s)
            return *list[middle];

        if(left == right)
            return -1;  
    }
}

这是我的代码,看起来它陷入了无限循环。我该如何解决?

【问题讨论】:

  • if(i = n) -->> if(i == n)
  • 真的!谢谢!我仍在尝试弄清楚如何运行它。
  • 打开警告,通常意味着使用-Wall标志编译,并修复所有警告。他们会解释为什么你的代码不工作(或者至少是第一组基本问题)。
  • 还有一个:else if(*list[middle] = s) -->> else if(*list[middle] == s)(可能有害)顺便说一句:int searchBinary() 函数有时会返回一个随机值。
  • -wall flag 是什么意思?

标签: c arrays function pointers


【解决方案1】:

更正后的代码是

   #include<stdio.h>
    int searchLinear(int s,int list[],int n);
    int searchBinary(int s, int list[],int n);   //you need to give forward declaration of funcs

int main()
    {
        int array[10];
        int i = 0,value;
        printf("Please enter 10 distinctive postive numbers with ascending       order.");
        for(i;i<10;i++)
        {
            scanf("%d",&array[i]);
        }
        printf("What value are you seaching for?");
        scanf("%d",&value);        //you missed & here
        printf("%d",searchBinary(value,array,10));

        return 0;   
    }

    int searchLinear(int s,int list[],int n){      // you can use this format of arguments
        //list[n];        //what did you wanted to do with this line
        int i = 0;
        for(i;i<n;i++)
        {
            if(s == list[i])
                return i;             //return index not value
        }
        if(i = n)
            return -1;
    }   
     int searchBinary(int s, int list[],int n){
        //list[n];
        int i,left,right,middle;
        left = 0;
        right = n-1;

        for(i = 0;i<n/2;i++)
        {
            middle = (left + right)/2;
             if(left > right)      //you have to check left>right, instead of left==right
                return -1;  
            if(list[middle] > s)   
                right = middle-1;
            else if(list[middle] < s)
                left = middle+1;
            else if(list[middle] == s)    //== is used for comparision
                return middle;

        }
    }

观察 cmets 中的修正

【讨论】:

  • 多么出色的解释。我忘了声明函数。我不知道为什么我之前没有声明并且它运行得很好。我还尝试在两个函数中的 list[],(int *list[]) 之前开始运行程序。但我无法弄清楚。它会生成以下错误:search.c:17:3: 警告:传递 'searchLinear' 的参数 2 使指针从没有强制转换的整数 [默认启用] search.c:2:5: note: expected 'int ** ' 但参数的类型是 'int'
  • 这是因为int list本身就是一个指针,在它之前加一个星号使它成为指针的指针,当你将list[]传递给它时,它给出了转换错误
  • 如果答案有帮助,您可以点赞并将其标记为正确,这样对其他用户更有益
  • 非常感谢!你的解释太棒了!我将 urs 标记为正确答案!
猜你喜欢
  • 1970-01-01
  • 2016-10-03
  • 2016-09-23
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2013-06-21
  • 2011-08-23
  • 2012-07-27
相关资源
最近更新 更多