您的位置:首页 > 其它

记录一个班级的成绩练习一维数组

2015-04-08 17:05 260 查看
直接上代码,mark一下:嘿嘿

#include <stdio.h>

/**
 * 记录并统计一个班的学生的成绩来
 * 练习一维数组的使用
 */
int main(void)
{
    int count;

    printf("How many students are in your class?\n");
    scanf("%d",&count);  //获取班级中学生的数量

    /**
     * 我在书中看的说是不允许变量赋值来这,但是
     * 在我的尝试中是可以的,不知道怎么回事。
     */
    int numbers[count],chinese[count],maths[count],eng[count];

    puts("Please input the StudentID and three scores:\n");
    printf("      studentID Chinese Math English\n");

    int i = 0;
    /**
     * 接受用户的成绩输入
     */
    for(i = 0;i < count;i++){
        printf("No.%d>",i+1);
        scanf("%d %d %d %d",&numbers[i],&chinese[i],&maths[i],&eng[i]);
    }

    /**
     * 输入完毕之后,输出用户的成绩及其平均成绩
     */
    printf("ID  CH  MA  EN  ***E\n");
    printf("--------------------------\n");

    int j;
    for(j = 0;j < count;j++){
        float ave = (chinese[j] + maths[j] + eng[j])/3;  //求出平均成绩
        printf("%d\t%d\t%d\t%d\t%f\n",numbers[j],chinese[j],maths[j],eng[j],ave);
    }

    /**
     * 循环求出各科的总成绩
     */
    float ave_chinese = 0,ave_math= 0 ,ave_english = 0;
    int m;
    for(m = 0;m < count;m++){
        ave_chinese += chinese[m];
        ave_math += maths[m];
        ave_english += eng[m];
    }

    //输出平均成绩
    printf("The Average of Chinese in this class is %f\n",ave_chinese/count);
    printf("The Average of math in this class is %f\n",ave_math/count);
    printf("The Average of english in this class is %f\n",ave_english/count);

    return 0;
}


下面是我的程序的运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐