qsort 与动态分配

#include
#include

int comp(const void *a, const void *b) {
    return *(int *) a - *(int *) b;
}

int main() {
    int *array;
    int n;
    scanf("%d", &n);
    array = (int *) malloc(n * sizeof(int));
    for (int i = 0; i < n; i++) {
        scanf("%d", (array + i));
    }
    qsort(array, n, sizeof(int), comp);
    for (int i = 0; i < n; i++) {
        printf("%d\t", array[i]);
    }

    float *arrarFloat;
    printf("input number of float:");
    scanf("%d", n);

    arrarFloat = (float *) malloc(n * sizeof(float));
    for (int i = 0; i < n; i++) {
        scanf("%lf", (array + i));
    }

    for (int i = 0; i < n; i++) {
        printf("%lf\t", array[i]);
    }
    printf("exit");
    return 0;
}

你可能感兴趣的:(C++基础,c语言,c++)