#include <stdio.h> #define Row 2 #define Column 4 void main() { /** 將 4 * 2 矩陣轉換為 2 *4 矩陣 4 * 2 矩陣格式: 1 2 3 4 5 6 7 8 轉換后的 2 *4 矩陣格式: 1 5 2 6 3 7 4 8 **/ int i, j; int arr[Row][Column] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; int tempArr[Column][Row] = { }; printf("原矩陣 4 * 2 隊列:\n"); for (i = 0; i < Row; i++) //輸出原矩陣 { for (j = 0; j < Column; j++) { printf("%d\t", arr[i][j]); } printf("\n"); } //矩陣轉換 for (i = 0; i < Row; i++) { for (j = 0; j < Column; j++) { tempArr[j][i] = arr[i][j]; } } printf("\n轉置后矩陣 2 * 4 隊列:\n"); for (i = 0; i < Column; i++) //輸出轉置后的矩陣 { for (j = 0; j < Row; j++) { printf("%d\t", tempArr[i][j]); } printf("\n"); } }
文章評論