数组求平均分最高分最低分
时间: 2023-09-03 15:08:40 浏览: 54
好的,我会尽力回答你的问题。要求一个数组的平均分、最高分和最低分,可以按照以下步骤进行:
1. 首先,遍历数组,累加数组中所有元素的值得到总分数。
2. 然后,计算平均分数,将总分数除以数组的长度即可。
3. 接着,遍历数组,找到最高分和最低分。
4. 最后,返回平均分数、最高分和最低分。
下面是一个示例代码,你可以参考一下:
```python
def calculate_scores(arr):
total_score = 0
max_score = float('-inf')
min_score = float('inf')
for score in arr:
total_score += score
if score > max_score:
max_score = score
if score < min_score:
min_score = score
avg_score = total_score / len(arr)
return avg_score, max_score, min_score
```
希望这个回答对你有帮助,如果你有其他问题,欢迎随时提问。
相关问题
用C语言去掉一组数组的最高分和最低分求平均值
好的,您可以参考以下代码实现该功能:
```c
#include <stdio.h>
int main() {
int scores[] = {90, 85, 92, 88, 95, 80};
int len = sizeof(scores) / sizeof(scores[0]); // 数组长度
int max = scores[0], min = scores[0]; // 初始化最高分和最低分
int sum = 0;
// 找到最高分和最低分
for (int i = 0; i < len; i++) {
if (scores[i] > max) {
max = scores[i];
}
if (scores[i] < min) {
min = scores[i];
}
}
// 求和(去掉最高分和最低分)
for (int i = 0; i < len; i++) {
if (scores[i] != max && scores[i] != min) {
sum += scores[i];
}
}
// 求平均值
float avg = (float)sum / (len - 2);
printf("去掉最高分和最低分的平均值为: %.2f\n", avg);
return 0;
}
```
这段代码先找出数组中的最高分和最低分,然后再去掉这两个分数求和,最后除以数组长度减去2(去掉最高分和最低分)即可得到平均值。
c语言用数组求十个人成绩的平均分,最高分和最低分
好的,我可以帮你解决这个问题。你可以使用一个长度为10的数组来存储这十个人的成绩,然后通过遍历数组来求平均分、最高分和最低分。下面是一个示例代码:
```c
#include <stdio.h>
int main() {
int scores[10];
int sum = 0, max = 0, min = 100;
for (int i = 0; i < 10; i++) {
printf("请输入第%d个人的成绩:", i+1);
scanf("%d", &scores[i]);
sum += scores[i];
if (scores[i] > max) {
max = scores[i];
}
if (scores[i] < min) {
min = scores[i];
}
}
printf("平均分:%d\n", sum / 10);
printf("最高分:%d\n", max);
printf("最低分:%d\n", min);
return 0;
}
```
在这个示例代码中,我们定义了一个长度为10的整型数组`scores`来存储十个人的成绩。然后,我们定义了三个变量:`sum`用来存储成绩总分,`max`用来存储最高分,`min`用来存储最低分。接着,我们使用`for`循环遍历数组,输入每个人的成绩,并且更新`sum`、`max`和`min`。最后,我们输出平均分、最高分和最低分。
希望这可以帮助到你!