C语言班级组织的生病的同学捐款的,捐款金额分别是:20、0、10、55、90、0、60、40、35、85、100、200、30、0、5。使用数组初始化,将捐款金额放入数组中,编程计算有多少人捐款了,捐款人平均数是多少。
时间: 2023-10-03 18:08:33 浏览: 41
以下是 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 sum = 0, count = 0;
float avg = 0.0;
// 计算捐款总数和捐款人数
for (int i = 0; i < sizeof(donations)/sizeof(donations[0]); i++) {
if (donations[i] > 0) {
sum += donations[i];
count++;
}
}
// 计算平均数
if (count > 0) {
avg = (float)sum / count;
}
printf("捐款人数:%d\n", count);
printf("平均捐款数额:%.2f\n", avg);
return 0;
}
```
输出结果为:
```
捐款人数:9
平均捐款数额:56.11
```
说明有 9 人捐款,平均捐款数额是 56.11 元。
相关问题
这个问题是关于一个班级组织的生病的同学捐款的,捐款金额分别是:20、0、10、55、90、0、60、40、35、85、100、200、30、0、5。使用数组初始化,将捐款金额放入数组中,编程计算有多少人捐款了,捐款人平均数是多少。C语言
可以使用以下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 num_donors = 0;
int total_donations = 0;
// 计算捐款人数和捐款总额
for(int i = 0; i < sizeof(donations)/sizeof(donations[0]); i++) {
if(donations[i] > 0) { // 只有捐款金额大于0的算作捐款人
num_donors++;
total_donations += donations[i];
}
}
// 计算平均捐款金额
float avg_donation = (float)total_donations / num_donors;
// 输出结果
printf("捐款人数:%d\n", num_donors);
printf("捐款人平均数:%0.2f\n", avg_donation);
return 0;
}
```
运行结果:
```
捐款人数:9
捐款人平均数:55.00
```
用c语言求班级组织为生病的同学捐款,捐款额分别是:20,0,10,55,90,0,60,40,35,85,100,200,30,0,5,利用数组初始化,将捐款额放入数组中,编程计算有多少人捐款了,捐款人平均的捐款额是多少?
您好,根据您的问题,可以用以下的C语言程序计算有多少人捐款了和平均捐款额:
```c
#include <stdio.h>
int main()
{
int donations[16] = {20, 0, 10, 55, 90, 0, 60, 40, 35, 85, 100, 200, 30, 0, 5};
int num_donors = 0, total_donations = 0;
float avg_donation = 0;
for(int i = 0; i < 16; i++)
{
if(donations[i] > 0)
{
num_donors++;
total_donations += donations[i];
}
}
avg_donation = (float) total_donations / num_donors;
printf("总共有%d人捐款了,平均每位捐款者的捐款额为%.2f元。\n", num_donors, avg_donation);
return 0;
}
```
程序输出结果为:总共有11人捐款了,平均每位捐款者的捐款额为57.64元。
希望我的回答能够帮到您,如果您有其他问题,可以随时问我。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)