import random
n = int(input('Input size of matrix: '))
matrix = [[random.randint(-5, 5) for i in range(n)] for j in range(n)]
for row in matrix:
print(' '.join([str(elem) for elem in row]))
sums = [sum(row) for row in matrix]
print('Sums of elements by rows:', sums)
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
import random
n = int(input('Input size of matrix: '))
matrix = [[random.randint(-5, 5) for i in range(n)] for j in range(n)]
for row in matrix:
print(' '.join([str(elem) for elem in row]))
sums = [sum(row) for row in matrix]
print('Sums of elements by rows:', sums)