with Ada.Text_IO ; use Ada.Text_IO ;
with Ada.Integer_Text_IO ; use Ada.Integer_Text_IO ;
procedure Q5J is
-- Definindo tipos para as matrizes
type Matrix is array ( 1 .. 10 , 1 .. 10 ) of Integer; -- Máximo de 10x10
A, B, C : Matrix; -- Matrizes A, B e C
M, N, P, Q : Integer; -- Dimensões das matrizes
begin
-- Entrada da matriz A
Put( "Digite o número de linhas da matriz A (m): " ) ;
Get( M) ;
Put( "Digite o número de colunas da matriz A (n): " ) ;
Get( N) ;
Put_Line( "Digite os elementos da matriz A:" ) ;
for I in 1 .. M loop
for J in 1 .. N loop
Put( "Elemento (" & Integer'Image( I) & ", " & Integer'Image( J) & "): " ) ;
Get( A( I, J) ) ;
end loop ;
end loop ;
-- Entrada da matriz B
Put( "Digite o número de linhas da matriz B (p): " ) ;
Get( P) ;
Put( "Digite o número de colunas da matriz B (q): " ) ;
Get( Q) ;
Put_Line( "Digite os elementos da matriz B:" ) ;
for I in 1 .. P loop
for J in 1 .. Q loop
Put( "Elemento (" & Integer'Image( I) & ", " & Integer'Image( J) & "): " ) ;
Get( B( I, J) ) ;
end loop ;
end loop ;
-- Verificando se as dimensões são compatíveis para a adição
if M = P and N = Q then
-- Calculando a matriz C = A + B
for I in 1 .. M loop
for J in 1 .. N loop
C( I, J) := A( I, J) + B( I, J) ;
end loop ;
end loop ;
-- Exibindo a matriz C
Put_Line( "A matriz C = A + B é:" ) ;
for I in 1 .. M loop
for J in 1 .. N loop
Put( Integer'Image( C( I, J) ) & " " ) ;
end loop ;
New_Line; -- Nova linha após cada linha da matriz
end loop ;
else
Put_Line( "As matrizes devem possuir o mesmo tamanho para efetuar a adição" ) ;
end if ;
end Q5J;
d2l0aCBBZGEuVGV4dF9JTzsgdXNlIEFkYS5UZXh0X0lPOwp3aXRoIEFkYS5JbnRlZ2VyX1RleHRfSU87IHVzZSBBZGEuSW50ZWdlcl9UZXh0X0lPOwoKcHJvY2VkdXJlIFE1SiBpcwogICAgLS0gRGVmaW5pbm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XRyaXogQjoiKTsKICAgIGZvciBJIGluIDEgLi4gUCBsb29wCiAgICAgICAgZm9yIEogaW4gMSAuLiBRIGxvb3AKICAgICAgICAgICAgUHV0KCJFbGVtZW50byAoIiAmIEludGVnZXInSW1hZ2UoSSkgJiAiLCAiICYgSW50ZWdlcidJbWFnZShKKSAmICIpOiAiKTsKICAgICAgICAgICAgR2V0KEIoSSwgSikpOwogICAgICAgIGVuZCBsb29wOwogICAgZW5kIGxvb3A7CgogICAgLS0gVmVyaWZpY2FuZG8gc2UgYXMgZGltZW5zw7VlcyBzw6NvIGNvbXBhdMOtdmVpcyBwYXJhIGEgYWRpw6fDo28KICAgIGlmIE0gPSBQIGFuZCBOID0gUSB0aGVuCiAgICAgICAgLS0gQ2FsY3VsYW5kbyBhIG1hdHJpeiBDID0gQSArIEIKICAgICAgICBmb3IgSSBpbiAxIC4uIE0gbG9vcAogICAgICAgICAgICBmb3IgSiBpbiAxIC4uIE4gbG9vcAogICAgICAgICAgICAgICAgQyhJLCBKKSA6PSBBKEksIEopICsgQihJLCBKKTsKICAgICAgICAgICAgZW5kIGxvb3A7CiAgICAgICAgZW5kIGxvb3A7CgogICAgICAgIC0tIEV4aWJpbmRvIGEgbWF0cml6IEMKICAgICAgICBQdXRfTGluZSgiQSBtYXRyaXogQyA9IEEgKyBCIMOpOiIpOwogICAgICAgIGZvciBJIGluIDEgLi4gTSBsb29wCiAgICAgICAgICAgIGZvciBKIGluIDEgLi4gTiBsb29wCiAgICAgICAgICAgICAgICBQdXQoSW50ZWdlcidJbWFnZShDKEksIEopKSAmICIgIik7CiAgICAgICAgICAgIGVuZCBsb29wOwogICAgICAgICAgICBOZXdfTGluZTsgIC0tIE5vdmEgbGluaGEgYXDDs3MgY2FkYSBsaW5oYSBkYSBtYXRyaXoKICAgICAgICBlbmQgbG9vcDsKICAgIGVsc2UKICAgICAgICBQdXRfTGluZSgiQXMgbWF0cml6ZXMgZGV2ZW0gcG9zc3VpciBvIG1lc21vIHRhbWFuaG8gcGFyYSBlZmV0dWFyIGEgYWRpw6fDo28iKTsKICAgIGVuZCBpZjsKZW5kIFE1SjsK
stdout
Digite o número de linhas da matriz A (m): Digite o número de colunas da matriz A (n): Digite os elementos da matriz A:
Elemento ( 1, 1): Elemento ( 1, 2): Elemento ( 2, 1): Elemento ( 2, 2): Digite o número de linhas da matriz B (p): Digite o número de colunas da matriz B (q): Digite os elementos da matriz B:
Elemento ( 1, 1): Elemento ( 1, 2): Elemento ( 2, 1): Elemento ( 2, 2): A matriz C = A + B é:
3 0
2 -3