![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /*--------------------------------- 2 array2d.c -- 处理二维数组的函数 3 ---------------------------------*/ 4 5 #include <stdio.h> 6 #define ROWS 3 7 #define COLS 4 8 9 void sum_rows(int ar[][COLS], int rows); 10 void sum_cols(int [][COLS], int); 11 int sum2d(int (*ar)[COLS], int rows); 12 13 int main() 14 { 15 int junk[ROWS][COLS] = 16 { 17 {2, 4, 6, 8}, 18 {3, 5, 7, 9}, 19 {12, 10, 8, 6} 20 }; 21 22 sum_rows(junk, ROWS); 23 sum_cols(junk, ROWS); 24 printf("Sum of all elements = %d\n", sum2d(junk, ROWS)); 25 26 return 0; 27 } 28 29 void sum_rows(int ar[][COLS], int rows) 30 { 31 for (int r(0); r != rows; ++r) 32 { 33 int total(0); 34 for (int c(0); c != COLS; ++c) 35 total += ar[r][c]; 36 printf("row %d: sum = %d\n", r, total); 37 } 38 } 39 40 void sum_cols(int ar[][COLS], int rows) 41 { 42 for (int c(0); c != COLS; ++c) 43 { 44 int total(0); 45 for (int r(0); r != rows; ++r) 46 total += ar[r][c]; 47 printf("col %d: sum = %d\n", c, total); 48 } 49 } 50 51 int sum2d(int ar[][COLS], int rows) 52 { 53 int total(0); 54 55 for(int r(0); r != rows; ++r) 56 for(int c(0); c != COLS; ++c) 57 total += ar[r][c]; 58 59 return total; 60 }