0

Here is my code:

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

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;
}

and here is my output: 0.7000000.2000000.800000 Sorted Array:0.7000000.2000000.800000

Can anyone tell why is qsort() not working for me?

3
  • 6
    Comparison functions must return int (not float) Commented Apr 12, 2014 at 8:24
  • See it live Commented Apr 12, 2014 at 8:24
  • @AlterMann Hail! Thankq Commented Apr 12, 2014 at 8:32

1 Answer 1

0

Comparison functions must return int (not float). So what you have to do is, change this: float comp (const void * elem1, const void * elem2) { to int comp (const void * elem1, const void * elem2) { That's it! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.