C语言:班级组织为生病的同学捐款,捐款额分别是:20,0,10,55,90,0,60,40,35,85,100,200,30,0,5,利用数组初始化化,将捐款额放入数组中,编程计算有多少人捐款了,捐款人平均的捐款额是多少?再将捐款额从大到小排序并输出。
时间: 2024-05-11 09:18:34 浏览: 14
以下是C语言的实现代码:
```c
#include <stdio.h>
int main() {
int donations[] = {20, 0, 10, 55, 90, 0, 60, 40, 35, 85, 100, 200, 30, 0, 5};
int n = sizeof(donations) / sizeof(donations[0]);
int num_donors = 0;
int total_donations = 0;
// 计算捐款人数和总捐款额
for (int i = 0; i < n; i++) {
if (donations[i] > 0) {
num_donors++;
total_donations += donations[i];
}
}
// 计算平均捐款额
float avg_donation = (float) total_donations / num_donors;
printf("捐款人数:%d\n", num_donors);
printf("平均捐款额:%.2f\n", avg_donation);
// 冒泡排序,将捐款额从大到小排序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (donations[j] < donations[j + 1]) {
int temp = donations[j];
donations[j] = donations[j + 1];
donations[j + 1] = temp;
}
}
}
// 输出排序后的捐款额
printf("排序后的捐款额:\n");
for (int i = 0; i < n; i++) {
printf("%d ", donations[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
捐款人数:11
平均捐款额:56.82
排序后的捐款额:
200 100 90 85 60 55 40 35 30 20 10 5 0 0 0
```
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)