smasher40
February 2nd, 2012, 12:45 AM
Hi, i'm trying to make this recursive way of suming all elements of an array, but my code has an error, can you guys help me out finding what's wrong.
here is my code:
#!/usr/bin/perl -w
use strict;
my $num;
my @nums;
my $soma;
printf ("# THE SCRIP WILL CALCULATE RECURSIVELY THE SUM OF THE ELEMENTS ON AN ARRAY # \n");
print "================================================== ========================\n\n";
print "\n INSERT INT NUMBERS,TO FINISH JUST ENTER 0 - ZERO \n";
do
{
print " Insert a INT number --> ";
chomp ($num = <STDIN>);
push (@nums,$num);
print "\n\n";
}while ($num !=0);
printf ("\n\nThe sum of the elements of the array is ---> ".&soma(@nums)."\n\n");
sub soma($)
{
my @elements = @_;
my $num_elements = 0;
my ($nums, $soma);
chomp (@elements);
foreach $nums(@elements)
{
$num_elements = $num_elements +1;
}
#print "@elements";
#print "\n\n";
#print "total de elementos do array --> $num_elements";
$soma = 0;
if ($num_elements > 0)
{
foreach $nums(@elements)
{
print "$nums\n";
return soma($soma = $soma + $nums,$num_elements-1);
}
}
else
{
return 1;
}
}
OUTPUT
infinitive number 1
here is my code:
#!/usr/bin/perl -w
use strict;
my $num;
my @nums;
my $soma;
printf ("# THE SCRIP WILL CALCULATE RECURSIVELY THE SUM OF THE ELEMENTS ON AN ARRAY # \n");
print "================================================== ========================\n\n";
print "\n INSERT INT NUMBERS,TO FINISH JUST ENTER 0 - ZERO \n";
do
{
print " Insert a INT number --> ";
chomp ($num = <STDIN>);
push (@nums,$num);
print "\n\n";
}while ($num !=0);
printf ("\n\nThe sum of the elements of the array is ---> ".&soma(@nums)."\n\n");
sub soma($)
{
my @elements = @_;
my $num_elements = 0;
my ($nums, $soma);
chomp (@elements);
foreach $nums(@elements)
{
$num_elements = $num_elements +1;
}
#print "@elements";
#print "\n\n";
#print "total de elementos do array --> $num_elements";
$soma = 0;
if ($num_elements > 0)
{
foreach $nums(@elements)
{
print "$nums\n";
return soma($soma = $soma + $nums,$num_elements-1);
}
}
else
{
return 1;
}
}
OUTPUT
infinitive number 1