首页 > 解决方案 > 排序函数不会打印排序数组?

问题描述

我的排序函数不会打印排序后的数组?

我正在尝试编写一个程序来收集数组元素,对数组进行排序,然后打印每个元素的阶乘。如果数组未正确排序,我不想超越自己并编写递归函数。对我来说,那种感觉很好;人们批评我使用 while 循环,但我还不知道另一种方法。任何输入表示赞赏。

#include <stdio.h>

int sorter(int numbList[]);
int getData(int numList[]);
//int recursive(int numList[]);

int main(void) {
    int x;
    int numberList[x];

    getData(&numberList[x]);
    sorter(&numberList[x]);

    //recursive(&numberList[x]);
    return 0;
}

//gets user input-data
int getData(int numbList[]) {
    int i;
    int x;
    printf("Enter number of Elements:\n");
    scanf("%d", &x);

    printf("Enter the values for each element starting from first 
element:\n");

    for (i = 0; i < x; i++) {
        scanf("%d", &numbList[i]);
    }

    printf("\nYou have filled the array list with\n");
    for (i = 0; i < x; i++) {
        printf("%d\n", numbList[i]);
    }
    return numbList[x];
}

//sorter function
int sorter(int numbList[]) {
    int x;
    int temp;
    int swapped;

    while (1) {
        swapped = 0;
        for (int i = 0; i < x; i++) {
            if (i > numbList[i + 1]) {
                temp = numbList[x];
                numbList[x] = numbList[x + 1];
                numbList[x + 1] = numbList[x];
                swapped = 1;
            }
            if (swapped == 0) {
                break;
            }
        }
        printf("Array as sorted:\n");
        for (int i = 0; i < x; i++) {
            printf("%d\t", numbList[x]);
        }
        return(numbList[x]);
    }
}

//recursive factorial function
/* int recursive(int numbList[]) {
    int b = 0;
    numbList[b] *= numbList[b - 1];
    return 0;
} */

标签: carrayssorting

解决方案


您的代码中有多个问题:

  • x定义数组时元素的数量未初始化numbList[x]。这具有未定义的行为。你应该传递一个指向计数的指针,getData这个函数应该更新这个值,分配数组,读取值并返回一个指向数组的指针。

  • 你不应该在没有a的情况下打破多行的字符串\

  • 交换码坏了:测试if (i > numbList[i + 1])不正确,应该是

    if (numbList[i] > numbList[i + 1])
    
  • 交换代码应该使用i而不是x作为索引,并且交换代码中的最后一个分配应该存储tempnumbList[i + 1].
  • 内部循环应该停止x - 1以避免读取超出数组的末尾。
  • 你应该让内循环运行到最后并从外循环中断 if swapped == 0

这是一个更正的版本:

#include <stdio.h>
#include <stdlib.h>

int *getData(int *count);
void sorter(int numList[], int count);

int main(void) {
    int x;
    int *numberList;

    numberList = getData(&x);
    if (numberList != NULL) {
        printf("Elements entered:");
        for (int i = 0; i < x; i++) {
            printf(" %d", numberList[i]);
        }
        printf("\n");
        sorter(numberList, x);
        printf("Sorted array:");
        for (int i = 0; i < x; i++) {
            printf(" %d", numberList[i]);
        }
        printf("\n");
        free(numberList);
    }
    return 0;
}

//gets user input-data
int *getData(int *countp) {
    int i, x;
    int *numbList;

    printf("Enter the number of elements: ");
    if (scanf("%d", &x) != 1 || x <= 0) {
        printf("Invalid size:");
        return NULL;
    }
    numbList = calloc(sizeof *numbList, x);
    if (numbList == NULL) {
        printf("Memory allocation error:");
        return NULL;
    }
    printf("Enter the element values: ");
    for (i = 0; i < x; i++) {
        if (scanf("%d", &numbList[i]) != 1) {
            free(numbList);
            return NULL;
        }
    }
    *countp = x;
    return numbList;
}

//sorter function

void sorter(int numbList[], int x) {
    for (;;) {
        int swapped = 0;
        for (int i = 0; i < x - 1; i++) {
            if (numbList[i] > numbList[i + 1]) {
                int temp = numbList[i];
                numbList[i] = numbList[i + 1];
                numbList[i + 1] = temp;
                swapped = 1;
            }
        }
        if (swapped == 0) {
            break;
        }
    }
}

推荐阅读