从键盘任意输入10个整数存入数组,自定义函数float add(int a[],int n))计算数组中正数的平均数,编写main函数测试add函数 c语言
时间: 2024-03-21 11:39:32 浏览: 26
好的,以下是一个示例代码,使用 C 语言实现:
```c
#include <stdio.h>
// 自定义函数,计算数组中正数的平均数
float add(int a[], int n) {
int sum = 0;
int count = 0;
for (int i = 0; i < n; i++) {
if (a[i] > 0) {
sum += a[i];
count++;
}
}
if (count > 0) {
return (float)sum / count;
} else {
return 0;
}
}
// 主函数,测试 add 函数
int main() {
int a[10];
// 从键盘输入 10 个整数
for (int i = 0; i < 10; i++) {
printf("请输入第%d个整数:", i+1);
scanf("%d", &a[i]);
}
// 调用 add 函数计算正数平均数
float avg = add(a, 10);
// 输出结果
printf("数组中正数的平均数为:%f\n", avg);
return 0;
}
```
您可以将这段代码复制到您的 C 语言编译器中进行编译运行,然后按照提示输入 10 个整数即可。程序会输出这 10 个数中正数的平均数。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)