简体   繁体   中英

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. Initialize the 2D array with elements as shown below in the original matrix using the initializer list. Display the original matrix and the transpose. There must only be one 2D array in the program.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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