P216 例4.写一个函数,使给定的一个3*3的二维整形数组转置
即行列互换

#define _CRT_SECURE_NO_WARNINGS#includeint main() {void change(int arr[3][3]);int a[3][3] = { 0 };int i = 0, j = 0;printf("请输入9个整数:\n");//赋值for (i = 0; i < 3; i++) {for (j = 0; j < 3; j++) {scanf("%d", &a[i][j]);}}//打印初始的数组printf("初始的数组\n");for (i = 0; i < 3; i++) {for (j = 0; j < 3; j++) {printf("%5d", a[i][j]);}printf("\n");}//交换change(a);//打印交换后的数组printf("\n交换后的数组\n");for (i = 0; i < 3; i++) {for (j = 0; j < 3; j++) {printf("%5d", a[i][j]);}printf("\n");}return 0;}void change(int arr[3][3]) {int a = 0, b = 0, t = 0;for (a = 0; a < 3; a++) {for (b = a+1; b < 3; b++) {//这里必须b=a+1,如果b=0,换了之后又会换回去t = arr[a][b];arr[a][b] = arr[b][a];arr[b][a] = t;}}}

运行结果: