zoukankan      html  css  js  c++  java
  • 谭浩强C语言程序设计 学习辅导练习题

    谭浩强C语言程序设计 学习辅导练习

    第七章 函数

    T14

    输入N个学生M门课成绩,分别用函数实现下列功能:
    1、计算每个学生的平均分
    2、计算每门课的平均分
    3、找出分数中最高分数所对应的学生和课程
    4、计算平均分方差

    //输入N个学生M门课成绩,分别用函数实现下列功能:
    //    1、计算每个学生的平均分
    //    2、计算每门课的平均分
    //    3、找出分数中最高分数所对应的学生和课程
    //    4、计算平均分方差
    #include <stdio.h>
    #include <stdlib.h>
    #define N 2
    #define M 3
    float score[N][M];
    float a_stu[N],a_cour[M];
    int r,c;
    int main(){
        int i,j;//
        float h;//存放最高分
        float s_var(void);//计算方差
        float highest();//找出最高分
        void input_stu(void);//输入学生成绩
        void aver_stu(void);//计算每个学生的平均分
        void aver_cour(void);//计算每个科目的平均分
        input_stu();
        aver_stu();
        aver_cour();
        printf("
    NO.    cour1    cour2    cour3    cour4    cour5    aver
    ");
        for(i=0;i<N;i++){
            printf("
     NO %2d ",i+1);
            for(j=0;j<M;j++)
                printf("%8.2f",score[i][j]);
                printf("%8.2f",a_stu[i]);
        }
        printf("
    average:");
        for(j=0;j<M;j++)
            printf("%8.2f",a_cour[j]);
        printf("
    ");
        h=highest();//找出最高分
        printf("highest:%7.2f    NO. %2d    course %2d
    ",h,r,c);
        printf("variance %8.2f
    ",s_var());//输出方差
        system("pause");
        return 0;
        
    }
    void input_stu(void){
        int i,j;
        for(i=0;i<N;i++){
            printf("
    input score of student%2d:
    ",i+1);
            for(j=0;j<M;j++)
                scanf("%f",&score[i][j]);
        }
    }
    void aver_stu(void){
        int i,j;
        float s;
        for(i=0;i<N;i++){
            s=0;
            for(j=0;j<M;j++)
                s +=score[i][j];
            a_stu[i]=s/(float)M;
        }
    }
    void aver_cour(void){
        int i,j;
        float s;
        for(j=0;j<M;j++){
            s=0;
            for(i=0;i<N;i++)
                s +=score[i][j];
            a_cour[j]=s/(float)N;
        }
    }
    
        float highest(){
            float high;
            int i,j;
            high=score[0][0];
            for(i=0;i<N;i++)
                for(j=0;j<M;j++)
                    if(score[i][j]>high){
                        high=score[i][j];
                        r=i+1;//数组行号I从0开始,学生号r从1开始,故r=i+1;
                        c=j+1;//
                    }
            return(high);
        }
        
        float s_var(void){
            int i;
            float sumx=0.0;
            float sumxn=0.0;
            for(i=0;i<N;i++){
                sumx += a_stu[i]*a_stu[i];
                sumxn +=a_stu[i];
    
            }
            return (sumx/N -(sumxn/N) * (sumxn/N));
        }
        
    View Code
  • 相关阅读:
    hdu4829 带权并查集(题目不错)
    hdu4829 带权并查集(题目不错)
    洛谷 P1076 寻宝(模拟 && 剪枝)
    洛谷 P1981 表达式求值(模拟)
    洛谷 P2239 螺旋矩阵(模拟 && 数学)
    洛谷 P2118 比例简化(枚举)
    洛谷 P3956 棋盘(记忆化搜索)
    洛谷 P5018 对称二叉树(搜索)
    洛谷 P5016 龙虎斗(模拟)
    洛谷 P1563 玩具谜题(模拟)
  • 原文地址:https://www.cnblogs.com/crystalmoore/p/5954760.html
Copyright © 2011-2022 走看看