C语言程序()从键盘输入N个学生和三门课的成绩,输出每个学生平均成绩、总成绩和最高总成绩

C语言程序()从键盘输入N个学生和三门课的成绩,输出每个学生平均成绩、总成绩和最高总成绩

//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "stdlib.h"
int main(void){
    int N,i,fm,tmp,(*p)[3];
    while(1){
        printf("Input N(int 0<N)...\nN=");
        if(scanf("%d",&N),N>0)
            break;
        printf("Error, redo: ");
    }
    if((p=(int (*)[3])malloc(sizeof(int)*3*N))==NULL){
        printf("Failed to create the array...\n");
        exit(0);
    }
    printf("Please enter the 3 grades of every student...\n");
    for(i=0;i<N;i++){
        printf("STU%d:\t",i+1);
        scanf("%d%d%d",p[i],p[i]+1,p[i]+2);
    }
    printf("\nStudent\tAverage\tTotal\n---------------------\n");
    for(fm=p[0][0]+p[0][1]+p[0][2],i=0;i<N;i++){
        printf("STU%d\t%.2f\t%d\n",i+1,tmp/3.0,tmp=p[i][0]+p[i][1]+p[i][2]);
        if(fm<tmp) 
            fm=tmp;
    }
    free(p);
    printf("The highest total score is %d\n",fm);
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-07

    N已知:定义 a[N][5] ,在每一行后2格直接计算平均、总成绩,最后遍历一次取最高或再定义一个变量存储当前最高总成绩。

    N未知(不要求保存原始数据):定义 a[5] 、 当前最高总成绩 i ,原理同上,只是每次读取数据覆盖掉上一次。

    N未知(要求保存原始数据):参考1,N定义大一点吧。

相似回答