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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 2
Reputation: renar is an unknown quantity at this point 
Solved Threads: 0
renar renar is offline Offline
Newbie Poster

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

 
0
  #1
Sep 19th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #2
Sep 19th, 2005
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.
  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)
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #3
Sep 19th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Sep 19th, 2005
>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:
  1. int compare ( const void *a, const void *b )
  2. {
  3. return -strcmp ( (const char *)a, (const char *)b );
  4. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #5
Sep 19th, 2005
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
  1. return *(int *)p2 - *(int *)p1;
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #6
Sep 19th, 2005
Originally Posted by Ancient Dragon
now how can subtracting two integers possibly cause integer overflow?
What's INT_MAX - INT_MIN?

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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #7
Sep 19th, 2005
>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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #8
Sep 19th, 2005
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

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #9
Sep 19th, 2005
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)
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #10
Sep 19th, 2005
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?)

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.
  1. int bar(const void *a, const void *b)
  2. {
  3. const int *x = a, *y = b;
  4. return (*x > *y) - (*x < *y);
  5. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC