阵列分配后,scanf挂起...... 0.o?

时间:2014-04-16 05:07:01

标签: c arrays scanf

我运行此代码并询问用户输入,然后在开始将所有这些数字输入数组位置时挂起,我在这里放置了一些测试以帮助我找出导致挂起的原因。我无法弄明白。它要么是愚蠢的容易,要么是我的编译器(Code :: Blocks)。

    // Chapter 9 Programming Project #1

    #include <stdio.h>

    #define N 10

    void selection_sort(int n, int a[]);

    int main(void)
    {
        int i, a[N];

        printf("Enter a series of integers: ");
        for (i = 0; i < N; i++) {
            scanf("%d", &a[i]);
            printf("Scanf Test #%d and %d\n", i, a[i]);
        }

        printf("Scanf Test\n");
        selection_sort(N - 1, a);

        // iterate through array
        for (i = 0; i < N; i++) {
            printf("%d ", a[i]);
        }
        printf("\n");

        return 0;
    }

    void selection_sort(int n, int a[n])
    {
        // Search for the highest value
        // Place that value at the end
        // Call the function with the end of the array removed
        // Create a way for it to break the cycle
        int i, temp, high = n;
        printf("Function Called\n");
        for (i = 0; i < n; i++) {
            printf("Test: %d", i);
            if (a[i] > a[high]) {
                temp = a[high];
                a[high] = a[i];
                a[i] = temp;
                printf("Test Pass: %d\n", i);
                if (n - 1 > 1)
                    selection_sort(n - 1, a);
            }
        }
    }

注意::此程序反复打印Scanf Test #0 and 1,直到最后一个数字被分配到a[i]中的位置....其中#0 and 1,1将是第一个数字用户已输入

编辑:为了清除混淆,如果用户只输入1个号码&#34; 1&#34;,那么程序将分配该值和/或输入的所有其他值,这可以通过&#验证34; Scanf测试&#34;我输入的内容将打印数组中的位置,后跟分配给它的数字。在将所有值分配给数组之后,理论上它应该运行下一个测试,该测试位于for循环的正下方。问题是它没有,程序反而在打印出最后一个位置和分配的数字后挂起......

该程序没有做任何其他事情,只是坐在那里,下面有一个人说他运行这个代码并且工作正常......这让我相信这是我的IDU中的编译问题(Code ::块)

2 个答案:

答案 0 :(得分:1)

  

该程序打印&#34; Scanf测试#0和1&#34;一遍又一遍

这是不检查scanf结果的结果。如果scanf无法读取整数,则会使流保持不变并报告失败。你没有检查过这个,所以它工作了一次,失败了9次。

如果输入10个整数,它将正常工作;但是很明显你要输入一些其他东西,比如字母会导致整数提取失败。

如果格式字符串中有单个转化规范("%d"),则scanf可以返回:

  • 1 - 成功
  • 0 - 输入垃圾字符
  • EOF - 流错误

您应该检查此值并采取适当的措施。如果程序不是1,你可以直接中止该程序,尽管如果要清除垃圾并在0的情况下再次询问,则可以直接中止。

转到实际排序。将N-1传递给函数时,您的界面很奇怪,然后让函数访问超出数组末尾的1项。做selection_sort(N, a),然后在函数high = n-1for (i = 0; i < n-1; i++)等内部会更清楚。

现在,该行存在一个微妙的问题:

void selection_sort(int n, int a[n])

此行与原型不匹配;您可以通过替换以下行来解决您的问题:

void selection_sort(int n, int a[])

原始版本导致程序对我来说意外行为,但前提是我在默认模式下调用gcc(即不符合C标准)。必须存在某种类型的GNU扩展,这会导致int a[n]出现问题。

在标准C中,原始代码应该有效(并且,如果通过-std=c99在标准模式下调用gcc,那么我强烈建议您这样做。)

答案 1 :(得分:0)

我知道我这样做的方式有点不同寻常,但我只能用到目前为止学到的东西。这样做允许用户输入任意数量的整数,一个或两个数字的数字,而不必担心输入特定数量的值......请随意批评,添加或改进我的方法。< / p>

感谢您帮助我。

    // Chapter 9 Programming Project #1

    #include <stdio.h>
    #include <ctype.h>

    #define N count

    void selection_sort(int n, int a[]);

    int main(void)
    {
        int i, temp, temp2, a[20], count_dgts = 0, count = 0;
        char ch;

        printf("Enter a series of integers\n");
        printf("Enter only one or two digit numbers: ");
        ch = getchar();
        for (i = 0; ch != '\n';) {
            switch (ch) {
                case '0': temp = 0; count_dgts += 1; break;
                case '1': temp = 1; count_dgts += 1; break;
                case '2': temp = 2; count_dgts += 1; break;
                case '3': temp = 3; count_dgts += 1; break;
                case '4': temp = 4; count_dgts += 1; break;
                case '5': temp = 5; count_dgts += 1; break;
                case '6': temp = 6; count_dgts += 1; break;
                case '7': temp = 7; count_dgts += 1; break;
                case '8': temp = 8; count_dgts += 1; break;
                case '9': temp = 9; count_dgts += 1; break;
                default: count_dgts = 0; break;
            }
            ch = getchar();
            if (count_dgts == 2) {
                a[i++] = (temp2 * 10) + temp;
                count += 1;
            }
            if (ch != ' ' && count_dgts == 1) {
                temp2 = temp;
            }
            if ((ch == ' ' && count_dgts == 1) ||
                (ch == '\n' && count_dgts == 1)) {
                a[i++] = temp;
                count += 1;
            }
        }
        selection_sort(N, a);
        // iterate through array
        printf("\nYour numbers in ascending order are: \n");
        for (i = 0; i < N; i++) {
            printf("%d ", a[i]);
        }
        printf("\n");
        return 0;
    }

    void selection_sort(int n, int a[])
    {
        // Search for the highest value
        // Place that value at the end
        // Call the function with the end of the array removed
        // Create a way for it to break the cycle
        int i, temp, high = n - 1;
        for (i = 0; i < n; i++) {
            if (a[i] > a[high]) {
                temp = a[high];
                a[high] = a[i];
                a[i] = temp;
                if (n - 1 > 1)
                    selection_sort(n - 1, a);
            }
        }
    }
相关问题