Compute the Transpose of a Matrix - C Array

C examples for Array:Multidimensional Arrays

Description

Compute the Transpose of a Matrix

Demo Code

#include <stdio.h>

int main()/*from w  ww.j a va  2  s .  co m*/
{
    int mat[12][12], transpose[12][12];
    int i, j, row = 5, col = 5;

    for (i = 0; i < row; i++)
    {
        printf("Enter %d values for row no. %d : ", col, i);
        for (j = 0; j < col; j++)
            scanf("%d", &mat[i][j]);
    }

    printf("\nMatrix A:\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d\t", mat[i][j]);
        }
        printf("\n");
    }

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            transpose[j][i] = mat[i][j];
        }
    }

    printf("\nTranspose of matrix A: \n");
    for (i = 0; i < col; i++)
    {
        for (j = 0; j < row; j++)
        {
            printf("%d\t", transpose[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Result


Related Tutorials