【西南交大网站训练题】班级共有 m个人,该班C语言的成绩存放在score(score为整数)数组中,计算该班成绩的平均分,并将小于平均分的成绩存储在一个数组中,并打印该数组的值。

作为一名西南交大计算机的大一新生,每次网站查看答案都要花10个金币,于是我决定将网站的答案写下来(不是标答,本人自己写的)造福大家。有问题欢迎指正TT

班级共有 m个人,该班C语言的成绩存放在score(score为整数)数组中,计算该班成绩的平均分,并将小于平均分的成绩存储在一个数组中,并打印该数组的值。
要求:
1.	请编写函数fun, 它的功能是:计算平均分,并将低于平均分的成绩和相应的数组下标分别存在不同的数组中(打印语句放在主函数中),声明如下:
int fun(int score[], int m, int below_score[], int below_index[]);
2.	请编写函数ReadScore,读入成绩,返回输入的有效人数,声明如下:
int ReadScore(int score[]);
3.	需要对数组越界做判断,如:在输入时,直接输入-1的情况,此时显示“there are no valid scores”,并终止程序。
4.	班机最多有40人,用宏定义数组的所含最多的元素数量。

输入:每一行输入一个人的成绩,直到输入值为负数时,结束成绩的输入,并将此时拥有的成绩数量,作为班机人数,如:
45
67 
98
-1 
输出:打印班机的总人数,低于平均分的,低于平均分的成绩及该成绩在输入时的序号,从1开始计数,如:
the number of the class:3
the number under the average score: 2
the 1th score is: 45
the 2th score is: 67

代码如下

#include<stdio.h>
#define N 40 //用宏定义数组的所含最多的元素数量 
int ReadScore(int score[N]);
int fun(int score[], int m, int below_score[], int below_index[]);//计算平均分,并且低于平均分的成绩和相应的数组下标分别存在不同的数组中 
int main(){
	int m;//表示总人数 
	int j;//表示低于平均分的人数 
    int score[N]={0};
    int below_score[N]={0};//用于储存低于平局分的成绩 
    int below_index[N]={0};//储存低于平均分成绩的序号
    printf("Input score:");
	m=ReadScore(score); 
	if(m==0){
		printf("there are no valid scores\n");
	}
	j=fun(score,m,below_score,below_index);
	printf("the number of the class:%d\n",m);
	printf("the number under the average score: %d\n",j);
	int i;
	for(i=0;i<j;i++){
		printf("the %dth score is: %d\n",below_index[i],below_score[i]);
	}
	return 0;
	
}
int ReadScore(int score[N]){
	int i;
	for(i=0;i<N;i++){
		scanf("%d",&score[i]);
		if(score[i]==-1){
			return i;
		}else{
			continue;
		}
	}
}
int fun(int score[], int m, int below_score[], int below_index[]){
	int aver=0;//表示平均值 
	int i,j;
	for (i=0;i<m;i++){
		aver+=score[i];
	}
	aver=aver/m;
	for(i=0,j=0;i<m;i++){
		if(score[i]<aver){
			below_score[j]=score[i];
			below_index[j]=i+1;
			j++;
		}else{
			continue;
		}
	}
	return j;
}
	

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值