0% found this document useful (0 votes)
74 views6 pages

Assignment A03

The document describes a Python program that performs various matrix operations - addition, subtraction, multiplication, and transpose of matrices. The program defines functions to accept matrix input, perform the different operations, and display the output matrices. It contains a main function that runs an interactive menu allowing the user to choose an operation and input the required matrices to compute the result.

Uploaded by

Sanchit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views6 pages

Assignment A03

The document describes a Python program that performs various matrix operations - addition, subtraction, multiplication, and transpose of matrices. The program defines functions to accept matrix input, perform the different operations, and display the output matrices. It contains a main function that runs an interactive menu allowing the user to choose an operation and input the required matrices to compute the result.

Uploaded by

Sanchit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment- A03

Name of Student: Darshan Yogesh Nerkar


Roll No.: SECOMP-B63

Problem Statement:Write a Python program to compute following


computation on matrix:

a) Addition of two matrices

b) Subtraction of two matrices

c) Multiplication of two matrices

d) Transpose of a matrix
Program:
def accept_matrix(M):
print("Enter the row and column of the matrices : ")
r = int(input("\t row = "))
c = int(input("\t column = "))
print("Enter the elements in the matrix")
for i in range (r):
A = []
for j in range (c):
A.append(int(input()))
M.append(A)

def addition_Matrix(M1,M2,M3,r,c) :
for i in range (r):
a =[]
for j in range (c):
a.append((M1[i][j])+(M2[i][j]))
M3.append(a)

def substraction_Matrix(M1,M2,M3,r,c) :
for i in range (r):
a =[]
for j in range (c):
a.append((M1[i][j])-(M2[i][j]))
M3.append(a)
def multiplication_matrix (M1,M2,M3,r1,c1,c2):
for i in range (r1):
A = []
for j in range (c2):
sum = 0
for k in range (c1):
sum = sum + (M1[i][k]+M2[k][j])
A.append(sum)
M3.append(A)

def Transpose_matrix (M,r,c,T):


for i in range (c):
A = []
for j in range (r):
A.append(M[j][i])
T.append(A)

def display(M,r,c):
for i in range (r):
print("\t\t",end=' ')
for j in range (c):
print("%4d" %M[i][j],end=' ')
print(" ")

def main():
M1=[]
M2=[]

accept_matrix(M1)
r1 = len(M1)
c1 = len(M1[0])
display(M1,r1,c1)
accept_matrix(M2)
r2 = len(M2)
c2 = len(M2[0])
display(M2,r2,c2)

while True:
M3 = []
print("Enter 1 to perform Addition.")
print("Enter 2 to perform Subtraction.")
print("Enter 3 to perform Multiplication.")
print("Enter 4 to perform Transpose.")
print("Enter 5 to Exit.")

ch = int(input("Enter your choice : "))

if (ch == 1):
if (r1 == r2 and c1 == c2):
print("Addition of 2 matrices : \n")
addition_Matrix(M1,M2,M3,r1,c1)
display(M3,r1,c1)
else:
print("Can't perform Addition.")

elif (ch == 2):


if (r1 == r2 and c1 == c2):
print("Subtraction of 2 matrices : \n")
substraction_Matrix(M1,M2,M3,r1,c1)
display(M3,r1,c1)
else:
print("Can't perform Subtraction.")
elif (ch == 3):
if(c1==r2):
print("Multiplication of matrices is : \n")
multiplication_matrix(M1,M2,M3,r1,c1,c2)
display(M3,r1,c2)
else:
print("Multiplication is not possible.")
elif ( ch == 4 ):
print("Transpose of matrix M1 is : \n")
Transpose_matrix(M1,r1,c1,M3)
display(M3,c1,r1)
print("Transpose of matrix M2 is : \n")
M4 =[]
Transpose_matrix(M2,r2,c2,M4)
display(M4,c2,r2)
elif (ch == 5):
exit()
else:
print("Enter a valid option.")

main()

Output:

You might also like