《数据结构与算法之8 求和函数》
1 #include <stdio.h> 2 #define N 6 3 int sum(int n, const arr[]);//求和函数声明declare 4 int main(void){ 5 int arr[N]; 6 int i; 7 int t_sum = 0; 8 9 for (i = 0; i < N;i++){//input int number N 10 scanf("%d", &arr[i]); 11 } 12 t_sum = sum(N, arr);//求和函数调用calling 13 printf("the totally sum of the arr = %d.\n", t_sum); 14 return 0; 15 } 16 17 int sum(int n, const arr[]){//求和函数定义define 18 int sum = 0; 19 int i; 20 21 for (i = 0; i < n; i++){ 22 sum += arr[i]; 23 } 24 25 return sum; 26 }