• 第 10 章 数组和指针


     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
    ", 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
    ", 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
    ", 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 }
    array2d.c

  • 相关阅读:
    矩阵旋转
    clang-format 规范及 Visual Stido Code 自定义格式化代码风格
    Windows 安装 MinGW-w64
    Linux配置Visual Stdio Code
    Ubuntu中安装eclipse
    Ubuntu安装JDK11
    Ubuntu安装搜狗输入法
    C/C++算术运算(类型使用)的注意事项
    闰年判断与日期计算
    查看变量类型
  • 原文地址:https://www.cnblogs.com/web1013/p/9106208.html
Copyright © 2020-2023  润新知