C language: two-dimensional array transposition, two-row three-column matrix becomes three-row two-column matrix

First of all, we need to understand that the essence of a two-dimensional array is a matrix in mathematics

So we only need to convert the array subscript to make the array transpose (1,2)-->(2,1)

例:int a [2][3] --> int b [3][2]

Output two-dimensional array using nested for loop

#include<stdio.h> 
int main(){ 
// 二维数组转置
 	int a[2][3]={1,2,3,4,5,6},b[3][2]; 
// 	使用嵌套for循环输出二维数组 
 	for(int i=0;i<2;i++){
 		for(int j=0;j<3;j++){
 			printf("%d",a[i][j]);//输出原始数组 
 			b[j][i]=a[i][j];//原数组的值给到新数组 
		 }
		 printf("\n");
	 }
	 for(int i=0;i<3;i++){
 		for(int j=0;j<2;j++){
 			printf("%d",b[i][j]);//输出转置数组 
		 }
	     printf("\n");
	 }
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_63987141/article/details/129169685