EXERCISE - 11 MULTI-DIMENSIONAL LISTS

A) Write a program that defines a matrix and prints. B) Write a program to perform addition of two square matrices. C) Write a program to perform multiplication of two square matrices.
A) AIM: Write a program that defines a matrix and prints.
SOURCE CODE:
mtrx=[[]] rows=int(input("Enter number of rows:")) cols=int(input("Enter number of columns:")) print("Enter %d X %d matrix elements::"%(rows,cols)) mtrx=[[int(input()) for j in range(cols)] for i in range(rows)] print("Given matrix is::") for i in range(rows): for j in range(cols): print(mtrx[i][j],end="\t") print()
OUTPUT:
Enter number of rows:3 Enter number of columns:2 Enter 3 X 2 matrix elements:: 2 5 6 8 3 9 Given matrix is:: 2 5 6 8 3 9

B) AIM: Write a program to perform addition of two square matrices.
SOURCE CODE:
mtrx1=[[]] mtrx2=[[]] mtrxsum=[[]] r1=int(input("Enter number of rows of first matrix:")) c1=int(input("Enter number of columns of first matrix:")) r2=int(input("Enter number of rows of second matrix:")) c2=int(input("Enter number of columns of second matrix:")) if(r1==r2 and c1==c2): print("Enter first matrix elements %d X %d::"%(r1,c1)) mtrx1=[[int(input()) for j in range(c1)] for i in range(r1)] print("Enter second matrix elements %d X %d::"%(r2,c2)) mtrx2=[[int(input()) for j in range(c2)] for i in range(r2)] mtrxsum =[[mtrx1[i][j]+mtrx2[i][j] for j in range(c1)] for i in range(r1)] print("Addition of two matrices is::") for i in range(r1): for j in range(c1): print(mtrxsum[i][j],end="\t") print() else: print("Addition of two matrices is impossible.")
OUTPUT:
Enter number of rows of first matrix:3 Enter number of columns of first matrix:2 Enter number of rows of second matrix:3 Enter number of columns of second matrix:2 Enter first matrix elements 3 X 2:: 1 2 3 4 5 6 Enter second matrix elements 3 X 2:: 6 7 8 9 10 11 Addition of two matrices is:: 7 9 11 13 15 17
C) AIM: Write a program to perform multiplication of two square matrices.
SOURCE CODE:
mtrx1=[[]] mtrx2=[[]] mtrxmul=[[]] r1=int(input("Enter number of rows of first matrix:")) c1=int(input("Enter number of columns of first matrix:")) r2=int(input("Enter number of rows of second matrix:")) c2=int(input("Enter number of columns of second matrix:")) if(c1==r2): print("Enter first matrix elements %d X %d::"%(r1,c1)) mtrx1=[[int(input()) for j in range(c1)] for i in range(r1)] print("Enter second matrix elements %d X %d::"%(r2,c2)) mtrx2=[[int(input()) for j in range(c2)] for i in range(r2)] mtrxmul=[[0 for j in range(c2)] for i in range(r1)] for i in range(r1): for j in range(c2): for k in range(c1): mtrxmul[i][j]=mtrxmul[i][j]+mtrx1[i][k]*mtrx2[k][j] print("Multiplication of two matrices is:") for i in range(r1): for j in range(c2): print(mtrxmul[i][j],end="\t") print() else: print("Multiplication of two matrices is impossible.")
OUTPUT:
Enter number of rows of first matrix:3 Enter number of columns of first matrix:2 Enter number of rows of second matrix:2 Enter number of columns of second matrix:2 Enter first matrix elements 3 X 2:: 1 2 3 4 5 6 Enter second matrix elements 2 X 2:: 7 8 9 10 Multiplication of two matrices is: 25 28 57 64 89 100