-
peng111说:
用C语言编写一个函数fun用来统计并输出某一门课的不及格人数,平均成绩和最低
成绩
#include #define N 20 void fun(float a[N], int n) { int i, j=0; float temp, avg, min=a[0]; for (i=0; i a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } for (i=1; i<n; i++) avg += a[i]; avg /= n-1; printf("不及格人数: %d\n平均成绩: %.2f\n最低成绩: %.2f\n", j, avg, min); } int main() { float a[N] = {78.5, 98.0, 57.3, 60.8, 70.2, 59.6, 80.1, 65.8, 82.7, 89.9, 55.0, 74.8, 83.3, 74.4, 71.0, 91.1, 81.2, 63.4, 99.9, 67.8}; int n = N; fun(a, n); return 0; }
-
@ChatGPT #include
#define N 20void fun(float a[N], int n)
{
int i, j=0;
float temp, avg, min=a[0];
for (i=0; i a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i=1; i<n; i++)
avg += a[i];
avg /= n-1;
printf("不及格人数: %d\n平均成绩: %.2f\n最低成绩: %.2f\n", j, avg, min);
}int main()
{
float a[N] = {78.5, 98.0, 57.3, 60.8, 70.2, 59.6, 80.1, 65.8, 82.7, 89.9, 55.0, 74.8, 83.3, 74.4, 71.0, 91.1, 81.2, 63.4, 99.9, 67.8};
int n = N;
fun(a, n);
return 0;
}解释一下
-
peng111说:
#include
#define N 20void fun(float a[N], int n)
{
int i, j=0;
float temp, avg, min=a[0];
for (i=0; i a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (i=1; i avg += a[i];
avg /= n-1;
printf("不及格人数: %d\n平均成绩: %.2f\n最低成绩: %.2f\n", j, avg, min);
}int main()
{
float a[N] = {78.5, 98.0, 57.3, 60.8, 70.2, 59.6, 80.1, 65.8, 82.7, 89.9, 55.0, 74.8, 83.3, 74.4, 71.0, 91.1, 81.2, 63.4, 99.9, 67.8};
int n = N;
fun(a, n);
return 0;
}解释一下
:
这段代码的功能是对N个成绩进行排序,计算不及格人数、平均成绩和最低成绩。
首先,通过for循环对数组a进行冒泡排序,把最低成绩放在最前面;然后,使用另一个for循环,从第二个成绩开始,累加每个成绩,最后除以总成绩数计算出平均成绩;最后,计算不及格人数,即小于60分的成绩个数,并输出结果。