How do you find the smallest value of a moon matrix?

4

I made this code that makes an Array (At least I think it's doing) and then writes all the values in it. obs: Values are random. I need to find the lowest value in the array, but the logic I've always used gives me a predictable result.

Example: Let's say the invertalo range is given by x and y . (X = 0 and y = 2500). In the code, the value of the smallest variable is always giving the smallest value that can be assumed and y x + 2, that is, in my code the small variable is always resulting in a value of 2. If I set the smallest value of the range to 5, the value of the smaller variable will be 7. (x = 5, x + 2 = 7)

matriz={}
for l=1,20 do
    matriz[l]={}
    for c=1,40 do
        matriz[l][c] = math.random(0,2500)
    end
end
menor = matriz[1][1]--Inicia na menor posição possivel
for i=1,20 do
    for j=1,40 do
        print("O valor na Linha:"..i.." Coluna:"..j.." é "..matriz[i][j])
        if matriz[i][j]<menor then-- essa parte deveria encontrar o meno valor
            menor = matriz[i][j]
        end--Final
    end
end
print("O menor valor encontrado é:"..menor)

This is the code I made, how I'm starting, I do not understand where his error is.

    
asked by anonymous 04.07.2016 / 04:44

1 answer

1

As suggested by @ederwander, a command to prepare the generation of pseudorandom numbers was lacking. Otherwise your logic is correct.

#!/bin/lua

math.randomseed(os.time()) -- faltava isso

matriz = {}

for l=1,20 do
    matriz[l] = {}
    for c = 1,40 do
        matriz[l][c] = math.random(0,2500)
    end
end

menor = matriz[1][1] -- Inicia na menor posição possivel

for i = 1,20 do
    for j = 1,40 do
        print("O valor na Linha:"..i.." Coluna:"..j.." é "..matriz[i][j])
        if matriz[i][j] < menor then -- essa parte deveria encontrar o meno valor
           menor = matriz[i][j]
        end -- Final
    end
end

print("O menor valor encontrado é:" .. menor)
    
09.10.2016 / 04:37