Escribir unha subrutina que reciba como argumentos unha matriz
de números enteiros e orde 4x4 e dous enteiros
. A subrutina debe calcular a matriz
de orde 3x3 centrada no elemento
. Cando o cálculo de
implica acceder a elementos
que non existen, haberá que poñer 0's. Por exemplo, dada a matriz
:
MELLORA DO PROGRAMA: Facer que o programa, no canto de inicializar a matriz coa sentencia data anterior, lea a matriz
dun ficheiro datos.dat, co seguinte formato:
|1 2 3 4 | |5 6 7 8 | |9 8 7 6 | |5 4 3 2 |Ademais, debe escribir a matriz
i = 2, j = 3 |2 3 4 | |6 7 8 | |8 7 6 |AVISO: Incluir como comentario nas primeiras liñas do programa o nome completo e o grupo (A, B)
SOLUCIÓN:
program matriz
integer a(4, 4), b(3, 3)
i = indice("i"); j = indice("j")
print *, i, j
call lee_matriz(a)
call calcula_b(a, b, i, j)
call escribe_matriz(b, i, j)
stop
end
cccccccccccccccccccccccccccccc
function indice(c)
character c
1 print *, "Introduza o índice ", c, " (1 <= ", c, " <= 4)"
read *, indice
if(indice.lt.1.or.indice.gt.4) goto 1
return
end
ccccccccccccccccccccccccccccccc
subroutine calcula_b(a, b, i, j)
integer a(4, 4), b(3, 3)
do 1 k = -1, 1
do 1 l = -1, 1
if(i+k.ge.1.and.i+k.le.4.and.j+l.ge.1.and.j+l.le.4) then
b(k + 2, l + 2) = a(i + k, j + l)
else
b(k + 2, l + 2) = 0
endif
1 continue
do 2 k = 1, 3
2 print *, (b(k, l), l = 1, 3)
return
end
cccccccccccccccccccccccccccccc
subroutine lee_matriz(a)
integer a(4, 4)
open(1, file = "datos.dat", status = "old", err = 1)
do 2 i = 1, 4
read (1, 3) (a(i, j), j = 1, 4)
2 continue
3 format("|", 4(i1, 1x), "|")
close(1)
return
1 print *, "Erro en open (read)"
stop
end
ccccccccccccccccccccccccccccccc
subroutine escribe_matriz(b, i, j)
integer b(3, 3)
open(1, file = "resultados.dat", status = "new", err = 1)
write (1, *) "i = ", i, ", j = ", j
do 2 k = 1, 3
write (1, 3) (b(k, l), l = 1, 3)
2 continue
3 format("|", i1, 1x, i1, 1x, i1, "|")
close(1)
return
1 print *, "Erro en open (write)"
stop
end