dsda

Pages: 12
sdasdsada
Last edited on
In this hypothetical order, does "potato" compare greater than positive infinity? What about strings such as "1&24", "124q", "000-124"? Where do they fit in this order?
Line 6: tmp is an uninitialized pointer. At lines 27 and 29 you're going to reference that uninitialized pointer which will cause a crash.

Line 9: main must be type int.

Line 38: main() should return 0.

As helios pointed out, you're comparing apples and oranges. stricmp is a case insensitive alphabetic compare. Line 25 is going to compare entries in alphabetic order, not numeric order. If you want a numeric comparison, use ints.
Last edited on
tmp is a null pointer, so when you copy to it at line 27, the behavior is undefined. Change line 6 to char a[50][50], tmp[50];

As Helios stated, right now you're sorting strings, which is different from sorting numbers. Your question implies that numbers should be sorted numerically. How should strings be be sorted? It sounds like strings should come after numbers?

If there are multiple strings, should they be sorted relative to each other?
Should "-123abc" be interpretted as a string or a number?
What about --234?

If this is a homework assignment, can you post the text of the assignment?
I've did what AbstractionAnon and dhayden told me to it still outputs the same result. I haven't really thought about trying "-123abc" and such, also i can't use ints, it has to be char. That is what's confusing me, but the main problem is just this: it has to display -6 first before -1. As of now, it displays -1,-6,potato which is clearly wrong. Is there like a function or something to modify this?
The definition of strcmp() is equivalent to
1
2
3
4
5
6
7
8
9
int strcmp(const char *a, const char *b){
    for (; *a || *b; a++, b++){
        if (*a < *b)
            return -1;
        if (*a > *b)
            return 1;
    }
    return 0;
}
By this definition, "-6" does indeed compare greater than "-1", because '-' == '-' and '6' > '1'. This order isn't wrong, you're just trying to use a lexicographical comparison function to compare numbers.
There's no built-in function that implements the order you're proposing, since at it is, it's ill-defined. You're on your own.
so basically I'm screwed then?
Not at all. You just have to implement whatever ordering function you want yourself. If you can describe what you want we might be able to help you design it.
Thanks. So one solution I've thought about is removing the negative sign in both those numbers, I think the output would then be: -6,-1, potato thus correct. The problem is how do i do this?
Err, no. Given two strings of the form S1 = x + y and S2 = x + z, where + is the concatenation operator, if S1 < S2 then y < z. In other words, since "-1" < "-6", "1" < "6", so the order would remain the same.
I see. I apologize. Sorry if I'm pretty dull but I'm trying my best to understand all the things you've said. I have no idea now on how to solve this.
You don't need to apologize.

Personally, I don't even see what you're trying to solve. What's the problem? You have a list of strings and you're sorting them with strcmp() and getting back a lexicographically sorted list.
Your problem is that a string is a fully general type. Anything that a computer can represent can be represented in a string. So if you're going to say that some strings are numbers and that they should all be comparable with each other you're going to need to have
1. A full definition of what shape a string may have to be considered a number.
2. A comparison between two numerical strings.
3. A comparison between two non-numerical strings.
4. A comparison between a numerical and a non-numerical string.

Which of these strings are numerical?
* "1"
* "-4"
* " -4" (note the space)
* "--7"
* "1+2"
* "1++2"
* "8.16196e+99"
* "0.999..."
* "pi"
* "1 + pi"
* "lim x -> 0 sin(x)/x"
* "potato + 1 where potato = 1"
* "inf"

How do you handle the cases where a number may be represented in multiple ways?
* cmp("1", "01") = ?
* cmp("0", "-0") = ?
* cmp("0", "+0") = ?
* cmp("1", "1.0") = ?
* cmp("1", "--1") = ?
* cmp("1", "+1") = ?
* cmp("0.999...", "1") = ?
Last edited on
wouldn't that be extremely difficult?
Not necessarily. It depends on how you answer those questions. There's quite a few sets of answers that define an order that's not stupidly complex to implement.
Last edited on
does using isdigit correct for number one?
Could you rephrase that in a way that's coherent?
1. A full definition of what shape a string may have to be considered a number.
1
2
3
4
5
6
for(p;p>0;p--)
{
 for(c=0;c<p;c++)
  {
     if(stricmp(a[c],a[c+1])>0)
     {

is using isdigit correct for this?
1
2
3
4
5
for(c=0;c<p;c++)
  {
     if(stricmp(a[c],a[c+1])>0)
     {
        if(isdigit(a[c],a[c+1])
isdigit() accepts a single character as its input, so I don't know what the code you've posted is supposed to mean.
I see so it's incorrect.
a
Last edited on
Pages: 12