From what I understand, you want to assemble and display a square array of order 4
, i.e., square matrix "4 x 4"
, in addition to showing the amount of elements that have values greater than 10
.
Well, to solve this question we can use an algorithm that is able to assemble only square order matrices "4 x 4"
or use an algorithm capable of mounting any order matrices "m x n"
(m = rows and n = columns).
As my intention is to show, more comprehensively, the assembly of any matrix, then we can use the following algorithm below. Yeah, this will be able to assemble any order matrices "m x n"
.
As in your case the matrix will always be square, that is m = n
, this implies that the number of rows is equal to the number of columns.
import numpy as np
m = int(input('Digite o número de linhas: '))
n = int(input('Digite o número de colunas: '))
cont = 0
matrizTemp = list()
for c in range(1, m + 1):
linha = list()
for i in range(1, n + 1):
while True:
try:
valor = int(input(f'Digite o {i}º elemento de {c}ª linha: '))
break
except ValueError:
print('\033[31mValor INVÁLIDO! Digite apenas inteiros!\033[m')
if valor > 10:
cont += 1
linha.append(valor)
matrizTemp.append(linha)
# Exibindo a matriz e suas dimensões.
matriz = np.array(matrizTemp)
print(f'\033[32mA matriz gerada é:\n{matriz}')
print(f'A quantidade de elementos maiores que 10 é: {cont}\033[m')
See here the functioning of the algorithm.
Note that when we run the algorithm we receive the following message; Digite o número de linhas
. We must now enter the number of linhas
that we want the matrix to have and press enter. Later we received the second message; Digite o número de colunas
. We must now enter the number of colunas
that we want the matrix to have and press enter.
After that we must insert each of the matrix elements. During the insertion of each matrix element a block if
checks that each inserted value is greater than 10
. If positive, the number of values greater than 10
in the counting variable cont
.
At the end of the insertion of the values the algorithm will mount and display the matrix and also the amount of elements whose values are greater than 10
.
Example
Imagine we want to create a matrix 4 x 4
which has the following values:
1, 2, 4, 6, 9, 10, 11, 3, 12, 16, 5, 7, 8, 14, 15, 20
When we execute this code we receive the message: digite o número de linhas:
. Right now we must enter the number 4 and press enter
. Then we received the second message: Digite o número de colunas:
. At this point we must re-enter the number 4 and press enter
. Then we received the following message: Digite o 1º elemento da 1ª linha:
. At this point we must enter the first element, which in the case of the suggested example is 1. Later we are asked to enter the next value and, at this time, we must enter the next value.
After we have entered all the values suggested in the suggested example the script will assemble a square array of order 4 x 4
with all values typed and will also show the amount of terms that have values greater than 10
.
Later the script will show the following outputs:
A matriz gerada é:
[[ 1 2 4 6]
[ 9 10 11 3]
[12 16 5 7]
[ 8 14 15 20]]
A quantidade de elementos maiores que 10 é: 6
Note that the script will display the assembly of the order square matrix 4 x 4
, in addition to displaying the amount of elements that have values bigger that 10
simple, the range starts at 0. soon: 0, 1, 2, 3, 4. Just you put 3, so it will have 4 houses from 0
– Murilo Melo