Home>

### please tell me what is wrong with the following description in c language

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;
}

• 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 example`1`ThatletterWhen there was`c [x] = (int) b [x];`Because thatCharacter codeTo`c [x]`It will be put in. If you think in ASCII`0x31`is 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 example`123,41`In response to the input`one two Three`When`41`You 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 number`strtol`I used a function. In fact`strtol`With 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 ;
if (NULL == fgets (buf, COUNFOF (buf), stdin)) {
return 1;
}
long nums ;
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'|| endptr  =='\ n'|| endptr  =='\ r') {
++ i;
break;
}
if (endptr ! =',') 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)