zoukankan      html  css  js  c++  java
  • 有一个班4个学生,5门课程 1求第1门课程的平均分; 2找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩; 3找出平均成绩在90分以上或全部课程成绩在85分以上的学生。4分别编3个函数实现以上3个要求。

    有一个班4个学生,5门课程。

    ①求第1门课程的平均分;

    ②找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;

    ③找出平均成绩在90分以上或全部课程成绩在85分以上的学生。

    分别编3个函数实现以上3个要求。

    解题思路: 4个学生,5门课程就是一个4x5的二维数组,

    1. 求第一门课的平均分,就是将第0列数据相加除以学生人数
    2. 遍历每个学生的成绩,判断不及格次数,若大于2则输出信息即可
    3. 统计每个学生总成绩,以及对每门课程成绩进行判断即可

    答案:

    #include<stdio.h>
    #include<math.h>
    
    float avg(int arry[][5], int n)
    {
    	float sum = 0;
    	for (int i = 0; i < n; i++) {
    		sum += arry[i][0];
    	}
    	printf("Average of course 1:%f
    ", (sum / n));
    	return (sum / n);
    }
    void fail(int arry[][5], int n)
    {
    	printf("Information on students who fail in more than two courses: ");
    	for (int i = 0; i < n; i++) {
    		int sum = 0, fail_count = 0;
    		for (int j = 0; j < 5; j++) {
    			if (arry[i][j] < 60) {
    				fail_count++;
    			}
    		}
    		if (fail_count <= 2) {
    			continue;
    		}
    		printf("seq:%d ", i + 1);
    		printf("score: ");
    		for (int j = 0; j < 5; j++) {
    			sum += arry[i][j];
    			printf("%d ", arry[i][j]);
    		}
    		printf("avg:%d ", sum / 5);
    		printf("
    ");
    	}
    	return;
    }
    void excellent(int arry[][5], int n)
    {
    	int i, j;
    	for (i = 0; i < n; i++) {
    		int sum = 0, count = 0;
    		for (j = 0; j < 5; j++) {
    			sum += arry[i][j]; //计算总分用于计算平均分
    			if (arry[i][j] > 85) {
    				count++; //若每门课都大于85则count总会与j同步++
    			}
    		}
    		if ((sum / 5) > 90 || count == j) {
    			printf("Excellent students: %d
    ", i + 1);
    		}
    	}
    	return;
    }
    
    int main()
    {
    	int arry[4][5];
    	printf("Please enter a 4x5 matrix:
    ");
    	for (int i = 0; i < 4; i++) {
    		for (int j = 0; j < 5; j++) {
    			scanf_s("%d", &arry[i][j]);
    		}
    	}
    	avg(arry, 4);
    	fail(arry, 4);
    	excellent(arry, 4);
    	printf("
    ");
    	system("pause");
    	return 0;
    }
    
    

     有一个班4个学生,5门课程 1求第1门课程的平均分;  2找出有两门以上课程不及格的学生,输出他们的学号和全部课程成绩及平均成绩;  3找出平均成绩在90分以上或全部课程成绩在85分以上的学生。4分别编3个函数实现以上3个要求。

  • 相关阅读:
    指针类型
    集合类型
    VMware打开虚拟机没反应的解决方案(全面汇总)
    redis主从|哨兵|集群模式
    MYSQL一次千万级连表查询优化
    StackExchange.Redis通用封装类分享
    Redis-五种数据类型解析
    Redis并发问题
    C#委托和事件
    Invoke 和 BeginInvoke 的区别
  • 原文地址:https://www.cnblogs.com/weiyidedaan/p/13292895.html
Copyright © 2011-2022 走看看