Here in this program, we will code to transpose a matrix. By transposing a matrix means we will change its row with its columns.
E.g. Suppose a matrix 2 3 4 5 6 7 8 9 10
The transpose of the upper matrix would be: 2 5 8 3 6 7 4 7 10
C++ & Python Program to transpose a Matrix
Transpose a Matrix in C++
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int arr[10][10],r,c;
cout<<"Enter the number of rows: ";
cin>>r;
cout<<"Enter the number of Columns: ";
cin>>c;
cout<<"Enter the matrix element:\n";
for(int i=0;i<c;i++)
for(int j=0; j<r; j++)
cin>>arr[i][j];
cout<<"The transpose of the matrix would be\n";
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
cout<<arr[j][i]<<" ";
cout<<endl;
}
getch();
}
Output:
Enter the number of rows: 2
Enter the number id Columns: 3
Enter the matrix elements
1
2
3
4
5
6
The transpose of the matrix would be
1 4
2 5
3 6
Transpose a Matrix In Python:
a=[]
arr =[]
r= int(input("Enter the number of rows: "))
c= int(input("Enter the number id Columns: "))
print("Enter the matrix elements")
for i in range(r):
a =[]
for j in range(c):
a.append(int(input()))
arr.append(a)
print("The transpose of the matrix would be")
for i in range(c):
for j in range(r):
print(arr[j][i],end=" ")
print()
Output:
Enter the number of rows: 3
Enter the number id Columns: 3
Enter the matrix elements
12
23
12
34
45
65
47
23
877
The transpose of the matrix would be
12 34 47
23 45 23
12 65 877
People are also reading:
- Program to print the truth table for XY+Z
- Python Program to Find the Factors of Number
- Program to print the 1 to 10 Multiples of a Number
- Programming in C and Java to convert from octal to decimal
- Program to calculate the sum of two numbers
- Matrix in Python
- Python Program to Add Two Matrices
- Python Program to Find Numbers Divisible by Another Number
- Program to convert a given number of days into years, weeks and days
- Python Program to Display the Multiplication Table
- Program to Calculate Simple Interest
- Python Program to Illustrate Different Set Operations
Leave a Comment on this Post