WarMj:二维数组数据处理练习

2017-07-09  本文已影响0人  WarMj

编写一段程序,使用二维数组操作5名学生3个科目(语文,数学,英语)的分数,完成如下处理。
例1:计算每科的最高分。
例2:计算每名学生3个科目的平均分。
——《明解C语言》练习6-11

成绩表样例(程序支持手动录入)

语文 数学 英语
学生1 99 98 97
学生2 99 98 97
学生3 99 98 97
学生4 99 98 97
学生5 99 98 97

代码分析

#include<stdio.h>
#include<stdlib.h>

//函数声明。 
void Scan_Score(const int score[5][3]);
void Max_Score(const int score[5][3]);
void Ave_Score(const int score[5][3]);

int main()
{
    int score[5][3] = {0};
//  int score[5][3] = {{99, 98, 97}, {99, 98, 97}, {99, 98, 97}};
    
    Scan_Score(score);
    Max_Score(score);
    Ave_Score(score);
    
    system("pause");
    return 0;
 } 

//成绩录入函数 
void Scan_Score(const int score[5][3])
{
    for(int i = 0; i < 5; i++)
    {
        printf("Please enter the score of the NO.%d student:\n", i+1);
        for(int j = 0; j < 3; j++)
        {
            switch(j)
            {
                case 0: puts("Chinese score:"); break;
                case 1: puts("Math score:");       break;
                case 2: puts("English score:"); break;
            }
            scanf("%d", &score[i][j]);
        }
    }
}

//各科成绩最高分函数 
void Max_Score(const int score[5][3])
{
    int Max_Ch = score[0][0], Max_Math = score[0][1], Max_Eng = score[0][2];
    for(int j = 0; j < 3; j++)
    {
        for(int i = 0; i < 5; i++)
        {
            switch(j)
            {
                case 0: if(Max_Ch < score[i][j])
                    {
                        Max_Ch = score[i][j];
                    }
                break;
            
                case 1: if(Max_Math < score[i][j])
                    {
                        Max_Math = score[i][j];
                    }
                break;
                
                case 2: if(Max_Eng < score[i][j])
                    {
                        Max_Eng = score[i][j];
                    }
                break;
            }
        }
    }
    printf("The max score of Chinese is:%d\n", Max_Ch);
    printf("The max score of Math is:%d\n", Max_Math);
    printf("The max score of English is:%d\n", Max_Eng);
 } 
 
//每名学生平均成绩函数 
void Ave_Score(const int score[5][3])
 {
    int sum[5] = {0};
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            sum[i] += score[i][j];
         }
         printf("The average score for Student.%d is :%d\n", i+1, sum[i]/3);
     }
 }
上一篇下一篇

猜你喜欢

热点阅读