某班数学课程12个人的考试成绩如下:66,98,33,99,77,54,87 99,55,77,78,58,请用C语言的一维数组做函数参数计算这12个人的平均分,要求结果保留1位小数
时间: 2024-05-13 20:19:39 浏览: 32
以下是一个计算平均分的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语言实现某班数学课程12个人的考试成绩如下:66,98,33,99,77,54,87,99,55,77,78,58,用一维数组做函数参数计算数学课的平均分(要求保留1位小数)。
好的,您可以参考以下代码实现:
```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` 函数计算平均分,最终输出结果。
某班的数学考试成绩如下:+90、78、98、67、53、97、78、100、76、88。编程计算并输出该班的总人数、最高分、最低分和平均分
该班的数学考试成绩如下:+90、78、98、67、53、97、78、100、76、88。编程计算并输出该班的总人数、最高分、最低分和平均分的Python代码如下:
```python
scores = [90,78, 98, 67, 53, 97, 78, 100, 76, 88]
total = len(scores) # 总人数
max_score = max(scores) # 最高分
min_score = min(scores) # 最低分
avg_score = sum(scores) / total # 平均分
print("总人数:", total)
print("最高分:", max_score)
print("最低分:", min_score)
print("平均分:", avg_score)
```
输出结果为:
```
总人数: 10
最高分: 100
最低分: 53
平均分: 82.5
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)