请叫下,下面这个程序,怎样用冒泡排序法排列,就差最后一步了
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <time.h> #define M 5 #define N 5 void input(int scores[][3]); void cal(int scores[][3], double avgR[], double avgC[]); void sort(double avgR[], int idx[]); void printstring(char *s, int n); int main(void) { char *ke[N] = {"yuwen","shuxue","yingyu","pingjun","zongfen"}; char *names[M] = {"qin", "wu", "yu", "sa", "er"}; int scores[M][3] = {0}, idx[M] = {0}, i, j; double avgR[M] = {0}, avgC[3] = {0}; input(scores); cal(scores, avgR, avgC); sort(avgR, idx); for(j=0;j<N;j++) { if(j==0) printstring(" ",8); printstring(ke[j],8); } printf("\n"); for (i = 0; i < M; i++) { printstring(names[i],8); for (j = 0; j < 3; j++) { printf("%3d ", scores[i][j]); } printf("%3.1lf ", avgR[i]); printf("%3.0f \n", avgR[i]*3); } getchar(); return 0; } void input(int score[][3]) { int i, j; srand((unsigned) time(NULL)); for (i = 0; i < M; i++) for (j = 0; j < 3; j++) score[i][j] = 40 + rand() % 61; return; } void cal(int scores[][3], double avgR[], double avgC[]) { int i, j; for (i = 0; i < M; i++) { for (j = 0; j < 3; j++) { avgR[i] += scores[i][j]; avgC[j] += scores[i][j]; } avgR[i] /= 3; } for (j = 0; j < 3; j++) avgC[j] /= M; return; } void sort(double avgR[], int idx[]) { int i, j,temp; for (i = 0; i < M; i++) idx[i] = i; for (i = 0; i < M - 1; i++) { for (j = i + 1; j < M; j++) if (avgR[i] < avgR[j]) { temp=idx[i], idx[i] = idx[j], idx[j] = temp; } } return; } void printstring(char *s, int n) { int i=strlen(s),j; if(i<=n) { printf("%s",s); for(j=0;j<(n-i);j++) printf(" "); } else printf("%8s",s); }现在输出的成绩每次都不一样,但要按总分排序,怎样用冒泡排序法排序啊,请教下各位