# Answer to Question #74859 in C for jw

Question #74859

how to change the code so that it can sort

struct SResult sample[] = { {"A1234", 10}, {"A1239", 5}, {"A1394", 7}, {"A1434", 3}, {"A1454", 5}, {"A2884", 7}, {"A3235", 7}, {"A4334", 9}, {"A4884", 2}, {"A6934", 5}, {"A7265", 7}, {"A9559", 3} };

void counting_sort(struct SResult scoreArr[], int N, int final[]) {

int freq[11] = {0}, cfreq[11] = {0};

int i, curScore;

//1. Compute Frquency

for (i = 0; i < N; i++){

freq[ scoreArr[i].score ] ++;

}

//2. Compute Cumulative Frequency

cfreq[0] = freq[0];

for (i = 1; i < 11; i++){

cfreq[i] = cfreq[i-1] + freq[i];

}

//3. Produce Final Position

for (i = 0; i < N; i++){

curScore = scoreArr[i].score;

final[ cfreq[ curScore ] - 1 ] = curScore;

cfreq[curScore]--;

}

}

struct SResult sample[] = { {"A1234", 10}, {"A1239", 5}, {"A1394", 7}, {"A1434", 3}, {"A1454", 5}, {"A2884", 7}, {"A3235", 7}, {"A4334", 9}, {"A4884", 2}, {"A6934", 5}, {"A7265", 7}, {"A9559", 3} };

void counting_sort(struct SResult scoreArr[], int N, int final[]) {

int freq[11] = {0}, cfreq[11] = {0};

int i, curScore;

//1. Compute Frquency

for (i = 0; i < N; i++){

freq[ scoreArr[i].score ] ++;

}

//2. Compute Cumulative Frequency

cfreq[0] = freq[0];

for (i = 1; i < 11; i++){

cfreq[i] = cfreq[i-1] + freq[i];

}

//3. Produce Final Position

for (i = 0; i < N; i++){

curScore = scoreArr[i].score;

final[ cfreq[ curScore ] - 1 ] = curScore;

cfreq[curScore]--;

}

}

Expert's answer

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

LEE JIA WEI31.03.18, 04:38Xcode

Assignment Expert29.03.18, 19:26What compiler do you use?

jw29.03.18, 17:02the output still error

int main()

{

struct SResult sample[] = {

{"A1234", 10},

{"A1239", 5},

{"A1394", 7},

{"A1434", 3},

{"A1454", 5},

{"A2884", 7},

{"A4334", 9},

};

struct SResult sorted[12] = {{0}};

int i;

counting_sort(sample, 12, sorted);

for (i = 0; i < 12; i++){

printf("[%s, %d]\n", sorted[i].studentID, sorted[i].score);

}

printf("\n");

return 0;

}

Assignment Expert26.03.18, 17:19Dear jw. Please check updated solution.

jw25.03.18, 07:20After run with using my main function. The output is incorrect

i get [, 3]

[ , 1]

[

, 5]

[, 0]

[, 0]

[, 0]

[, 0]

[, 0]

[, 0]

[, 0]

[, 0]

## Leave a comment