数组05 - 零基础入门学习C语言27
第六章:数组05
让编程改变世界
Change the world by program
二维数组元素的引用和初始化
数据类型 数组名 [常量表达式1][常量表达式2] ={ 初始化数据 };我们有4种方法对二维数组初始化:
(1) 直接分行给二维数组赋初值。 如:int a[3][4]={{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
(2) 可以将所有数据写在一个花括弧内, 按数组排列的顺序对各元素赋初值。 如:int a[3][4]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
(3) 可以对部分元素赋初值。 如:int a[3][4]={{1}, {5}, {9}};
有请直观图: [caption id="attachment_98" align="aligncenter" width="263"]![二维数组](http://blog.fishc.com/wp-content/uploads/2012/07/图片17.gif)
int a[3][4]={{1}, {0, 6}, {0, 0, 11}};
有图有真相: [caption id="attachment_99" align="aligncenter" width="255"]![二维数组](http://blog.fishc.com/wp-content/uploads/2012/07/图片23.gif)
int a[3][4]={{1}, {5, 6}};
有图有真相: [caption id="attachment_100" align="aligncenter" width="290"]![二维数组](http://blog.fishc.com/wp-content/uploads/2012/07/图片32.gif)
int a[3][4]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
它等价于:int a[][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
在定义时也可以只对部分元素赋初值而省略第一维的长度, 但应分行赋初值。 如:int a[][4]={{0, 0, 3}, {}, {0, 10}};
有图有真相: [caption id="attachment_101" align="aligncenter" width="288"]![二维数组](http://blog.fishc.com/wp-content/uploads/2012/07/图片41.gif)
![二维数组](http://blog.fishc.com/wp-content/uploads/2012/07/图片5-150x86.gif)
int a[5][3]={ {80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85} };
2) 按行连续赋值可写为:int a[5][3]={ 80,75,92,61,65,71,59,63,70,85,87,90,76,77,85};
这两种赋初值的结果是完全相同的。 程序实现如下: [codesyntax lang="c"]#include <stdio.h> void main() { int i, j, s=0, average, v[3]; int a[5][3] ={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}}; for(i=0; i < 3; i++) { for(j=0;j<5;j++) s = s + a[j][i]; v[i] = s / 5; s = 0; } average = (v[0] + v[1] + v[2]) / 3; printf("math:%dnc languag:%dndFoxpro:%dn", v[0], v[1], v[2]); printf("total:%dn", average); }[/codesyntax]
二维数组程序举例
NO.1 将一个二维数组行和列元素互换, 存到另一个二维数组中。
将数组a[2][3]转化为数组b[3][2] [caption id="attachment_103" align="aligncenter" width="300"]![二维数组举例](http://blog.fishc.com/wp-content/uploads/2012/07/图片6-300x83.gif)
#include <stdio.h> void main() { int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; int b[3][2], i, j; printf(″array a: n″); for(i=0;i<=1;i++) { for(j=0;j<=2;j++) { printf(″%5d″, a[i][j]); b[j][i] = a[i][j]; } printf(″n″); } printf(″array b:n″); for (i=0;i<=2;i++) { for(j=0;j<=1;j++) printf("%5d″, b[i][j]); printf(″n″); } }[/codesyntax]
NO.2 有一个3×4的矩阵, 要求编程序求出其中值最大的那个元素的值, 以及其所在的行号和列号。
[caption id="attachment_104" align="aligncenter" width="300"]![二维数组举例](http://blog.fishc.com/wp-content/uploads/2012/07/图片7-300x182.gif)
#include <stdio.h> void main() { int i, j, row=0, colum=0, max; int a[3][4] = {{1, 2, 3, 4}, {9, 8, 7, 6}, {-10, 10, -5, 2}}; max = a[0][0]; for (i=0;i<=2;i++) { for (j=0;j<=3;j++) { if (a[i][j]>max) { max = a[i][j]; row = i; colum = j; } } } printf(″max=%d, row=%d, colum=%dn″, max, row, colum); }[/codesyntax]
NO.3 从键盘上输入9个整数, (对照九宫格的形式, 输入三行, 每行输入三个数) 保存在二维数组中, 按数组原来位置输出第一行和第一列的所有元素。
[caption id="attachment_105" align="aligncenter" width="300"]![二维数组转换](http://blog.fishc.com/wp-content/uploads/2012/07/8-300x127.gif)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步