北極熊愛吃鱈魚 2023-05-30 14:42 采纳率: 0%
浏览 19

编写C程序1,参考下图程序内容,用函数实现以下功能

通过编程输入并存储一个班10名同学某门课程的成绩,然后实现下列要求:
(1)函数1:输入每名同学的成绩,计算全班10名同学的总分和平均分,并输出总分、平均分;
(2)函数2:输出每名同学的成绩;
(3)函数3:查找出最高分和最低分,并输出最高分、最低分;
(4)函数4:将10名同学的成绩从低到高排序,并输出成绩排序情况

  • 写回答

2条回答 默认 最新

  • Watch the clown 2023-05-30 14:54
    关注

    在你之前的楼上就有一模一样的,现成代码:

    img

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    void inputScores(int scores[], int n) {
        cout << "请输入" << n << "名同学的成绩:" << endl;
        for (int i = 0; i < n; i++) {
            cin >> scores[i];
        }
    }
    
    void outputScores(int scores[], int n) {
        cout << "10名同学的成绩为:" << endl;
        for (int i = 0; i < n; i++) {
            cout << scores[i] << " ";
        }
        cout << endl;
    }
    
    void calculateTotalAndAverage(int scores[], int n) {
        int total = 0;
        for (int i = 0; i < n; i++) {
            total += scores[i];
        }
        double average = (double) total / n;
        cout << "全班10名同学的总分为:" << total << endl;
        cout << "全班10名同学的平均分为:" << average << endl;
    }
    
    void findMaxAndMin(int scores[], int n) {
        int maxScore = *max_element(scores, scores + n);
        int minScore = *min_element(scores, scores + n);
        cout << "全班10名同学的最高分为:" << maxScore << endl;
        cout << "全班10名同学的最低分为:" << minScore << endl;
    }
    
    void sortScores(int scores[], int n) {
        sort(scores, scores + n);
        cout << "全班10名同学的成绩从低到高排序为:" << endl;
        for (int i = 0; i < n; i++) {
            cout << scores[i] << " ";
        }
        cout << endl;
    }
    
    int main() {
        int scores[10];
        inputScores(scores, 10);
        outputScores(scores, 10);
        calculateTotalAndAverage(scores, 10);
        findMaxAndMin(scores, 10);
        sortScores(scores, 10);
        return 0;
    }
    
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 5月30日