1

I have this code for sort array polsum:

int comp (const void * a, const void * b){   
   double aa = *(double*)a, bb = *(double*)b;

   if (aa < bb) return -1;
   if (aa > bb) return  1;
   return 0;
}

double sort(double *polsum){                              
    int p;
    qsort(polsum, sizeof(double),sizeof(double), comp);
    return 0;
}

But output is:

5,01/
80,86/
85,01/
85,01/
300,88/
600,88/
77888,88/
100400,00/
670,88/
80,86/
80,86/

Where have I made a mistake?

1
  • You're not showing us your full code. Commented Apr 8, 2013 at 18:30

1 Answer 1

1

Your passing in sizeof(double) for the count of elements to be sorted. sizeof(double) == 8

Consider adding the following to your sort function:

double sort(double *polsum, int count) {
    int p;
    qsort(polsum, count, sizeof(double), comp);
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

but why? how can I repair this please? I really don't know
Your passing in an array of doubles, you should, at some point know how many elements there are. You should pass in the number of elements to your sort function.

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.