C语言程序:录入五门课程成绩并计算平均成绩及排序
#include <stdio.h>
int main() {
float scores[5];
float sum = 0;
float avg = 0;
int i, j;
// 录入成绩
printf('请输入五门课程的成绩:\n');
for (i = 0; i < 5; i++) {
scanf('%f', &scores[i]);
sum += scores[i];
}
// 计算平均成绩
avg = sum / 5;
printf('平均成绩为:%.2f\n', avg);
// 从小到大排序
for (i = 0; i < 4; i++) {
for (j = i+1; j < 5; j++) {
if (scores[i] > scores[j]) {
float temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
// 输出排序后的成绩
printf('成绩从小到大排序为:\n');
for (i = 0; i < 5; i++) {
printf('%.2f ', scores[i]);
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/mMJb 著作权归作者所有。请勿转载和采集!