Thread: Could use some help with qsort

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    49

    Could use some help with qsort

    I'm having trouble setting this up correctly, could someone point out my errors ?
    Code:
    #include <stdio.h>
    double arrayOne[] = {2.3, 5.9, 2.7, 1.9, 3.6, 8.10},
             arrayTwo[] = {6.1, 0.2, 3.5, 2.8},
             arrayOutput[20];
    
    
      int cmpfunc (const void * a, const void * b)
      {
        return ( *(double*)a - *(double*)b );
      }
    
    
    int main(void){
      int k;
       printf("SORT AND UNIFY ARRAYS\n\n");
       printf("Prior to sorting arrayOne look like this ");
       for(k = 0; k < 6; ++k)
         printf("%.1f  ", arrayOne[k]);
       qsort(arrayOne, 6, sizeof(double), cmpfunc);
       printf("\n\nAfter sorting arrayOne looks like this ");
       for(k = 0; k < 6; k++)
         printf("  %3.1f", arrayOne[k]);
    
    
        return 0;
    }
    results from running it
    SORT AND UNIFY ARRAYS


    Prior to sorting arrayOne look like this 2.3 5.9 2.7 1.9 3.6 8.1


    After sorting arrayOne looks like this 1.9 2.7 2.3 3.6 5.9 8.1

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think your cmpfunc looks a bit suspicious. It should return a negative integer if the numbers are less than, a positive integer if they are greater than, otherwise the integer 0 if they are equal. Try this

    Code:
    int cmpfunc (const void * a, const void * b)
    {
    	const double *ad = a;
    	const double *bd = b;
    	if (*ad > *bd) {
    		return 1;
    	}
    	else if (*ad < *bd) {
    		return -1;
    	}
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    49
    It functions now, thank you kind sir.

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    You'll need to include <stdlib.h> to make the declaration for qsort available as well. It's also a good idea to terminate your program's output with a new-line character.
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    49
    what is a new line character ?

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    '\n'
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by Barney McGrew View Post
    '\n'
    I can do ASCII art as well. \o/ @}--'--,'---- (that's a flaming sword with an orb on the pommel)

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    49
    This program works just fine now, thanks by the way but when i call my qsort i get the error, "passing argument 4 of 'qsort' from incompatible pointer type". I would like to hear if anyone knows why this happens.
    Thanks for helping a beginner out!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 5
    
    
      int cmpfunc (const int *element1 , const int *element2 ){
        if (*element1 > *element2)
          return 1;
        else if (*element1 < *element2)
          return -1;
    
    
        return 0;
      }
    
    
    
    
    void arrayRemoveDuplicate(int array[], int *sizeAfter){
      int a, b, c, size = SIZE;
      for(a = 0; a < size; ++a){
        for(b = a + 1; b < size;){
          if(array[b] == array[a]){
            for(c = b; c < size; ++c)
              array[c] = array[c + 1];
              --size;
              (*sizeAfter -= 1);
          }
          else
          ++b;
        }
      }
    }
    
    
    int main(void){
      int k, sizeA = SIZE, array[SIZE];
      printf("Input integer elements for %d element array\n", SIZE);
      fflush(stdout);
      for(k = 0; k < SIZE; ++k)
        scanf("%d", &array[k]);
      arrayRemoveDuplicate(array, &sizeA);
    
    
      printf("After removing duplicates the array has %d elements and looks like this\n", sizeA);
      for(k = 0; k < sizeA; ++k)
        printf("%d   ", array[k]);
      printf("\nNow lets sort it in ascending order\n");
      qsort(array, sizeA, sizeof(int), cmpfunc);
      for(k = 0; k < sizeA; ++k)
        printf("%d  ", array[k]);
    
    
    
    
        return 0;
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > int cmpfunc (const int *element1 , const int *element2 ){
    Because qsort() expects these to be const void* pointers.
    You have to do the casting within the function itself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort help
    By bbray in forum C Programming
    Replies: 11
    Last Post: 09-18-2011, 01:25 PM
  2. Help with qsort
    By muqing in forum C Programming
    Replies: 18
    Last Post: 04-12-2010, 02:16 AM
  3. Help here with qsort!
    By xxxrugby in forum C Programming
    Replies: 11
    Last Post: 02-09-2005, 08:52 AM
  4. Qsort
    By shanen98 in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2005, 12:31 PM
  5. qsort
    By dayknight in forum C++ Programming
    Replies: 9
    Last Post: 09-17-2004, 02:06 PM

Tags for this Thread