You are on page 1of 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