How to find the largest value in an array? [duplicate]

0

Well, I have a 3x4 array and I want to know how to develop a python code to get the highest value from this array.

My code done so far was this:

LINHAS = 3
COLUNAS = 4
LINHA_
MatrizM = []

for i in range(LINHAS):
    MatrizM.append([])
    for j in range(COLUNAS):
        n = int(input())
        MatrizM[i].append(n)

print('=== MATRIZ M ===')
for i in range(LINHAS):
    for j in range(COLUNAS):
        print(MatrizM[i][j], end='\t')
    print()

What I do not know is how I can develop a code that shows me the highest value of this array

    
asked by anonymous 06.06.2018 / 18:15

1 answer

0

You can create an IF condition within the repeated, it will look like this:

controle = 0 (0 ou algum valor da matriz)
for(...)
.
if n > controle:
controle = n
.
.

Let's assume that the first value is 10, the IF will test if 10 is greater than 0, as will be TRUE the control variable receives 10. Let's assume that the next value is 9, when testing again the result will be FALSE because 9 is less than 10, then the value will not overlap.

As the friend suggested in the comment, your matrix may have only negative values, to get around this, the control variable needs to get some value from the table instead of 0. In this way there will be no problems with positive and negative numbers

    
06.06.2018 / 18:26