C Program to Find Transpose of a Matrix - IProgramX

Q. Write a program to accept a matrix A of size mXn and store its transpose in matrix B. Display matrix B. Write separate functions. 


Program

#include <stdio.h>
void main()
{
    static int array[10][10];
    int i, j, m, n;

    printf("Enter the order of the matrix \n");
    scanf("%d %d", &m, &n);
    printf("Enter the coeficients of the matrix\n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &array[i][j]);
        }
    }
    printf("The given matrix is \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            printf(" %d", array[i][j]);
        }
        printf("\n");
    }
    printf("Transpose of matrix is \n");
    for (j = 0; j < n; ++j)
    {
        for (i = 0; i < m; ++i)
        {
            printf(" %d", array[i][j]);
        }
        printf("\n");
    }
}

Output:

Enter the order of the matrix
3
3
Enter the coeficients of the matrix
3
6
2
5
4
9
8
7
4
The given matrix is
 3 6 2
 5 4 9
 8 7 4
Transpose of matrix is
 3 5 8
 6 4 7
 2 9 4

Post a Comment

0 Comments