I run this code and it asks for the users input and then hangs when it starts entering all those numbers into the array position, I placed some tests in here to help me figure out what was causing the hang. I can't figure it out. It's either something stupidly easy or it's gonna be something with my compiler(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);
            }
        }
    }

Note::This program prints Scanf Test #0 and 1 over and over until the last number has been assigned to its position in a[i]....where #0 and 1, 1 would be the first number the user entered

Edit: To clear up confusion, if the user only entered 1 number being "1", then the program would assign that value and/or all other values entered and this can be verified with the "Scanf Test" that I entered, which will print the position in the array followed by the number which was assigned to it. After all values have been assigned to the array, in theory it should run the next test which is right below that for loop. The problem is that it doesn't, the program instead hangs after printing out the last position and number that was assigned....

The program doesn't do anything else but just sit there, one person below stated he ran this code and it worked fine...which leads me to believe this is a compiling issue in my IDU (Code::Blocks)

有帮助吗?

解决方案

This program prints "Scanf Test #0 and 1" over and over

This is a consequence of not checking the result of scanf. If scanf cannot read an integer, then it leaves the stream untouched and reports failure. You don't check for this, so it worked once and failed 9 times.

If you enter 10 integers it will work fine; however clearly you're entering some other stuff such as letters which is causing the extraction of an integer to fail.

When there's a single conversion specification ("%d") in the format string, scanf can return:

  • 1 - success
  • 0 - junk characters entered
  • EOF - stream error

You should check this value and take appropriate action. You could just abort the program if it is not 1, although it would be pro to clear the junk and ask again in case 0.

Moving onto the actual sort. Your interface is weird as you pass N-1 to the function, and then have the function access 1 item beyond the end of the array. It would be clearer to do selection_sort(N, a), and then inside the function, high = n-1 and for (i = 0; i < n-1; i++) etc.

Now, there is a subtle issue with the line:

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

This line does not match the prototype; you can fix your problem by replacing the line with:

void selection_sort(int n, int a[])

The original version caused the program to behave unexpectedly for me, but only if I invoke gcc in default mode (i.e. not C-standard compliant). There must be some sort of GNU extension in play that is causing trouble with int a[n].

In Standard C, the original code should work (and does, if gcc is invoked in standard mode via -std=c99, which is something I'd strongly recommend doing anyway).

其他提示

I know the way I did this is a bit unconventional, but I could only use what I have learned so far. Doing it this way allows the user to enter any number of integers, one or two digit numbers, and not have to worry about entering a specific number of values...Please feel free to criticize and add to or improve on my method.

Thanks for helping me with this.

    // 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);
            }
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top