I've got an array of integers and i was wondering how you would code for a cumulative frequency array. should i use a for loop or is there a quicker way.
e.g given integers: 1 2 3 4 5 6 7 8 the new array would print: 1 3 6 10 15 21 28 36 thanks!
one way, very inefficient:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw/sum/;
my @arr = (1..10);
my @newarr;
for my $i (0..$#arr) {
$newarr[$i] = sum(@arr[0..$i])
}
looping and summing is much better:
use strict;
use warnings;
my @arr = (1..10);
my @newarr;
my $mid;
for my $i (0..$#arr) {
$mid += $arr[$i];
push(@newarr,$mid);
}