这是我的代码:
#include #include float comp (const void * elem1, const void * elem2) { float f = *((float*)elem1); float s = *((float*)elem2); if (f > s) return 1; if (f < s) return -1; return 0; } int main(void) { int t, n, temp, temp1, x; float input[2][50][1000]; scanf("%d", &t); for(temp=0; temp<t; temp++){ scanf("%d ", &n); for(temp1=0; temp1<n; temp1++){ scanf("%f", &input[0][temp][temp1]); } for(temp1=0; temp1<n; temp1++){ scanf("%f", &input[1][temp][temp1]); } for(x=0; x<temp1; x++){ printf("%f", input[0][temp][x]); } qsort (input[0][temp], n, sizeof(*input[0][temp]), comp); printf("n Sorted Array:"); for(x=0; x<temp1; x++){ printf("%f", input[0][temp][x]); } } return 0; }
这是我的输出:0.7000000.2000000.800000排序数组:0.7000000.2000000.800000
任何人都可以告诉为什么qsort()不适合我?
比较函数必须返回int
(不是float
)。 所以你需要做的是,改变这个: float comp (const void * elem1, const void * elem2) {
to int comp (const void * elem1, const void * elem2) {
就是这样! ?
以上就是c/c++开发分享qSort not Sorting my array相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/522370.html