#include <stdio.h>
#define A 35
#define B 30
int main()
{
float average(float array[], int n);
float score1[35], score2[30]; //定义两个数组分别用来存放两个班级的学生成绩
int i;
printf("Please star enter Class A %d studens score:\n",A);
for (i = 0;i < A;i++)
{
printf("Please enter Class A the %d studens score:\n", i + 1);
scanf("%f", &score1[i]);
if (score1[i] < 0 || score1[i]>100)
{
printf("Input Error, Please reenter %d student:\n ", i + 1);
i--;
}
}
printf("\n\nPlease start Class B %d studens score:\n",B);
for (i = 0;i < B;i++)
{
printf("Please enter Class B the %d studens score:\n", i + 1);
scanf("%f", &score2[i]);
if (score2[i] < 0 || score2[i]>100)
{
printf("Please enter Class B the %d studens score:\n", i + 1);
i--;
}
}
printf("The average of class A is %6.2f\n", average(score1, A));
printf("The average of class B is %6.2f\n", average(score2, B));
return 0;
}
float average(float array[], int n)
{
int i;
float aver, sum = array[0];
for (i = 1;i < n;i++)
sum = sum + array[i];
aver = sum / n;
return(aver);
}
求两个班级的平均成绩
最新推荐文章于 2023-03-09 17:12:37 发布