p271第十章 数组和指针

//array2d.c --处理二维数组的函数
#include <stdio.h>
#define ROWS 3
#define COLS 4
void SUM_rows (int ar[][COLS],int rows);
void SUM_cols (int [][COLS],int );
int sum2d (int (*ar)[COLS],int rows) ;
int main (void)
{
int junk [ROWS][COLS] = {
  {2,4,6,8},
  {3,5,7,9},
  {12,10,8,6}
};

SUM_rows (junk,ROWS);
SUM_cols (junk,ROWS);
printf ("Sum of all elements = %d\n",sum2d(junk,ROWS));
return 0;
}
void SUM_rows (int ar[][COLS],int rows)
{
int r;
int c;
int tot;
for (r=0;r<rows;r++)
{
tot=0;
for (c=0;c<COLS;c++)
tot += ar[r][c];
printf ("row %d: sum=%d\n",r,tot);
}
}


void SUM_cols (int ar[][COLS],int rows)
{
int r;
int c;
int tot;
for (c=0;c<COLS;c++)
{
tot=0;
for (r=0;r<rows;r++)
tot += ar[r][c];
printf("col %d; sum = %d\n",c,tot);
}
}


int sum2d (int ar[][COLS],int rows)
{
int r;
int c;
int tot=0;
for (r=0;r<rows;r++)
for (c=0;c<COLS;c++)
tot += ar[r][c];
return tot;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值