Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印出一个矩阵及其转置,C#_C#_Algorithm_Matrix_Transpose - Fatal编程技术网

打印出一个矩阵及其转置,C#

打印出一个矩阵及其转置,C#,c#,algorithm,matrix,transpose,C#,Algorithm,Matrix,Transpose,我有一个创建转置5x8矩阵的程序。我已经创建了一个多维5x8数组,并且还创建了一个新数组,用于保存多维数组的转置。问题是,我首先想把原始矩阵写到控制台,在同一行上,我想写出转置。这是我的密码: class Program { static void Main(string[] args) { int[,] matrix = new int[5, 8] { { 1, 2, 3, 4, 5,6,7,8 }, { 9,10,11,1

我有一个创建转置5x8矩阵的程序。我已经创建了一个多维5x8数组,并且还创建了一个新数组,用于保存多维数组的转置。问题是,我首先想把原始矩阵写到控制台,在同一行上,我想写出转置。这是我的密码:

 class Program
{
    static void Main(string[] args)
    {


        int[,] matrix = new int[5, 8] { 
       { 1, 2, 3, 4, 5,6,7,8 }, 
       { 9,10,11,12,13,14,15,16},
       { 17,18,19,20,21,22,23,24 },
       { 25,26,27,28,29,30,31,32 },
       { 33,34,35,36,37,38,39,40 },

        };

        for (int j = 0; j < 8; j++)
        {
            for (int r = 0; r < 5; r++)
                Console.Write("{0} ", matrix[r, j]);

            Console.WriteLine();
        }

        int[,] newArray = new int[8, 5];
        for (int j = 0; j < 8; j++)
            for (int r = 0; r < 5; r++)
                newArray[j, r] = matrix[r, j];




        Console.ReadLine();

    }
}
类程序
{
静态void Main(字符串[]参数)
{
int[,]矩阵=新的int[5,8]{
{ 1, 2, 3, 4, 5,6,7,8 }, 
{ 9,10,11,12,13,14,15,16},
{ 17,18,19,20,21,22,23,24 },
{ 25,26,27,28,29,30,31,32 },
{ 33,34,35,36,37,38,39,40 },
};
对于(int j=0;j<8;j++)
{
对于(int r=0;r<5;r++)
Write(“{0}”,矩阵[r,j]);
Console.WriteLine();
}
int[,]newArray=newint[8,5];
对于(int j=0;j<8;j++)
对于(int r=0;r<5;r++)
newArray[j,r]=矩阵[r,j];
Console.ReadLine();
}
}
我希望在控制台窗口上显示的内容如下:


但我只能打印转置矩阵。如何解决此问题?

在计算转置后,您可以同时打印两行

 for (int j = 0; j < 8; j++)
    {
        //write a line from the first matrix
        for (int r = 0; r < 5; r++)
            Console.Write("{0} ", matrix[r, j]);

        //add some spaces for visual separation
        Console.Write("\t\t");

        //write a line from the transpose matrix
        for (int r = 0; r < 5; r++)
            Console.Write("{0} ", newArray[r, j]);

        Console.WriteLine();
    }
for(int j=0;j<8;j++)
{
//从第一个矩阵中写一行
对于(int r=0;r<5;r++)
Write(“{0}”,矩阵[r,j]);
//添加一些用于视觉分隔的空间
Console.Write(“\t\t”);
//从转置矩阵中写出一行
对于(int r=0;r<5;r++)
Write(“{0}”,newArray[r,j]);
Console.WriteLine();
}