从键盘上输入若干学生的成绩,直到输入-1结束。求平均分和及格率并保留1位小数输出

2024-11-22 05:23:11
推荐回答(1个)
回答1:

C语言源程序:
#include "stdio.h"

#define MAX 100

void main()
{
int arr[MAX];
int i, size = 0;
int temp;
double sum = 0, avg, rate;
int pass = 0;

while(1)
{
printf("score : ");
scanf("%d", &temp);
if(temp == -1)
break;
arr[size++] = temp;
sum += temp;
if(temp >= 60)
pass++;
}

avg = sum / size;
rate = 1.0 * pass / size;

printf("avg = %.1lf, pass = %.1lf%\n", avg, rate*100);
}

测试输出:
score : 87
score : 45
score : 78
score : 43
score : 100
score : -1
avg= 70.6, pass = 60.0%