C语言实现某班数学课程12个人的考试成绩如下:66,98,33,99,77,54,87,99,55,77,78,58,用一维数组做函数参数计算数学课的平均分(要求保留1位小数)。
时间: 2023-07-27 08:17:32 浏览: 57
好的,您可以参考以下代码实现:
```c
#include <stdio.h>
double calculateAverage(int scores[], int length) {
int sum = 0;
for (int i = 0; i < length; i++) {
sum += scores[i];
}
return (double) sum / length;
}
int main() {
int scores[] = {66, 98, 33, 99, 77, 54, 87, 99, 55, 77, 78, 58};
int length = sizeof(scores) / sizeof(scores[0]);
double average = calculateAverage(scores, length);
printf("The average score is %.1f\n", average);
return 0;
}
```
这段代码定义了一个名为 `calculateAverage` 的函数,该函数接受两个参数:一个整型数组 `scores` 和数组的长度 `length`。函数的作用是计算数组中所有元素的平均值,并返回结果(注意返回值为 `double` 类型)。在 `main` 函数中,我们定义了一个数组 `scores` 来存储考试成绩,并调用 `calculateAverage` 函数计算平均分,最终输出结果。
相关问题
某班数学课程12个人的考试成绩如下:66,98,33,99,77,54,87 99,55,77,78,58,请用C语言的一维数组做函数参数计算这12个人的平均分,要求结果保留1位小数
以下是一个计算平均分的C语言函数:
```c
#include <stdio.h>
float calculateAverage(int scores[], int size) {
int sum = 0;
for (int i=0; i<size; i++) {
sum += scores[i];
}
return (float)sum / size;
}
int main() {
int scores[] = {66, 98, 33, 99, 77, 54, 87, 99, 55, 77, 78, 58};
int size = sizeof(scores) / sizeof(scores[0]);
float average = calculateAverage(scores, size);
printf("平均分为:%.1f", average);
return 0;
}
```
运行结果:
```
平均分为:73.8
```
这个函数接受一个整型数组和数组大小作为参数,通过循环遍历数组计算总分,最后用总分除以数组大小得到平均分,并返回一个浮点数类型的结果。在主函数中,我们定义了一个包含所有考试成绩的整型数组,并通过计算数组大小的方式传递给函数来计算平均分。最后,我们使用printf函数输出平均分,并使用%.1f的格式说明符保留1位小数。
C语言某班数学考试成绩如下:90,78+,98,67,53,97,78,100,76,88编程输出该班的总人数,最高分,最低分和平均分
以下是C语言代码,可以输出该班的总人数,最高分,最低分和平均分:
```c
#include <stdio.h>
int main() {
int scores[] = {90, 78, 98, 67, 53, 97, 78, 100, 76, 88};
int len = sizeof(scores) / sizeof(scores[0]);
int sum = 0, max = scores[0], min = scores[0];
float avg;
for (int i = 0; i < len; i++) {
sum += scores[i];
if (scores[i] > max) {
max = scores[i];
}
if (scores[i] < min) {
min = scores[i];
}
}
avg = (float)sum / len;
printf("总人数:%d\n", len);
printf("最高分:%d\n", max);
printf("最低分:%d\n", min);
printf("平均分:%.2f\n", avg);
return 0;
}
```
输出结果为:
```
总人数:10
最高分:100
最低分:53
平均分:82.70
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)