Solved

problem in using qsort

Posted on 2001-06-12
4
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Accepted Solution

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

Expert Comment

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

Author Comment

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

Expert Comment

by:jasonclarke
ID: 6185426
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 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