1. 程式人生 > 實用技巧 >c語言二維陣列的轉置

c語言二維陣列的轉置

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 3
#define transsize 3 void trans(int A[][maxsize],int B[][transsize],int m, int n){
printf("轉置前----\n");
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
printf("%d\t",B[i][j]);
}
printf("\n");
} for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
B[j][i] = A[i][j];
}
}
printf("轉置後----\n");
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
printf("%d\t",B[i][j]);
}
printf("\n");
} }
int main(){
int a[][maxsize]={{1,2,3},{4,5,6},{7,8,9}};
int b[][transsize]={{1,2,3},{4,5,6},{7,8,9}};
trans(a,b,transsize,maxsize);
return 0;
}