【问题标题】:C Trying to search an array for a specific numberC 尝试在数组中搜索特定数字
【发布时间】:2017-03-13 23:56:12
【问题描述】:

我一直在尝试开发一个对数组进行排序的控制台程序,然后允许用户在数组中搜索特定值。在大多数情况下,排序部分工作正常(虽然我想将其简化为一个 for 循环,但现在必须这样做)。

但是搜索部分每次都会给我数字 6487516,无论我输入什么数字。我确定它与我的函数 find_number 相关,我只是不知道是什么。

#include <stdio.h>
#include <stdlib.h>
#define NOT_FOUND -1
#define num 9

int main(int argc, char *argv[])
{
    int ask,how_many;
    int user_array[num];
    int sorted_array[num];
    int i,temp,junction;

    printf("type 10 numbers with spaces in between then press enter \n, type the numbers again then press enter, after this press q and then press  enter      ");

    for (i = 0; i<=num ; ++i)
        scanf("    %d      ",&user_array[i]);
    //printf("type ");
    for (i = 0; i<=num ; ++i)
        scanf("    %d      ",&sorted_array[i]);

    for (i = 0; i<=num ; ++i)
        printf("    A : %d  ",user_array[i]);

    for (i = 0; i<=num ; ++i)
        printf("    B : %d  ",sorted_array[i]);

    for (i = 0; i <= num; ++i)
    {
        for (junction = 0; junction <= num - i; junction++)
        {
            if (sorted_array[junction] > sorted_array[junction+1] )
            {
                temp = sorted_array[junction];
                sorted_array[junction] = sorted_array[junction+1];
                sorted_array[junction +1] = temp;
            }
        }
    }

    printf (" Left is the Sorted right is the Original");
    for (i = 0; i<= num; ++i)
        printf(" \n %d,         %d   ",sorted_array[i],user_array[i]);

    printf (" What number do you want to search for?\n");
    fflush (stdin);
    scanf (" %d",&ask);
    printf (" how many numbers? \n");
    fflush (stdin);
    scanf (" %d",&how_many);
    int truth = find_number (sorted_array, ask, how_many);
    printf (" %d",&truth);
    return 0;
}

int find_number( const int target[10], int goal, int n)
{
    int z,found = 0,locate;
    int i = 0;
    while (!found && i < n)
    {
        if (target[i] == goal)
            found = 1;
        else        
            ++i;
    }
    if (found)
        locate = i;
    else
        locate = NOT_FOUND;
    return locate;
}

【问题讨论】:

  • for( i = 0; i&lt;=num ; ++i) 应该是 for( i = 0; i&lt;num ; ++i) 。否则你的数组太短了。
  • 顺便说一句,您的问题还不错,但也不是很好。查看代码的格式:太多的空行和损坏的缩进。难以阅读。幸运的是,您已将错误放在第一行 :)
  • 旁白:scanf(" %d ",&amp;user_array[i]); 这样的行是错误的,请删除空格,尤其是尾随的会影响后续输入。前面的那些根本是不必要的(使用%c 格式时可能只有一个)。 scanf("%d", &amp;user_array[i]);
  • 奇怪数字的原因在这里:printf(" %d",&amp;truth); 打印的是truth 的地址,而不是truth 的值。删除&amp; 以获取值。
  • @Jean-François Fabre 好的,所以我进行了更正,但我仍然收到 6487516 号,您认为它指向内存位置吗?

标签: c arrays sorting search


【解决方案1】:

这是C语言中的一个成语:

#define N   20
int array [N];
for (int j=0;  j < N;  ++j)
     //do something with array[j]

请注意,它不是 j &lt;= N。这会导致访问一个分配的数组元素,即array[N]

【讨论】:

    【解决方案2】:

    您的代码中存在多个问题:

    • num 应定义为 10 以使数组具有 10 个元素 (!)

    • 您对数组的迭代次数过多。经典的 C 习语是for (i = 0; i &lt; n; i++) { ... },其中n 是数组的大小,即元素的数量。由于数组基于0,因此数组中最后一个有效索引是n - 1

    • scanf() 格式中有多余的空格,可能是为了美观。尾随有副作用:scanf() 将保持读取输入,直到它得到一些不是空白的东西。不要这样做。

    • fflush(stdin); 调用未定义的行为。删除它。

    • find 函数适用于任何大小的数组,删除原型中硬编码的 10 大小,无论如何都会被编译器忽略。

    这是一个更正的版本:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define NOT_FOUND  (-1)
    #define NUM 10
    
    int find_number(const int *target, int goal, int n);
    
    int main(int argc, char *argv[]) {
        int ask, how_many;
        int user_array[NUM];
        int sorted_array[NUM];
        int i, temp, junction;
    
        printf("type %d numbers with spaces in between then press enter\n"
               "type the numbers again then press enter, "
               "after this press q and then press  enter", NUM);
    
        for (i = 0; i < NUM; ++i)
            scanf("%d", &user_array[i]);
    
        for (i = 0; i < NUM; ++i)
            scanf("%d", &sorted_array[i]);
    
        for (i = 0; i < NUM; ++i)
            printf("    A : %d\n", user_array[i]);
    
        for (i = 0; i < NUM; ++i)
            printf("    B : %d\n", sorted_array[i]);
    
        for (i = 0; i < NUM; ++i) {
            for (junction = 0; junction < NUM - i - 1; junction++) {
                if (sorted_array[junction] > sorted_array[junction + 1]) {
                    temp = sorted_array[junction];
                    sorted_array[junction] = sorted_array[junction + 1];
                    sorted_array[junction + 1] = temp;
                }
            }
        }
    
        printf (" Left is the Sorted right is the Original\n");
        for (i = 0; i < NUM; ++i)
            printf("%5d, %5d\n", sorted_array[i], user_array[i]);
    
        printf(" What number do you want to search for?\n");
        scanf("%d", &ask);
        printf(" how many numbers?\n");
        scanf("%d", &how_many);
        int truth = find_number(sorted_array, ask, how_many);
        printf(" %d\n", &truth);
        return 0;
    }
    
    int find_number(const int *target, int goal, int n) {
        int i;
        for (i = 0; i < n; i++) {
            if (target[i] == goal)
                return i;
        }
        return NOT_FOUND;
    }
    

    注意事项:

    • 您应该将user_array 复制到sorted_array,而不是要求用户再次输入数字。

    • 您应该使用更好的算法在已排序的数组中进行搜索。

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 2020-05-05
      • 2014-05-27
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2022-01-10
      相关资源
      最近更新 更多