Home>

Please tell me what is wrong with the following description in C language. Please.

It is written to solve the following problems.
This is a program that sorts the numbers separated by, in descending order.

When I put a sharp, the characters became bigger, so I started with include, but the actual program has a sharp.


Please sort the contents of the array below in descending order and attach them.
Example: If i have an array {1,5,3,2}, sort it in descending order to get {5,3,2,1}. So the answer is {5321}.

[15,1,93,52,66,31,87,0,42,77,46,24,99,10,19,36,27,4,58,76,2,81,50,102,33,94 , 20,14,80,82,49,41,12,143,121,7,111,100,60,55,108,34,150,103,109,130,25,54,57,159,136,110,3,167,119,72,18,151,105,171,160,144,85,201,193,188,190,146,210,211,63,207]


include

int main () {
int cap = 200;
unsigned char a [cap], b [cap];
int c [cap], tmp;
int s, t, u, i, j, x;

printf ("Enter value \ n");
scanf ("% s",&a);

t = 0;
u = 1;

Remove //, and store only the number in b. Also, record how many in total with else.
for (s = 0;s if (a [s]! =',') {
b [t] = a [s];
++ t;
} else {
u = u + 1;
}
// Convert b to int type. Also, at that time, put it in c.
for (x = 0;x c [x] = (int) b [x];
}
// Sort in descending order.
for (i = 0;i for (j = i + 1;j if (c [i] tmp = c [i];
c [i] = c [j];
c [j] = tmp;
}
}
}

printf ("descending sorted numbers \ n");
for (i = 0;i printf ("% d \ n", c [i]);
}
return 0;
}

c
  • Answer # 1

    The first big misconception is the conversion of strings and numbers.

      // Convert b to int type. Also, at that time, put it in c.
        for (x = 0;x<cap;++ x) {
            c [x] = (int) b [x];
        }

    Please understand that this is strange.

    For example1ThatletterWhen there wasc [x] = (int) b [x];Because thatCharacter codeToc [x]It will be put in. If you think in ASCII0x31is not it. Is this the conversion you really want? It's different?

    There is one more big mistake.

      Remove //, and store only the number in b. Also, record how many in total with else.
        for (s = 0;s<cap;++ s) {
            if (a [s]! =',') {
                b [t] = a [s];
                ++ t;
            }
            else {
                u = u + 1;
            }
        }

    Delimiter,The point I'm trying to identify is correct, but it doesn't record what character the delimiter was in. For example123,41In response to the inputone two ThreeWhen41You have to divide it into numbers and convert it to a numerical value, but this is a problem.

    By the way, to convert a string to a numberstrtolI used a function. In factstrtolWith the function, you don't have to record the position of the delimiter every time.

    #include<stdio.h>
    #include<stdlib.h>
    #include<errno.h>
    #ifndef COUNFOF
    #define COUNFOF (arr) (sizeof (arr)/sizeof (* arr))
    #endif
    int main (void)
    {
        char buf [512];
        if (NULL == fgets (buf, COUNFOF (buf), stdin)) {
            return 1;
        }
        long nums [256];
        const char * s = buf;
        char * endptr;
        size_t i;
        for (i = 0;i<COUNFOF (nums);++ i) {
            errno = 0;
            const long t = strtol (s,&endptr, 10);
            if (0! = errno || (0 == t&&endptr == s)) return 1;
            nums [i] = t;
            if (endptr [0] =='\ 0'|| endptr [0] =='\ n'|| endptr [0] =='\ r') {
                ++ i;
                break;
            }
            if (endptr [0]! =',') return 2;
            s = endptr + 1;
        }
        for (size_t j = 0;j! = i;++ j) {
            printf ("% ld,", nums [j]);
        }
        return 0;
    }

    It should be roughly as above (sorting omitted)