Solved

problem in using qsort

Posted on 2001-06-12
4
154 Views
Last Modified: 2010-04-02
Hi,

   I am writing a C++ program with the use of qsort to sort a double array.

   part of the code is as follow:

int CGetAvgTopInfoGain::compare(const void * a, const void * b) {

        double *x, *y;
        x=(double*)a;
        y=(double*)b;

        if (&x<&y)
                return -1;
        else if (&x==&y)
                return 0;
        else if (&x>&y)
                return 1;
}                                      

feature is a double array with size 500;
size is an integer which equals 20;
when I try to call the following method
  qsort (feature_IG, size, SIZE_DOUBLE, compare);

CGetAvgTopInfoGain.cc:74: argument passing to `void *' from `double'
is always resulted. Could you help me to solve this problem?

0
Comment
Question by:godman_polly
4 Comments
 
LVL 2

Accepted Solution

by:
GloriousRain earned 50 total points
Comment Utility
try leave compare function alone, not in CGetAvgTopInfoGain class, godman_polly.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
   if (&x<&y) (and same) -> error!
must be
   if (*x < *y)
0
 

Author Comment

by:godman_polly
Comment Utility
It seems that the problem is not as you suggest.
0
 
LVL 9

Expert Comment

by:jasonclarke
Comment Utility
There is a much better way of sorting stuff than using qsort in modern C++.  You could get rid of your compare function altogether add an include:

#include <algorithm>

and then just call:

std::sort(feature_IG, feature_IG+size);

apart from being far simpler to work with, this will result in *quicker* sorting in most cases.

If you insist on using quicksort, then both above comments appear to be correct.  Your compare function cannot be a member function and you need to use the * operator rather than & to get the actual values of the doubles.


0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now