#include <stdio.h>
#define N 40
int ReadScore(int score[],long num[]);//函数原型;
void DataSort(int score[], int n, long num[]);//函数原型;
void PrintfScore(int score[], int n, long num[]);//函数原型;
int main() {int score[N], n;long num[N];n = ReadScore(score,num);printf("Total students are %d\n", n);DataSort(score, n,num);printf("Sorted scores:\n");PrintfScore(score, n,num);return 0;
}
//函数功能:读入该门课的成绩:
int ReadScore(int score[], long num[]) {int i = -1;printf("Input student't ID and score:\n");do {i++;scanf_s("%ld %d",&num[i], &score[i]);} while (num[i]>0&&score[i] >= 0);return i;
}
//函数功能:将该门课的成绩按照高低排序;
void DataSort(int score[], int n, long num[]) {int i, j, temp1, k;long temp2;for (i = 0; i < n - 1; i++) {k = i;for (j = i + 1; j < n; j++) {if (score[j] > score[k]) {k = j;//记录最大的下标位置}}if (k != i) {//若最大数的下标位置不在下标位置i;temp1 = score[k];score[k] = score[i];score[i] = temp1;temp2 = num[k]; num[k] = num[i]; num[i] = temp2;}}
}
//函数功能:打印该门课的成绩;
void PrintfScore(int score[], int n, long num[]) {int i;for (i = 0; i < n; i++) {printf("%d%4d\n",num[i],score[i]);}
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态