Answer to Question #74859 in C for jw
2018-03-20T11:01:14-04:00
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]--;
}
}
1
2018-03-26T10:18:22-0400
The answer to the question is available in the PDF file https://www.assignmentexpert.com/https://www.assignmentexpert.com/homework-answers/programming-answer-74859.pdf
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS !
Learn more about our help with Assignments:
C
Comments
Xcode
What compiler do you use?
the 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; }
Dear jw. Please check updated solution.
After 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