Functions - Matrix - Transpose of a Matrix

# Functions and Matrix
# Transpose of a Matrix

# Function - Read Matrix
def fn_read(r, c):
    M = []
    print("Enter values of Matrix : ")
    for i in range(0,r,1):
        tmp = []
        for j in range(0,c,1):
            ele = int(input())
            tmp.append(ele)
        M.append(tmp)
    return M

# Function - Display Matrix
def fn_display(M):
    for row in M:
        for ele in row:
            print(ele, end=" ")
        print()

# Function - Transpose
def fn_transpose(MA, r, c):
    M = []
    for i in range(0,c,1):
        tmp = []
        for j in range(0,r,1):
            ele = MA[j][i]
            tmp.append(ele)
        M.append(tmp)
    return M

# Main Program

import sys

Mat_A = []
Mat_Transpose = []

print("Enter number of rows and columns :")
r1 = int(input("Matrix 1 - Row : "))
c1 = int(input("Matrix 1 - Column : "))

# Transpose
Mat_A = fn_read(r1, c1)
       
print("Given Matrix : ")   
fn_display(Mat_A)
   
Mat_Transpose = fn_transpose(Mat_A, r1, c1)
print("Transpose of given matrices : ")
fn_display(Mat_Transpose)   
   

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu