1)分别求出数组中所有3的倍数之和以及5的倍数之和。形参n给了数组中数据的个数,利用指针three返回3的倍数之和,利用指针five返回5的倍数之和。例如:数组中的值依次为:1,5,9,2,3,15,11,6,10;则利用指针three返回3的倍数之和33;利用指针five返回5的倍数之和30。请在下面空白处完善这一函数的功能,并且调试运行出结果。 源代码: 运行结果: #include <stdio.h> #define N 20 void fun(int *a,int n,int *three,int *five) { } void main() { int a[N]={1,5,9,2,3,15,11,6,10},i,n=9,three,five; printf("The original data is:\n"); for(i=0;i<n;i++) printf("%5d",*(a+i)); printf("\n\n"); fun(a,n,&three,&five); printf("3的倍数之和:%d\n",three); printf("5的倍数之和:%d\n",five); }
时间: 2024-01-14 11:03:02 浏览: 30
完善的代码如下:
```
#include <stdio.h>
#define N 20
void fun(int *a, int n, int *three, int *five) {
*three = 0; // 初始化3的倍数之和
*five = 0; // 初始化5的倍数之和
for (int i = 0; i < n; i++) {
if (*(a + i) % 3 == 0) {
*three += *(a + i); // 累加3的倍数
}
if (*(a + i) % 5 == 0) {
*five += *(a + i); // 累加5的倍数
}
}
}
void main() {
int a[N] = {1, 5, 9, 2, 3, 15, 11, 6, 10}, i, n = 9, three, five;
printf("The original data is:\n");
for (i = 0; i < n; i++) {
printf("%5d", *(a + i));
}
printf("\n\n");
fun(a, n, &three, &five);
printf("3的倍数之和:%d\n", three);
printf("5的倍数之和:%d\n", five);
}
```
运行结果:
```
The original data is:
1 5 9 2 3 15 11 6 10
3的倍数之和:33
5的倍数之和:30
```
相关推荐
![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)