標籤:c 執行個體 矩陣轉置 數組
直接上代碼,在代碼中有對矩陣的學習,包括初始化學習以及如何使用等。
#include <stdio.h>/** * 給出提示,要求輸入數組A * ,通過二維數組,進行數組的轉置 * 得出數組B,輸出結果 * * 該執行個體主要是為了進行學習二維數組 * @brief main * @return */int main(void){ /** * 二維數組的初始化: * 1:分行給二維數組賦值 * static int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; * * 2:將所有資料寫在一個大括弧中 * static int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12}; * * 3:對數組進行部分賦值 * static int a[3][4] = {{1},{2},{3}}; * 相當於該數組為 * 1 0 0 0 * 2 0 0 0 * 3 0 0 0 */ //下面進行執行個體編寫 int row,colume; printf("Please the number of row and colume of the array(divided by ‘,‘):\n"); scanf("%d,%d",&row,&colume); //擷取輸入的行數和列數 //定義數組A int array[row][colume]; int i,j; //擷取使用者的輸入來填充數組A for(i = 0;i < row;i++){ for(j = 0;j < colume;j++){ printf("Please enter the number in (%d,%d):\n",i,j); scanf("%d",&array[i][j]); } } //定義數組B int MatrixB[colume][row]; //進行轉置 /** * 兩個數組如果相互轉置的話, * 則一個數組的行等於另一個數組的列 * 一個數組的列等於另一個數組的行 * 注意: * 轉置之後的矩陣的行數和列數為轉置之前的列數和行數 */ for(i = 0;i < colume;i++){ for(j = 0;j < row;j++){ MatrixB[i][j] = array[j][i]; } } //輸出矩陣B for(i = 0;i < colume;i++){ for(j = 0;j < row;j++){ printf("%d\t",MatrixB[i][j]); } printf("\n"); } return 0;}
C數組實現矩陣的轉置