Solver COMPUTE an average
Algebra
->
Average
-> Solver COMPUTE an average
Log On
Algebra: Average
Section
Solvers
Solvers
Lessons
Lessons
Answers archive
Answers
Source code of 'COMPUTE an average'
This Solver (COMPUTE an average)
was created by by
ichudov(507)
:
View Source
,
Show
,
Put on YOUR site
About ichudov
:
I am not a paid tutor, I am the owner of this web site!
==section input Compute average of these numbers (separate them with a comma ","): *[input x=1,2,3]. ==section solution perl $x =~ s/\s+/,/g; $x =~ s/\,+/,/; my @x = split( /,/, $x ); my $count = @x; my $sum = 0; my $expr = "average( "; my $e1 = ""; my $comma = ""; foreach my $x (@x) { $sum += $x; $expr .= "$comma$x"; $e1 .= "+$x"; $comma = ','; } $avg = $sum/@x; $expr .= ") = ($e1)/$count = $sum/$count = $avg"; print "{{{$expr}}}"; ==section output avg ==section check x=5 avg=5 ==section practice perl my $c = randint( 3, 10 ); $comma = ""; for( ; $c >= 0; $c-- ) { $x .= "$comma" . randint( -3, 16 ); $comma = ','; }