#!/usr/bin/env perl
use strict;

my @A = (
    [2, 3, 5, 2],
    [7, -8, 12, 4],
    [4, 5, 0, 7]
);

my @B = (
    [8, 7, 5, 8],
    [16, -4, -10, 7],
    [0, -1, 7, 0]
);

my @C = secti_matice(\@A, \@B);
tiskni_matici(\@C);



sub secti_matice {
    my($r_A, $r_B) = @_;
    my @C;

    #kontrola, zda mohou byt matice secteny
    return if @$r_A != @$r_B;
    return if scalar @{$r_A->[0]} != scalar @{$r_B->[0]};

    #ziskani poctu radku a sloupcu - podle predchozi kontroly jsou tyto pocty zarucene pro obe matice stejne
    my $radku = @$r_A;
    my $sloupcu = @{$r_A->[0]};

    #vse OK, secteme
    for(my $i=0; $i<$radku; $i++){
        for(my $j; $j<$sloupcu; $j++){
            $C[$i][$j] = $A[$i][$j] + $B[$i][$j];
        }
    }

    return @C;
}

sub tiskni_matici {
    my($r_matice) = @_;

    my $radku = @$r_matice;
    my $sloupcu = @{$r_matice->[0]};

    for(my $i=0; $i<$radku; $i++){
        for(my $j; $j<$sloupcu; $j++){
            printf "%8.1f ", $$r_matice[$i][$j];
        }
        print "\n";
    }
}