p275第十章函数和指针
2016-02-02 21:12
267 查看
//varrar2d.c --使用变长数组的函数
#include <stdio.h>
#define ROWS 3
#define COLS 4
int sum2d (int rows,int cols,int ar[rows][cols]);
int main (void)
{
int i,j;
int rs=j;
int cs=10;
int junk[ROWS][COLS]={
{2,4,6,8},
{3,5,7,9},
{12,10,8,6}
};
int morejunk [ROWS][COLS+2]={
{20,30,40,50,60,70},
{5,6,7,8,9,10}
};
int varr[rs][cs];
for (i=0;i<rs;i++)
for (j=0;j<cs;j++)
varr[i][j]=i*j+j;
printf("3X5 array\n");
printf("Sum of all elements= %d\n",
sum2d(ROWS-1,COLS+2,morejunk));
printf ("3x10 VLA\n");
printf ("Sum of all elements = %d\n",
sum2d(ROWS-1,COLS+2,morejunk));
printf ("3x10 VLA\n");
printf ("Sum of all elements = %d\n",
sum2d(rs,cs,varr));
return 0;
}
//带有一个VLA参数的函数
int sum2d (int rows,int cols,int ar[rows][cols])
{
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;
}
#include <stdio.h>
#define ROWS 3
#define COLS 4
int sum2d (int rows,int cols,int ar[rows][cols]);
int main (void)
{
int i,j;
int rs=j;
int cs=10;
int junk[ROWS][COLS]={
{2,4,6,8},
{3,5,7,9},
{12,10,8,6}
};
int morejunk [ROWS][COLS+2]={
{20,30,40,50,60,70},
{5,6,7,8,9,10}
};
int varr[rs][cs];
for (i=0;i<rs;i++)
for (j=0;j<cs;j++)
varr[i][j]=i*j+j;
printf("3X5 array\n");
printf("Sum of all elements= %d\n",
sum2d(ROWS-1,COLS+2,morejunk));
printf ("3x10 VLA\n");
printf ("Sum of all elements = %d\n",
sum2d(ROWS-1,COLS+2,morejunk));
printf ("3x10 VLA\n");
printf ("Sum of all elements = %d\n",
sum2d(rs,cs,varr));
return 0;
}
//带有一个VLA参数的函数
int sum2d (int rows,int cols,int ar[rows][cols])
{
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;
}
相关文章推荐
- [WC2016]挑战NPC
- Hdu2066(一个人的旅行)
- 环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)
- Leetcode 316
- Android学习 RadioButton
- SimpleNews- Android MVP模式学习
- BZOJ 1013: [JSOI2008]球形空间产生器sphere
- java 单链表实现队列
- 10 ARC
- splay算法
- 【NOIP2010】乌龟棋
- C#测试题若干,都是基础阿
- Spring-定时任务之Quartz
- [土狗之路]coursera C语言进阶练习题 排队游戏
- Bug大全
- config 写入
- P29 (**) Compose arbitrarily many procedures of one argument.
- P29 (**) Compose arbitrarily many procedures of one argument.
- C语言求斐波拉切数列第n项
- linux命令学习之(sed)