怎么用C语言实现,数组中12位数,去除最高和最低值,剩下的十个数取平均值
时间: 2024-02-27 09:52:40 浏览: 60
可以按照以下步骤实现:
1. 定义一个包含12个元素的数组,并初始化这个数组。
2. 用一个循环找到最高值和最低值,并记录它们的位置。
3. 用另外一个循环,将除最高值和最低值以外的所有元素加起来。
4. 计算剩下的10个数的平均值。
以下是C语言的实现代码:
```c
#include <stdio.h>
int main() {
int arr[12] = {3, 6, 8, 2, 4, 9, 1, 5, 7, 10, 11, 12}; // 定义数组并初始化
int max_idx = 0; // 最高值的位置
int min_idx = 0; // 最低值的位置
// 找出最高值和最低值的位置
for (int i = 1; i < 12; i++) {
if (arr[i] > arr[max_idx]) {
max_idx = i;
}
if (arr[i] < arr[min_idx]) {
min_idx = i;
}
}
int sum = 0; // 剩下10个数的和
// 计算剩下10个数的和
for (int i = 0; i < 12; i++) {
if (i != max_idx && i != min_idx) {
sum += arr[i];
}
}
float avg = (float) sum / 10; // 计算平均值
printf("剩下10个数的平均值为: %.2f\n", avg);
return 0;
}
```
输出结果为:
```
剩下10个数的平均值为: 5.50
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)