C语言实现五门课程成绩排序及平均分计算
#include <stdio.h>
int main() { // 定义五门课程的成绩数组 float scores[5]; float sum = 0;
// 循环读取五门课程的成绩 for(int i = 0; i < 5; i++) { printf('请输入第%d门课程的成绩:', i+1); scanf('%f', &scores[i]);
// 求和
sum += scores[i];
}
// 求平均成绩 float average = sum / 5;
// 冒泡排序 for(int i = 0; i < 4; i++) { for(int j = 0; j < 4-i; j++) { if(scores[j] > scores[j+1]) { // 交换两个数 float temp = scores[j]; scores[j] = scores[j+1]; scores[j+1] = temp; } } }
// 输出排序后的成绩 printf('五门课程的成绩从小到大排序如下:\n'); for(int i = 0; i < 5; i++) { printf('%.2f ', scores[i]); }
// 输出平均成绩 printf('\n五门课程的平均成绩为:%.2f', average);
return 0; }
原文地址: https://www.cveoy.top/t/topic/mMIY 著作权归作者所有。请勿转载和采集!