500字范文,内容丰富有趣,生活中的好帮手!
500字范文 > C语言 利用一维数组和选择法对成绩高低排序 及输出对应的学号

C语言 利用一维数组和选择法对成绩高低排序 及输出对应的学号

时间:2022-12-10 01:48:04

相关推荐

C语言 利用一维数组和选择法对成绩高低排序 及输出对应的学号

声明:该编译器为vs,所以输入函数写为scanf_s形式!

代码如下:

#include <stdio.h>#define N 40int 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]);}}

运行结果如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。