Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem using qsort(). Please help!

Reply
Thread Tools

Problem using qsort(). Please help!

 
 
dandelion
Guest
Posts: n/a
 
      12-08-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

<snip quote only post>

Yessss..... ?


 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-08-2004
Ravi Uday <(E-Mail Removed)> writes:
> Lawrence Kirby wrote:

[...]
>> The function is supposed to compare the elements pointed at by arg1
>> and
>> arg2. This doesn't even look at arg2.
>>

> Oops ! apologies.
> It should have been
>
> int compare( const void *arg1, const void *arg2 )
> {
> GROUPED *left = (GROUPED *)arg1;
> GROUPED *right = (GROUPED *)arg1;
>
> if (left->val > right->val)
> return 1;
> if (left->val < right->val)
> return -1;
>
> return 0;
> }


This still doesn't look at arg2, but it's just an easily correctable
typo.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Ravi Uday
Guest
Posts: n/a
 
      12-09-2004


Keith Thompson wrote:
> Ravi Uday <(E-Mail Removed)> writes:
>
>>Lawrence Kirby wrote:

>
> [...]
>
>>>The function is supposed to compare the elements pointed at by arg1
>>>and
>>>arg2. This doesn't even look at arg2.
>>>

>>
>>Oops ! apologies.
>>It should have been
>>
>>int compare( const void *arg1, const void *arg2 )
>>{
>>GROUPED *left = (GROUPED *)arg1;
>>GROUPED *right = (GROUPED *)arg1;

^^^^
Yes, it should have been 'arg2'
sorry!
>>
>> if (left->val > right->val)
>> return 1;
>> if (left->val < right->val)
>> return -1;
>>
>> return 0;
>>}

>
>
> This still doesn't look at arg2, but it's just an easily correctable
> typo.
>


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      12-09-2004
On Wed, 08 Dec 2004 17:38:47 +0530, Ravi Uday wrote:

....


> Oops ! apologies.
> It should have been
>
> int compare( const void *arg1, const void *arg2 )
> {
> GROUPED *left = (GROUPED *)arg1;
> GROUPED *right = (GROUPED *)arg1;


Make that arg2.

>
> if (left->val > right->val)
> return 1;
> if (left->val < right->val)
> return -1;
>
> return 0;
> }


That looks good but don't cast away const when you don't need to, and you
very rarely need to.

int compare( const void *arg1, const void *arg2 )
{
const GROUPED *left = arg1;
const GROUPED *right = arg2;

if (left->val > right->val)
return 1;
if (left->val < right->val)
return -1;

return 0;
}

You don't then even need the cast.

Lawrence
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-09-2004
Ravi Uday wrote:
> Keith Thompson wrote:
>> Ravi Uday <(E-Mail Removed)> writes:
>>> Lawrence Kirby wrote:

>>
>> [...]
>>
>>>> The function is supposed to compare the elements pointed at by
>>>> arg1 and arg2. This doesn't even look at arg2.
>>>
>>> Oops ! apologies.
>>> It should have been
>>>
>>> int compare( const void *arg1, const void *arg2 )
>>> {
>>> GROUPED *left = (GROUPED *)arg1;
>>> GROUPED *right = (GROUPED *)arg1;

> ^^^^
> Yes, it should have been 'arg2'


However it should not have any cast. And the local variables
should have the 'const' attribute.

const GROUPED *left = arg1, *right = arg2;

and you can avoid instruction stream flushing with:

return (*left > *right) - (*left < *right);

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Ravi Uday
Guest
Posts: n/a
 
      12-10-2004
<snip>

>>>>int compare( const void *arg1, const void *arg2 )
>>>>{
>>>>GROUPED *left = (GROUPED *)arg1;
>>>>GROUPED *right = (GROUPED *)arg1;
>>>

>> ^^^^
>>Yes, it should have been 'arg2'

>
>
> However it should not have any cast. And the local variables
> should have the 'const' attribute.
>
> const GROUPED *left = arg1, *right = arg2;
>
> and you can avoid instruction stream flushing with:
>
> return (*left > *right) - (*left < *right);
>


You mean
return (*left->val > *right->val ) - (*left->val < *right->val);

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-10-2004
Ravi Uday wrote:
>
> <snip>
>
>>>>> int compare( const void *arg1, const void *arg2 )
>>>>> {
>>>>> GROUPED *left = (GROUPED *)arg1;
>>>>> GROUPED *right = (GROUPED *)arg1;
>>>>
>>> Yes, it should have been 'arg2'

>>
>> However it should not have any cast. And the local variables
>> should have the 'const' attribute.
>>
>> const GROUPED *left = arg1, *right = arg2;
>>
>> and you can avoid instruction stream flushing with:
>>
>> return (*left > *right) - (*left < *right);

>
> You mean
> return (*left->val > *right->val ) - (*left->val < *right->val);


Thanks for the correction. Please maintain attributions for
material you quote.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please please please help this guy with his open source java app casioculture@gmail.com Java 4 05-05-2005 08:24 AM
Console profile for Windows app in VC++ - PLEASE PLEASE PLEASE HELP! MuZZy C++ 7 01-07-2005 08:40 PM
Computer problems please please please help Nick Computer Support 0 06-04-2004 08:49 PM
HELP! HELP! PLEASE, PLEASE, PLEASE tpg comcntr Computer Support 11 02-15-2004 06:22 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments