0% found this document useful (0 votes)
41 views4 pages

Matrix Programs

The document contains Python programs to perform operations on arrays and matrices: 1) Define and initialize a one-dimensional array, and print the array elements. 2) Read elements into a one-dimensional array from user input and print the array. 3) Read elements into a two-dimensional matrix from user input and print the matrix. 4) Define functions to add two matrices and multiply two matrices, if they are of compatible dimensions.

Uploaded by

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

Matrix Programs

The document contains Python programs to perform operations on arrays and matrices: 1) Define and initialize a one-dimensional array, and print the array elements. 2) Read elements into a one-dimensional array from user input and print the array. 3) Read elements into a two-dimensional matrix from user input and print the matrix. 4) Define functions to add two matrices and multiply two matrices, if they are of compatible dimensions.

Uploaded by

praneethgowda11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Program to declare a single dimensional array, initialize the array elements and display

those array elements.

from array import *


a = array('i',[12,34,56,78,90])
print("The array Elements are: ")
print(a)
print("The array Elements are: ")
print(a[0])
print(a[1])
print(a[2])
print(a[3])
print(a[4])
print("The array Elements are: ")
for i in range(len(a)):
print(a[i])

Program to declare a single dimensional array, Read the array elements from keyboard
and display those array elements.

from array import *


a = array('i',[])
n = int(input("Enter Array Size: "))
for i in range(n):
x = int(input("Enter Array Element: "))
a.append(x)
print("The array Elements are: ")
for i in range(n):
print(a[i])
Write a oython program to read the two dimensional array elements from keyboard
and display those elements in matrix form.

def twod_matrix(m, n):


Outp = []
for i in range(m):
row = []
for j in range(n):
num = int(input("Enter the element: "))
row.append(num)
Outp.append(row)
return Outp

def disp_mat(K, m, n):


for i in range(m):
for j in range(n):
print(K[i][j],end=" ")
print()

m = int(input("Enter the Martrix Rows: "))


n = int(input("Enter the Matrix Columns: "))

print("Enter the Matrix Elements: ")


A = twod_matrix(m, n)
print("The Matrix Elements are: ")
disp_mat(A,m,n)
Addition of two Matrices:

def twod_mat(m, n):


Outp = [] # initially output matrix is empty
for i in range(m): # iterate to the end of rows
row = []
for j in range(n): # j iterate to the end of column
num = int(input("Enter the element: "))
row.append(num) # add the user element to the end of the row
Outp.append(row) # append the row to the output matrix
return Outp

def disp_mat(K, m, n):


for i in range(m):
for j in range(n):
print(K[i][j],end=" ")
print()

def add_mat(A,B,m, n):


Outp = []
for i in range(m):
row = []
for j in range(n):
x = A[i][j] + B[i][j]
row.append(x)
Outp.append(row)
disp_mat(Outp,m,n)

m = int(input("Enter the First Martrix Rows: "))


n = int(input("Enter the First Matrix Columns: "))
p = int(input("Enter the Second Matrix Rows: "))
q = int(input("Enter the Second Matrix Columns: "))

print("Enter the First Matrix Elements: ")


A = twod_mat(m, n)
print("Enter the Second Matrix Elements: ")
B = twod_mat(p, q)

print("The First Matrix Elements are: ")


disp_mat(A,m,n)
print("The Second Matrix Elements are: ")
disp_mat(B,p,q)

print("The Addition of Two Matrices is ")


if(m==p and n==q):
add_mat(A,B,m,n)
else:
print("is not possible")
Multiplication of Two Matrices:

def twod_mat(m, n):


Outp = [] # initially output matrix is empty
for i in range(m): # iterate to the end of rows
row = []
for j in range(n): # j iterate to the end of column
num = int(input("Enter the element: "))
row.append(num) # add the user element to the end of the row
Outp.append(row) # append the row to the output matrix
return Outp

def disp_mat(K, m, n):


for i in range(m):
for j in range(n):
print(K[i][j],end=" ")
print()

def mul_mat(A,B,m,n,q):
Outp = []
for i in range(m):
row = []
for j in range(q):
x=0
for k in range(n):
x += A[i][k] * B[k][j]
row.append(x)
Outp.append(row)
disp_mat(Outp,m,q)

m = int(input("Enter the First Martrix Rows: "))


n = int(input("Enter the First Matrix Columns: "))
p = int(input("Enter the Second Matrix Rows: "))
q = int(input("Enter the Second Matrix Columns: "))

print("Enter the First Matrix Elements: ")


A = twod_mat(m, n)
print("Enter the Second Matrix Elements: ")
B = twod_mat(p, q)

print("The First Matrix Elements are: ")


disp_mat(A,m,n)
print("The Second Matrix Elements are: ")
disp_mat(B,p,q)

print("The Multiplication of Matrices is ")


if(n == p):
mul_mat(A,B,m,n,q)
else:
print("is not possible")

You might also like