#!/usr/bin/env perl
use strict;

my @A = (
    [ 2,  3,  5, 2],
    [ 7, -8, 12, 4],
    [ 4,  5,  0, 7],
    [ 4,  5,  0, 1],
    [ 4,  0,  7, 1]
);

my @B = (
    [  8,  7,   5,   8,  15],
    [ 16, -4, -10,   5,  77],
    [  0, -1,   7,   0,  12],
    [  4,  5,   0,   7,  14]
);

my @C = vynasob_matice(\@A, \@B);
tiskni_matici(\@C);



sub vynasob_matice {
    my($r_A, $r_B) = @_;
    my @C;
    my $prvek = 0;

    #ziskani poctu radku a sloupcu matic A a B
    my $A_radku = @$r_A;
    my $A_sloupcu = @{$r_A->[0]};
    my $B_radku = @$r_B;
    my $B_sloupcu = @{$r_B->[0]};

    #kontrola, zda mohou byt matice vynasobeny
    return if $A_sloupcu != $B_radku;

    #vse OK, vynasobime
    for(my $i=0; $i<$A_radku; $i++){
        for(my $j; $j<$B_sloupcu; $j++){
            for(my $k=0; $k<$A_sloupcu; $k++){
                $prvek += $r_A->[$i][$k] * $r_B->[$k][$j];
            }
            $C[$i][$j] = $prvek;
            $prvek = 0;
        }
    }

    return @C;
}

sub tiskni_matici {
    my($r_C) = @_;

    my $radku = @$r_C;
    my $sloupcu = @{$r_C->[0]};

    for(my $i=0; $i<$radku; $i++){
        for(my $j; $j<$sloupcu; $j++){
            printf "%8.1f ", $$r_C[$i][$j];
        }
        print "\n";
    }
}
