C语言入门--3x3转置矩阵

题目

C语言入门--3x3转置矩阵_第1张图片

答题思路

我这里使用的是两个多维数组,因此此段代码仅需修改输入部分既可用于多种对称矩阵的转置

第一个多维数组用于记录原始的矩阵排列,第二个多维数组用于记录转置后的矩阵排列

我的思路很简单,先将右上角的数交换到左下角(本人非数学专业,专业名词要使用错了还请包涵),再将左下角的数交换到右上角

完整的代码

#include 
#define Matrix_Number 3

int main(){

    int Row_Counter,Print_Counter;
    int Matrix[Matrix_Number][Matrix_Number];
    int Matrix_Switch[Matrix_Number][Matrix_Number];
    int Switch_Column_Counter;
    printf("3 X 3 Matrix:\n");

    //Input each number of ROW
    for (Row_Counter = 0;Row_Counter < Matrix_Number; Row_Counter++)
    {
        printf("ROW Number %d \n",Row_Counter + 1);

        printf("Please input Column Number : \n");
        scanf("%d,%d,%d",&Matrix[Row_Counter][0],&Matrix[Row_Counter][1],&Matrix[Row_Counter][2]);

    }

    //Print the Matrix before Switch
    printf("Before Switch :\n");
    for (Row_Counter = 0;Row_Counter < Matrix_Number; Row_Counter++)
    {
        for (Print_Counter = 0 ; Print_Counter < 2 ; Print_Counter++)
        {
            printf("%d  ",Matrix[Row_Counter][Print_Counter]);
        }
        printf("%d  \n",Matrix[Row_Counter][2]);
    }

    //Exchange the number in the upper right corner to the lower left corner
    for (int Switch_Row_Counter = 0 ; Switch_Row_Counter < Matrix_Number ; Switch_Row_Counter++)
    {
        for (Switch_Column_Counter = Switch_Row_Counter; Switch_Column_Counter < Matrix_Number ; Switch_Column_Counter++)
        {
            Matrix_Switch[Switch_Column_Counter][Switch_Row_Counter] = Matrix[Switch_Row_Counter][Switch_Column_Counter];
        }
    }

    //Exchange the number in the lower left corner to the upper right corner.
    for (int Switch_Row_Counter = 0 ; Switch_Row_Counter < Matrix_Number ; Switch_Row_Counter++)
    {
        for (Switch_Column_Counter = Switch_Row_Counter; Switch_Column_Counter < Matrix_Number ; Switch_Column_Counter++)
        {
            Matrix_Switch[Switch_Row_Counter][Switch_Column_Counter] = Matrix[Switch_Column_Counter][Switch_Row_Counter];
        }
    }

    //Print the Matrix after Switch
    printf("After Switch :\n");
    for (Row_Counter = 0;Row_Counter < Matrix_Number; Row_Counter++)
    {

        for (Print_Counter = 0 ; Print_Counter < 2 ; Print_Counter++)
        {
            printf("%d  ",Matrix_Switch[Row_Counter][Print_Counter]);
        }
        printf("%d  \n",Matrix_Switch[Row_Counter][2]);
    }
}

代码的分部讲解

交换的第一步

for (int Switch_Row_Counter = 0 ; Switch_Row_Counter < Matrix_Number ; Switch_Row_Counter++)
{
    for (Switch_Column_Counter = Switch_Row_Counter; Switch_Column_Counter < Matrix_Number ; Switch_Column_Counter++)
    {
        Matrix_Switch[Switch_Column_Counter][Switch_Row_Counter] = Matrix[Switch_Row_Counter][Switch_Column_Counter];
    }
}

最外层的循环控制器控制的是矩阵的行数,当 Switch_Row_Counter = 0 的时候,让嵌套的循环的控制器等于 0 ,嵌套的循环控制器控制的是矩阵的列,这样我们就可以读取原始的多维数组 Matrix[0] [0] 的数据了,这里对新的多维矩阵的[0] [0]进行赋值。当

Switch_Row_Counter = 1 的时候,Switch_Column_Counter = 1,这里的意思是,从矩阵的第二行的第二个开始交换,所以当第二行交换完成之后,就按照这个方法继续交换剩下的部分。

这样就将右上部分的数据交换到了新的矩阵的左下部分。

交换的第二步

for (int Switch_Row_Counter = 0 ; Switch_Row_Counter < Matrix_Number ; Switch_Row_Counter++)
{
    for (Switch_Column_Counter = Switch_Row_Counter; Switch_Column_Counter < Matrix_Number ; Switch_Column_Counter++)
    {
        Matrix_Switch[Switch_Row_Counter][Switch_Column_Counter] = Matrix[Switch_Column_Counter][Switch_Row_Counter];
    }
}

按照刚才的方法,我们仅仅将行与列的控制器在数组部分交换填写一下就可以了

更多的拓展使用

这里我们先抛开数据的输入部分,单就置换矩阵部分的程序来说

#define Matrix_Number 3

我定义了一个全局变量,就这一个变量就可以控制整个矩阵的大小,因此我们只需要更改这一个全局变量就可以进行更多的对称矩阵的置换

你可能感兴趣的:(C,语言入门)