#!/usr/bin/perl

@points = (10, 10, 31, 28, 46, 22, 27, 28, 42, 31, 8, 27, 45, 34, 6, 23);

@weights = (1..16);
@normed_weights = normalize(\@weights);   # Divide each weight by 136.

print "Mean weighted score: ",
          weighted_average(\@points, \@normed_weights);

# @norms = normalize(\@array) stores a normalized version of @array
# in @norms.
sub normalize {
    my ($arrayref) = @_;
    my ($total, @result);
    foreach (@$arrayref) { $total += $_ }
    foreach (@$arrayref) { push(@result, $_ / $total) }
    return @result;
}

sub weighted_average {
    my ($arrayref, $weightref) = @_;
    my ($result, $i);
    for ($i = 0; $i < @$arrayref; $i++) {
        $result += $arrayref->[$i] * $weightref->[$i];
    }
    return $result;
}
