2. 编写程序实现输入一个班级学生数学成绩,并在输入结束后显示出来的功能。要求班级学生人数由用户输入 5
展开全部
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,t,i; //学生人数,临时存储,i循环控制
float *p,average,highest,lowest; //学生成绩,平均成绩,最高成绩,最低成绩
average = 0;
printf("输入学生个数: ");
scanf("%d",&n);
if((p = ((float *)malloc(sizeof(float)*n))) == NULL){
printf("calloc error!");
exit(0);
}
printf("输入这%d个学生成绩: ",n);
for(i = 0; i < n; i++)
scanf("%f",&p[i]);
highest = lowest = p[0];
for(i = 0; i < n; i++){
average = average + p[i];
if(highest < p[i])
highest = p[i];
else if(lowest > p[i])
lowest = p[i];
}
average = average / n;
printf("运算结果: ");
printf("average = %.2f\thighest = %.1f\tlowest = %.1f\n",average,highest,lowest);
free(p);
}
这个自己写的 希望对你有帮助。
#include<stdlib.h>
main()
{
int n,t,i; //学生人数,临时存储,i循环控制
float *p,average,highest,lowest; //学生成绩,平均成绩,最高成绩,最低成绩
average = 0;
printf("输入学生个数: ");
scanf("%d",&n);
if((p = ((float *)malloc(sizeof(float)*n))) == NULL){
printf("calloc error!");
exit(0);
}
printf("输入这%d个学生成绩: ",n);
for(i = 0; i < n; i++)
scanf("%f",&p[i]);
highest = lowest = p[0];
for(i = 0; i < n; i++){
average = average + p[i];
if(highest < p[i])
highest = p[i];
else if(lowest > p[i])
lowest = p[i];
}
average = average / n;
printf("运算结果: ");
printf("average = %.2f\thighest = %.1f\tlowest = %.1f\n",average,highest,lowest);
free(p);
}
这个自己写的 希望对你有帮助。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询