944,174 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4063
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 19th, 2005
0

how do i sort reords from Z-A? (in turbo C)

Expand Post »
anyone Please give me an example code of sorting a records from Z-A.

Can someone point me to some good tutorials so I can learn how to write and compile Turbo C program?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
renar is offline Offline
2 posts
since Sep 2005
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

look for qsort() -- all you have to do is write your own custom comparison function that returns an integer similar to strcmp(). The code below assums you are sorting an int array in descending order.
C++ Syntax (Toggle Plain Text)
  1. int mycomp(const void* p1, const void* p2)
  2. {
  3. int* i1 = (int*)p1;
  4. int* i2 = (int *)p2;
  5. return *i2 - *i1;
  6. }

Or an array of strings, in descending order (I think)
C++ Syntax (Toggle Plain Text)
  1. int mycomp(const void* p1, const void* p2)
  2. {
  3. int x = strcmp((char*)p1,(char*)p2);
  4. return (x < 0) ? 1 : (x > 0) ? -1 : 0;
  5. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

AD: There's no need to cast away constness (it may do no harm, but I avoid things that can be bad habits). And your integer comparison function can be a victim of integer overflow.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

>return *i2 - *i1;
Eew. Can we say integer overflow?

>return (x < 0) ? 1 : (x > 0) ? -1 : 0;
Eeeeew! Nested conditional statements are fugly and hard to follow. How about this:
C++ Syntax (Toggle Plain Text)
  1. int compare ( const void *a, const void *b )
  2. {
  3. return -strcmp ( (const char *)a, (const char *)b );
  4. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

now how can subtracting two integers possibly cause integer overflow?

Narue: I agree your solution is simpler. But don't thumb you nose at nested conditional statements just because you may not understand them. Eeeeew :lol:

Dave: I'm sertain you already know that the two parameters must be cast to the correct type. So I don't know what you meant in your post. I could have posted this, but others, such as Narue, may not have understood it
C++ Syntax (Toggle Plain Text)
  1. return *(int *)p2 - *(int *)p1;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

Quote originally posted by Ancient Dragon ...
now how can subtracting two integers possibly cause integer overflow?
What's INT_MAX - INT_MIN?

Quote originally posted by Ancient Dragon ...
Dave: I'm sertain you already know that the two parameters must be cast to the correct type.
Right, you are incorrectly casting away the constness. Casting with (const int*) would be better.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

>now how can subtracting two integers possibly cause integer overflow?
Under is over and over is under. The point is that you could subtract beyond the limits of a signed integer, and Bad Things(TM) happen when you do stuff like that.

>But don't thumb you nose at nested conditional statements just
>because you may not understand them.
If everyone thumbed their noses at nested conditional statements, I wouldn't need to understand them. And the world would be a better place.

>I'm sertain you already know that the two parameters must be cast to the correct type.
And const is there for a reason. But for assignment there's no need to use casts:
C++ Syntax (Toggle Plain Text)
  1. int compare ( const void *a, const void *b )
  2. {
  3. const int *ia = a;
  4. const int *ib = b;
  5.  
  6. if ( *ia < *ib )
  7. return +1;
  8. else if ( *ia > *ib )
  9. return -1;
  10. else
  11. return 0;
  12. }
When you go directly into a function call, a cast is a good idea, but remember that strcmp expects pointers to const char, not pointers to char. You don't cast to the actual type, you cast to the expected type and hope with all your might that the two types are compatible.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

Quote originally posted by Dave Sinkula ...
What's INT_MAX - INT_MIN?
-1 with my compiler -- but you'r right about int overvlow in that one and only case. I don't ever recall any number of real world applications where that will happen, but then I havn't read all real-world programs either

Quote originally posted by Dave Sinkula ...
Right, you are incorrectly casting away the constness. Casting with (const int*) would be better.
Ok now I see your objection. Easy fix to the purests, but does not really do anything in this situation. But I suppose one should be consistent and use const correctly as you pointed out.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

Quote originally posted by Narue ...
but remember that strcmp expects pointers to const char, not pointers to char.
that's one of the purposes of function prototypes, to give the compiler enough information to make correct data type conversions. So you always do this? (I don't)
C++ Syntax (Toggle Plain Text)
  1. char name1[20];
  2. char name2[20];
  3.  
  4. ...
  5. if( strcmp( (const char*)name1, (const char*)name2))
  6. ...

I never explicitly typecast the parameters to const char*. So why bother in that comparison function I posted ?

And the same thing applies to all functions that take a const something-or-another.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Sep 19th, 2005
0

Re: how do i sort reords from Z-A? (in turbo C)

Quote originally posted by Ancient Dragon ...
-1 with my compiler
No, but that's what it does with the undefined behavior. (And can you guess how that might screw up a sort?)

Quote originally posted by Ancient Dragon ...
but you'r right about int overvlow in that one and only case.
Okay, how about subtracting from INT_MAX any negative number? For me that gives a couple billion cases. Then take INT_MAX-1 and a couple billion minus one more. Then take INT_MAX-2 ...

And you could also start on the other side: Subtract from INT_MIN and positive number. Subtract from INT_MIN+1 every number greater than 2.

I'd say there are quite a few combinations of two integers that result in undefined behavior.

[edit]By the way, this is a comparison function I'd run across.
C++ Syntax (Toggle Plain Text)
  1. int bar(const void *a, const void *b)
  2. {
  3. const int *x = a, *y = b;
  4. return (*x > *y) - (*x < *y);
  5. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Tic Tac Toe game, Can't resolve "Request for member" error
Next Thread in C++ Forum Timeline: What's the point of learning C++?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC