Easy Tutorial
❮ C Function Getchar C Function Strtok ❯

C Language Example - Matrix Transposition

C Language Examples

Matrix Transposition.

Example

#include <stdio.h>

int main()
{
    int a[10][10], transpose[10][10], r, c, i, j;
    printf("Enter the number of rows and columns of the matrix: ");
    scanf("%d %d", &r, &c);

    // Store matrix elements
    printf("\nEnter matrix elements:\n");
    for(i=0; i&lt;r; ++i)
        for(j=0; j&lt;c; ++j)
        {
            printf("Enter element a%d%d: ",i+1, j+1);
            scanf("%d", &a[i][j]);
        }

    // Display matrix a[][]
    printf("\nEntered matrix: \n");
    for(i=0; i&lt;r; ++i)
        for(j=0; j&lt;c; ++j)
        {
            printf("%d  ", a[i][j]);
            if (j == c-1)
                printf("\n\n");
        }

    // Transpose
    for(i=0; i&lt;r; ++i)
        for(j=0; j&lt;c; ++j)
        {
            transpose[j][i] = a[i][j];
        }

    // Display the transposed matrix
    printf("\nTransposed matrix:\n");
    for(i=0; i&lt;c; ++i)
        for(j=0; j&lt;r; ++j)
        {
            printf("%d  ",transpose[i][j]);
            if(j==r-1)
                printf("\n\n");
        }

    return 0;
}

Output:

Enter the number of rows and columns of the matrix: 2 3

Enter matrix elements:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered matrix: 
2  3  4  

5  6  4  


Transposed matrix:
2  5  

3  6  

4  4

C Language Examples

❮ C Function Getchar C Function Strtok ❯