1
How could I build this matrix without using Random/randit? Then I need to do the function sum element by element, of the first two. The sum shall be settled via written sub-programme for that purpose. This subprogram should receive as parameters only the two input matrices and return the resulting matrix if it is possible to evaluate the sum, or None, if it is not possible. After the return of the subprogram, the main program must display the content of the matrix following the format presented in the matrix construction. If None is returned, the main program should send the message "Cannot add matrices of different dimensions".
#Contrução da Matriz 1 e Matriz 2 - Programa Principal
import random
matriz1 = []
n = int(input("Informe a quantidade de linhas\n da matriz 1:" ))
m = int(input("Informe a quantidade de colunas\n da matriz 1:" ))
for i in range(n):
matriz1.append([])
for j in range(m):
matriz1[i].append(random.randint(0,100))
for i in range(len(matriz1)):
for j in range(len(matriz1[i])):
print(matriz1[i][j], end=" ")
print ("\n")
matriz2 = []
n = int(input("Informe a quantidade de linhas\n da matriz 2:" ))
m = int(input("Informe a quantidade de colunas\n da matriz 2:" ))
for i in range(n):
matriz2.append([])
for j in range(m):
matriz2[i].append(random.randint(0,100))
for i in range(len(matriz2)):
for j in range(len(matriz2[i])):
print(matriz2[i][j], end=" ")
print ("\n")
Do you need to do this with the same result but without using the Random function? That’s the only change to do?
– Miguel
yes.... remove the Random/randit...... and make the sum function that I am not getting... def somarMatrizes(matriz1, matriz2): if Len(matriz1)!= Len(matriz2): print ("It is not possible to add matrices of different dimensions") Return None matriz_soma = [] for i in range(Len(matriz1)): matriz_soma.append([]) for j in range(Len(matriz1)): matriz_soma[i]. append( matriz1[i][j] + matriz2[i][j]) Return matriz_soma print (somarMarines)
– ABSoares
@ Miguel ...... Could you help me with this too? Continuing what I did? def somarMatrizes(matriz1, matriz2): if Len(matriz1)!= Len(matriz2): print ("It is not possible to add matrices of different dimensions") Return None matriz_soma = [] for i in range(Len(matriz1)): matriz_soma.append([]) for j in range(Len(matriz1)): matriz_soma[i]. append( matriz1[i][j] + matriz2[i][j]) Return matriz_soma print (somarMatrizes)
– ABSoares