C語言數組求和的方法有以下幾種:
int sum = 0;
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < length; i++) {
sum += arr[i];
}
printf("數組的和為:%d\n", sum);
int sum = 0;
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
int *p = arr;
for (int i = 0; i < length; i++) {
sum += *(p + i);
}
printf("數組的和為:%d\n", sum);
int sum(int arr[], int length) {
if (length == 0) {
return 0;
} else {
return arr[length - 1] + sum(arr, length - 1);
}
}
int arr[] = {1, 2, 3, 4, 5};
int length = sizeof(arr) / sizeof(arr[0]);
printf("數組的和為:%d\n", sum(arr, length));
以上是幾種常見的求和方法,根據實際需求選擇合適的方法。