In: Computer Science
Implement the following functions in a module called arrayfunctions.py. You must use the booksite modules stdarray and stdio to implement the functions.
print1Darray() takes a one dimensional integer array argument and prints it (field width for each element should be 5).
print2Darray() takes a two dimensional integer array argument and prints it (field width for each element should be 5 - should be printed as a m x n matrix).
add2Darrays() takes 2, two dimensional integer array arguments. You can assume the arrays are the exact same size. The function returns a new array that is the addition of the 2 arrays passed in.
print2DarrayBoolean() takes a two dimensional boolean array argument and prints a * in place of True and a space in place of False. Include row and column numbers.
Codes for above methods in Python3 are as follows:
def print1Darray(arr):
for value in arr:
print("%-5d" % value,end='')
print("")
def print2Darray(arr):
for row in arr:
print1Darray(row)
def add2Darrays(a,b):
add=[]
for i in range(len(a)):
temp=[]
for j in range(len(a[0])):
temp.append(a[i][j]+b[i][j])
add.append(temp)
return add
def print2DarrayBoolean(arr):
for row in arr:
for col in row:
if(col):
print("*",end='')
else:
print(" ",end='')
print("")
print1Darray([1,2,3,4,5])
print("")
print2Darray([[1,2,3],[4,5,6]])
print("")
print2Darray(add2Darrays([[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]))
print("")
print2DarrayBoolean([[True,False,True],[True,False,True]])
Sample output
Mention in comments if any mistakes or errors are found. Thank you.