分别求出数组中所有3的倍数之和以及5的倍数之和。形参n给了数组中数据的个数,利用指针three返回3的倍数之和,利用指针five返回5的倍数之和。例如: 数组中的值依次为:1, 5,9,2,3,15,11, 6,10;则利用指针three返回3的倍数之和33;利用指针five返回5的倍数之和30。请在下面空白处完善这一函数的功能,并且调试运行出结果
时间: 2024-03-01 11:56:22 浏览: 27
好的,以下是完善后的代码:
```c
#include <stdio.h>
void sum(int arr[], int n, int *three, int *five) {
*three = 0;
*five = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 3 == 0) {
*three += arr[i];
}
if (arr[i] % 5 == 0) {
*five += arr[i];
}
}
}
int main() {
int arr[] = {1, 5, 9, 2, 3, 15, 11, 6, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int three, five;
sum(arr, n, &three, &five);
printf("The sum of numbers that are divisible by 3 is: %d\n", three);
printf("The sum of numbers that are divisible by 5 is: %d\n", five);
return 0;
}
```
函数 `sum` 用于求出数组中所有3的倍数之和以及5的倍数之和,使用指针 `three` 返回3的倍数之和,使用指针 `five` 返回5的倍数之和。在函数中,首先将指针 `three` 和 `five` 初始化为0,然后遍历数组,对于数组中的每一个数,如果它是3的倍数,就将它加到 `three` 中;如果它是5的倍数,就将它加到 `five` 中。最后在 `main` 函数中调用 `sum` 函数,并输出结果。