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?
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.
int mycomp(const void* p1, const void* p2)
{
int* i1 = (int*)p1;
int* i2 = (int *)p2;
return *i2 - *i1;
}
Or an array of strings, in descending order (I think)
int mycomp(const void* p1, const void* p2)
{
int x = strcmp((char*)p1,(char*)p2);
return (x < 0) ? 1 : (x > 0) ? -1 : 0;
}
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.
>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:
int compare ( const void *a, const void *b )
{
return -strcmp ( (const char *)a, (const char *)b );
}
;)
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
return *(int *)p2 - *(int *)p1;
now how can subtracting two integers possibly cause integer overflow?
What's INT_MAX - INT_MIN?
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.
>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:
int compare ( const void *a, const void *b )
{
const int *ia = a;
const int *ib = b;
if ( *ia < *ib )
return +1;
else if ( *ia > *ib )
return -1;
else
return 0;
}
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. :)
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 :D
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.
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)
char name1[20];
char name2[20];
...
if( strcmp( (const char*)name1, (const char*)name2))
...
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.
-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?)
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.
int bar(const void *a, const void *b)
{
const int *x = a, *y = b;
return (*x > *y) - (*x < *y);
}
Oh! your right Dave -- because subtracting a minus is really addition. I knew that, just checking if you did too :cheesy: :o
int bar(const void *a, const void *b) { const int *x = a, *y = b; return (*x > *y) - (*x < *y); }
That will not compile with either VC++ 6.0 or Dev-C++. Requires cast like this
int bar(const void *a, const void *b)
{
const int *x = (int*)a, *y = (int*)b;
return (*x > *y) - (*x < *y);
}
but otherwise an interesting algorithm.
Avoid compiling C code in a C++ compiler. (Or else save the file with a .c extension instead of .cpp!)
[edit]
but otherwise an interesting algorithm.
Yeah, I've been trying to Google the thing about 'no branches' or 'no instruction flush' or whatever it was -- something potentially beneficial to remember so that I could Google it again one day.
[edit=2]Here's another integer compare function that Narue might enjoy.
int baz(const void *a, const void *b)
{
const int *x = a, *y = b;
return (*x < *y) ? -1 : (*x > *y);
}
[edit=3]And note that in all my posts to this thread, the comparison function when used with qsort would sort in increasing order -- rather than the OP's requested decreasing order.
>So why bother in that comparison function I posted ?
Because pointers to void can be funny beasties. I like to make sure that everything matches up before the function call on the off chance that I miss a subtle issue. That's actually a throwback from when I didn't fully understand pointers and wrote code to protect myself from my own ignorance, but at the very least it proves that I know what types strcmp expects. :)
>That will not compile with either VC++ 6.0 or Dev-C++. Requires cast like this
Try Turbo C, at least that way you won't accidentally compile as C++. ;)
how can i sort in turbo c++?
or how can i sort 3 input number from highest to lowest??
like this!!
Enter Number: 2
Enter Number: 1
Enter Number: 4
Value of A:4
Value of B:2
Value of C:1
is that right?
this is my code
#include<stdio.h>
#include<conio.h>
main
{
int A,B,C;
clrscr();
printf("Enter Number:");
........
here
A=B;
B=C;
C=A;
is that right?
and
printf("Value of A:%d",A);
Why are you guys using void*, ewww on that. This is C++, use its power.
>> is that right?
No. You have to use some if statements to check of the value of A > B, or B > C or C > A.
any one can u plz tell me the code for sorting 10 numbers using C++ codes
To match your very little effort at asking the question..
my answer is: std::sort (<algorithm> header).
any one can u plz tell me the code for sorting 10 numbers using C++ codes
put the numbers in an array then google for bubblesort.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.