Thread: trouble with qsort

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    trouble with qsort

    I have written this to test qsort. The array takes the values, but qsort isn't working in my code. I think that there may also be a problem with calloc, becasue I am able to enter more entries than supp;ied by calloc, what do you think?
    Code:
    /*A program that reads into an array created to hold x amount of ints, then order the array using qsort and finally print it out*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    
    int compare_doubles(double *v1, double *v2);
    int read_into_array(double[]);
    void print_array(double[], int);
    
    
    int
    main(int argc, char **argv) {
    	double *array;
    	int n, i;
    	
    	printf("Enter how many values to be stored in the array: ");
    	scanf("%d", &n);
    	
    	array = calloc(n, sizeof(double));
    	
    	for (i = 0; i < n; i++)
    	{
    		array[i]=0;
    	}
    	printf("Now put some values into the array to sort: ");
    	
    	n = read_into_array(array);
    	
    	qsort(array, n, sizeof(*array), compare_doubles);
    	
    	print_array(array, n);
    
    	return 0;
    }
    
    int
    compare_doubles (double *ptr1, double *ptr2) {
    	if (ptr1<ptr2)
    	{
    		return -1;
    	}
    	if (ptr1>ptr2)
    	{
    		return 1;
    	}
    	return 0;
    }
    
    int
    read_into_array(double A[]) {
    	int cntr=0;
    	double number;
    	
    	while (scanf("%lf", &number)==1)
    	{
    		A[cntr]=number;
    		cntr++;	
    	}
    	return cntr;
    }
    
    void print_array(double A[], int n) {
    	int i;
    	
    	for (i=0; i<n; i++) {
    		printf("%.3f   ", A[i]);
    	}
    	printf("\n");
    	return;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Take a good hard look at your comparison function.
    You're not comparing double's, you are in fact comparing pointers!
    Surprise surprise, the pointers are already in order, so no swaps take place.
    You need to compare *ptr1 < *ptr2 etc.

  3. #3
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    yes, I changes the function to :
    Code:
    int
    compare_doubles (const void *a, const void *b) {
    	double *arg1=(double*)a;
    	double *arg2=(double*)b;
    	if (*arg1<*arg2)
    	{
    		return -1;
    	}
    	if (*arg1>*arg2)
    	{
    		return 1;
    	}
    	return 0;
    }
    now it works

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    [QUOTE=qubit67;639319]
    Code:
    double *arg1=(double*)a;
    double *arg2=(double*)b;
    You don't need the (double *) on the right hand side. A void pointer can always be cast silently. But you should be declaring these as "const double *" not just "double *". Surely you get a warning?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by brewbuck View Post
    Quote Originally Posted by qubit67 View Post
    Code:
    double *arg1=(double*)a;
    double *arg2=(double*)b;
    You don't need the (double *) on the right hand side. A void pointer can always be cast silently. But you should be declaring these as "const double *" not just "double *". Surely you get a warning?
    There should't be any warning due to implicit cast - it removes const modifier by force
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vart View Post
    There should't be any warning due to implicit cast - it removes const modifier by force
    You mean explicit I assume? You're right, I'm so used to NOT explicitly casting for a conversion like this that I forgot it wouldn't warn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. C++ link error with qsort
    By bvnorth in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2003, 02:22 AM
  3. qsort problems... help!
    By Manseed in forum C Programming
    Replies: 5
    Last Post: 06-21-2003, 04:39 PM
  4. qsort - newbie trouble with pointers
    By aze in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 03:38 PM
  5. Need help with qsort() and union of structures
    By DanTheMan in forum C Programming
    Replies: 1
    Last Post: 04-02-2002, 08:30 AM