用动态分配内存空间,输入若干个成绩,去掉两个最高的,两个最低分,求平均成绩
时间: 2023-05-21 09:05:03 浏览: 76
非常高兴为您解答。在这个问题中,我们可以使用动态内存分配来实现成绩输入和排序。以下是一些可能的伪代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int num_scores, i;
float average_score = 0;
printf("请输入成绩数量: ");
scanf("%d", &num_scores);
// 用malloc动态分配内存
float *scores = (float*)malloc(num_scores * sizeof(float));
// 输入成绩
for (i = 0; i < num_scores; i++) {
printf("请输入第%d个成绩: ", i+1);
scanf("%f", &scores[i]);
}
// 对成绩进行排序
for (i = 0; i < num_scores; i++) {
int max_index = i;
int min_index = i;
int j;
// 找到最大和最小的成绩
for (j = i+1; j < num_scores; j++) {
if (scores[j] > scores[max_index])
max_index = j;
if (scores[j] < scores[min_index])
min_index = j;
}
// 交换最大和最小的成绩
float temp = scores[i];
scores[i] = scores[min_index];
scores[min_index] = temp;
if (max_index == i)
max_index = min_index;
// 交换第二大和第二小的成绩
temp = scores[num_scores-i-1];
scores[num_scores-i-1] = scores[max_index];
scores[max_index] = temp;
}
// 计算平均成绩
for (i = 2; i < num_scores-2; i++) {
average_score += scores[i];
}
average_score /= (num_scores - 4);
printf("平均成绩为: %.2f\n", average_score);
// 用free释放动态分配的内存
free(scores);
return 0;
}
```
希望这个答案能帮到您。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)