简体   繁体   English

矩阵二维数组的转置

[英]Transpose of a Matrix 2D array

Please enlighten me on how to create a C program that finds the transpose of an order 5 matrix represented by a two-dimensional (2D) array. 请启发我如何创建一个C程序,以找到由二维(2D)数组表示的5阶矩阵的转置。 Initialize the 2D array with elements as shown below in the original matrix using the initializer list. 使用初始值设定项清单,在原始矩阵中使用如下所示的元素初始化2D阵列。 Display the original matrix and the transpose. 显示原始矩阵和转置。 There must only be one 2D array in the program. 程序中只能有一个2D数组。

Example: 例:

Original matrix 原始矩阵

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

Transpose of the Matrix: 矩阵转置:

1      6       11      16      21

2      7       12      17      22

3      8       13      18      23

4      9       14      19      24

5      10      15      20      25

Transpose of a given matrix can be calculated as below : 给定矩阵的转置可以如下计算:

#include<stdio.h>
void main()
{
    int c,r,i,j;
    printf("Enter number of rows and columns : ");
    scanf("%d %d",&r,&c);
    int arr[r][c];
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("\nEnter element : ");
            scanf("%d",&arr[i][j]);
        }
    }
    printf("\nOriginal array is : \n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            printf("%d\t",arr[i][j]);
        }
        printf("\n");

    }
    printf("\nTranspose array is : \n");
    for(i=0;i<c;i++)
    {
        for(j=0;j<r;j++)
        {
            printf("%d\t",arr[j][i]);
        }
        printf("\n");

    }
}

As there must be only one array in the program, a valid approach would be to transpose the matrix in-place, which can be done with the following nested loops. 由于程序中必须只有一个数组,因此一种有效的方法是就地转置矩阵,这可以通过以下嵌套循环来完成。

for( int i = 0; i < n; i++)
{
    for ( j = i+1; j < n; j++ ) // only the upper is iterated
    {
        swap(&(a[i][j]), &(a[j][i]));
    }
}

The following subroutine would perform the swapping, where pointers to int are used. 以下子例程将执行交换,其中将使用指向int指针。 In the calling code above, these are obtained by using the addresses of the elements to be swapped. 在上面的调用代码中,这些是通过使用要交换的元素的地址获得的。

void swap(int* arg1, int* arg2)
{
    int buffer = *arg1;
    *arg1 = *arg2;
    *arg2 = buffer;
}
int array[5][5]={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};

//transpose
cout<<"TRANSPOSE"<<endl;
for(int i=0;i<=4;i++){   
  for(int j=0;j<=4;j++)  {      
    if(array[i]>array[j]){           
      int temp;              
      temp=array[i][j];               
      array[i][j]=array[j][i];                
      array[j][i]=temp;
    }      
  }
}

As question asks only for displaying a transpose... it should be easy to. 由于问题仅要求显示转置,所以应该很容易。

int matrix[5][5] = {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};
for(int i=0;i<5;i++) {
    for (int j=0;j<5;j++) {
        std::cout<<matrix[j][i]<<" ";
    }
    std::cout<<"\n";
}

EDIT 1: replace cout with printf to get it working in C compiler 编辑1:用printf替换cout以使其在C编译器中工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM