c
  1. c-program-to-find-transpose-of-a-matrix

Program to Find Transpose of a Matrix - (C Programs)

In this tutorial, we'll discuss how to write a C program to find the transpose of a matrix.

Syntax

void transpose(int arr[][COL], int transposedArr[][ROW])

Example

#include <stdio.h>
#define ROW 3
#define COL 3

void transpose(int arr[][COL], int transposedArr[][ROW]);

int main()
{
    int arr[ROW][COL] = { {1, 2, 3},
                          {4, 5, 6},
                          {7, 8, 9} };
    int transposedArr[COL][ROW], i, j;

    transpose(arr, transposedArr);

    printf("Original matrix:\n");
    for(i = 0; i < ROW; i++)
    {
        for(j = 0; j < COL; j++)
        {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }

    printf("Transposed matrix:\n");
    for(i = 0; i < COL; i++)
    {
        for(j = 0; j < ROW; j++)
        {
            printf("%d ", transposedArr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

void transpose(int arr[][COL], int transposedArr[][ROW])
{
    int i, j;

    for(i = 0; i < ROW; i++)
    {
        for(j = 0; j < COL; j++)
        {
            transposedArr[j][i] = arr[i][j];
        }
    }
}

Output

Original matrix:
1 2 3
4 5 6
7 8 9
Transposed matrix:
1 4 7
2 5 8
3 6 9

Explanation

The program takes a matrix of size ROW x COL as input and returns the transpose of the matrix. The transpose of a matrix is obtained by interchanging the rows and columns of the matrix.

The program defines two matrices arr and transposedArr of sizes ROW x COL and COL x ROW respectively. It then reads in the elements of arr from the user.

The function transpose takes in the original matrix arr as input and returns its transpose in the matrix transposedArr. The function does this by interchanging the rows and columns of the original matrix, and storing the result in transposedArr.

Finally, the program displays the original matrix arr and its transpose transposedArr on the screen.

Use

Matrices and matrix operations are used in a variety of applications, including mathematics, physics, computer science, and more. The transpose of a matrix is an important operation that is used in many mathematical and scientific calculations.

Summary

In this tutorial, we discussed how to write a C program to find the transpose of a matrix. We covered syntax, example, output, explanation, use, and important points of the program. By understanding how to find the transpose of a matrix in C, you can perform a variety of matrix operations in your programs.

Published on: