Matrix transpose in C
In this post, you will learn different ways to perform matrix transpose using the C programming language.
A matrix is a set of numbers arranged in rows and columns so as to form a rectangular array. Matrices are utilised substantially more in everyday life than most would have suspected. A square matrix can represent a linear transformation of a geometric object. A real-life example is Adobe Photoshop. It uses a matrix to process linear transformations to render images. In robotics and automation, matrices are the fundamental building blocks for robot development. The contributions for controlling robots are acquired based on the calculations from matrices.
The transpose of a matrix is simply a flipped version of the original matrix. We can transpose a matrix by switching its rows with its columns, i.e., writing the elements of the rows as columns and writing the elements of a column as rows. For example, the value in the 1st row and 3rd column ends up in the 3rd row and 1st column. In other words, the transpose of B[][] is obtained by changing B[i][j] to B[j][i]. These are different ways to find the transpose of a matrix in the C programming language.
Transpose of a Matrix in C using for loop
In the given C program, we have used nested loops to iterate through each row and each column. In each iteration, we place the matrix[i][j] element into transpose[j][i].
#include <stdio.h>
int main(){
int row, col, i, j, matrix[10][10], transpose[10][10];
printf("Enter rows and columns: \n");
scanf("%d%d", &row, &col);
printf("Enter elements of the matrix: \n");
for (i= 0; i < row; i++)
for (j = 0; j < col; j++)
scanf("%d", &matrix[i][j]);
for (i = 0;i < row;i++)
for (j = 0; j < col; j++)
transpose[j][i] = matrix[i][j];
printf("Transpose of the matrix: \n");
for (i = 0; i < col; i++) {
for (j = 0; j < row; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
return 0;
}
Output of the above code:
Enter rows and columns:
3 3
Enter elements of the matrix:
5 9 2
8 4 2
4 9 4
Transpose of the matrix:
5 8 4
9 4 9
2 2 4
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative