Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C++
#include< stdio.h >

int main()
{
    int m, n, c, d, matrix[10][10], transpose[10][10];

    printf ("Enter the number of rows and columns of matrix: \n");
    scanf ("%d%d",&m,&n);
    printf ("Enter the elements of matrix: \n");

    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            scanf("%d",&matrix[c][d]);
        }
    }

    for(c = 0; c < m; c++)
    {
        for(d = 0; d < n; d++)
        {
            transpose[d][c] = matrix[c][d];
        }
    }

    printf("Transpose of entered matrix: \n");

    for(c = 0; c < n; c++)
    {
        for(d = 0; d < m; d++)
        {
            printf("%d\t",transpose[c][d]);
        }
        printf("\n");
    }

    return 0;
}
Posted
Updated 11-Oct-17 20:22pm
v2

Sure it is. You're so very close!

Just remember what the transposition of a matrix actually involves - it involves reflecting the elements across the diagonal axis that runs from top-left to bottom-right.

That is to say:
[0 1]    [0 2]
[2 3]    [1 3]

[0 1 2]  [0 3 6]
[3 4 5]  [1 4 7]
[6 7 8]  [2 5 8]


In each case, the row & column indicess of an element are swapped unless the row index == column index.

So, you just need a single temporary variable of whatever type your matrix holds. Its just a series of (N^2 - N) swap operations, where N is the number of rows or columns - assuming a square matrix.

Put the contents of [row][column] into tmpVal
Set the contents of [row][column] to be [column][row]
Set the contents of [column][row] to be tmpVal

Naturally, this doesn't take into account the fact that we don't have to copy elements if column==row. Its actually probably quicker just to blast through, never comparing the column and the row. Theres relatively few element this will be true for - worst case is 50%, when you've got a 2x2 matirx - or for a 10x10 matrix, it will be 10 elements. This can be calculated (for a square matrix) by N/(N^2) So, as the matrix grows larger, the potential savings get ever smaller.

Yet, if you try to do this 'optimization' you'll find yourself comparing the row to the column for 100% of the elements.
 
Share this answer
 
Comments
kumar_11 23-Nov-13 14:27pm    
I really appreciate your way of making things clear.
Just one doubt of mine,Is it doable where the condition arises like raw!=column.
enhzflep 23-Nov-13 14:41pm    
That's okay, you're really welcome.

A tuna sandwich is less valuable than a fishing lesson. A tuna sandwich and a fishing lesson can often be even better again!

Yep, it is doable when row != column, in fact - the only time that you _need_ to swap is when row != column. It will be quicker to swap regardless of whether row==column or not (no conditional branching or pipeline stalls).

Since [1][1] is always going to be [1][1], and [2][2] always goes to [2][2], you can safely swap [row=1][column=1] with [column=1][row=1] etc, etc.

Probably best you forget all about this mention of an optimization for now. It will only make the code less clear to read, for very, very little gain. Often optimized code is harder to read than incorrect, yet simply implemented code.
kumar_11 24-Nov-13 3:47am    
Thank you so much for the answer.
I request you to write the code here because I tried so hard but still not getting result.
enhzflep 24-Nov-13 3:55am    
Code or it didn't happen! :p

If you add what you've tried, I'll try to guide you further.
kumar_11 24-Nov-13 4:16am    
Here is what I have written, Kindly correct me where I am wrong.
//transpose of matrix 12/10/2017 ashu jain
#include<stdio.h>
int main()
{
int arr[10][10],i,j,row,col,temp;
printf("how many rows and columns you want to take");
scanf("%d %d",&row,&col);
printf("enter the elements of matrix");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&arr[i][j]);
printf("transpose of matrix is\n");
if(row<col)
{
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{

if(i<j)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}}
else

for(i=0;i<row;i++)
for(j=0;j<col;j++)
{

if(i>j)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}}
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
}
 
Share this answer
 
v2
Comments
Richard Deeming 12-Oct-17 13:20pm    
Even if we ignore the fact that this question was asked, answered, and solved FOUR YEARS AGO, an unformatted code-dump is not a solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900