Skip to main content

Program to define a function which calculates the transpose of a square matrix

#Program to define a function which calculates the transpose of a square matrix and test it with main.
def imatrix(m):
 A=[]
 for i in range(m):
  a=input("Enter the data of row "+str(i+1)+" of the matrix:").split(",")
  b=list(a)
  A.append(b)
 return A
def showm(A):
 m=len(A)
 for i in range (m):
  print(A[i])
def transm(A):
 m=len(A)
 n=len(A[0])
 B=[[0 for i in range (m)] for j in range (n)]
 for i in range (m):
  for j in range (n):
   B[j][i]=A[i][j]
 return B
m=input("Enter the dimension of the square matrix:")
m=int(m)
X=imatrix(m)
print("The matrix is:")
showm(X)
print("Transpose of the matrix is:")
Y=transm(X)
showm(Y)

Comments