Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Code that inputs two square matrices and then calls methods that add, sub

ID: 3765851 • Letter: P

Question

Python Code that inputs two square matrices and then calls methods that add, subtract, and multiply the two matrices. Your program must first ask the user the size of the square matrices that are to be used. This must be a number from 2 to 6. Next the user is requested to enter the two matrices. Next the program prints out the two matrices and then invokes the three methods. The first method adds the two matrices, the next subtracts the second from the first, and the third multiplies the two matrices. The program must print the results of these operations after each of the methods returns. Make certain to output these matrices in a way that is easy to read and understand!

Explanation / Answer

// Please copy paste wisely otherwise you can fall in to identation Error.

Multiplication :

def main():
   print("Enter size of the Square Matrix")
   size=int(input())
   mata=[]
   matb=[]
   matp=[]
   print ('Enter elements of Mat. A')
   for i in range(size):
       temp=[]
       for j in range(size):
           print ('Enter A',i,j)
           temp.append(int(input()))
       mata.append(temp)
   print (' Your Matrix A')
   for tmp in mata:
       print (tmp)

   print (' Enter elements of Mat. B')
   for i in range(size):
       temp=[]
       for j in range(size):
           print ('Enter B',i,j)
           temp.append(int(input()))
       matb.append(temp)
   print (' Your Matrix B')
   for tmp in matb:
       print (tmp)

   for i in range(size):
       temp=[]
       for j in range(size):
           prod=0
           for k in range(size):
               prod=prod+mata[i][k]*matb[k][j]
           temp.append(prod)
       matp.append(temp)

   print (' Product of Matrix A & B')
   for tmp in matp:
       print (tmp)

   return 0


main()

*********************

Addition

def main():
    print("Enter size of the Square Matrix")
    size=int(input())
    mata=[]
    matb=[]
    matp=[]
    print ('Enter elements of Mat. A')
    for i in range(size):
        temp=[]
        for j in range(size):
            print ('Enter A',i,j)
            temp.append(int(input()))
        mata.append(temp)
    print (' Your Matrix A')
    for tmp in mata:
        print (tmp)

    print (' Enter elements of Mat. B')
    for i in range(size):
        temp=[]
        for j in range(size):
            print ('Enter B',i,j)
            temp.append(int(input()))
        matb.append(temp)
    print (' Your Matrix B')
    for tmp in matb:
        print (tmp)

    for i in range(size):
        temp=[]
        matp.append(temp)
        for j in range(size):
            add=mata[i][j]+matb[i][j]
            temp.append(add)
      

    print (' Addition of Matrix A & B')
    for tmp in matp:
        print (tmp)

    return 0


main()

Substraction

def main():
   print("Enter size of the Square Matrix")
   size=int(input())
   mata=[]
   matb=[]
   matp=[]
   print ('Enter elements of Mat. A')
   for i in range(size):
       temp=[]
       for j in range(size):
           print ('Enter A',i,j)
           temp.append(int(input()))
       mata.append(temp)
   print (' Your Matrix A')
   for tmp in mata:
       print (tmp)

   print (' Enter elements of Mat. B')
   for i in range(size):
       temp=[]
       for j in range(size):
           print ('Enter B',i,j)
           temp.append(int(input()))
       matb.append(temp)
   print (' Your Matrix B')
   for tmp in matb:
       print (tmp)

   for i in range(size):
       temp=[]
       matp.append(temp)
       for j in range(size):
           diff=mata[i][j]-matb[i][j]
           temp.append(diff)
      

   print (' Difference of Matrix A & B')
   for tmp in matp:
       print (tmp)

   return 0


main()