LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2014, 04:32 PM   #1
thras0
LQ Newbie
 
Registered: Jul 2012
Location: Romania
Distribution: CentOS , Redhat , Ubuntu , Linux Mint
Posts: 6

Rep: Reputation: Disabled
Question incorrect output from perl subroutine


Hello there , I have been playing with Perl and found myself in a bit of a situation. I did read about it on different sites (perlmeme , perlmonks , etc) but could not figured what its wrong with my code.

request :

Quote:
Extra credit exercise: write a subroutine, called &above_average, that takes a
list of numbers and returns the ones that are above the average (mean). (Hint: make
another subroutine that calculates the average by dividing the total by the number
of items.) Try your subroutine in this test program.
my failed attempt :

Code:
#!/usr/bin/perl

use strict;

my @fred = above_average(1 .. 10);
print "\@fred is @fred. \n";
print "shoud be 6 7 8 9 10. \n";

my @barney = above_average(100, 1..10);
print "\@barney is @barney\n";
print "(Should be just 100)\n";

my $sum_fred = sum(@fred);
print "sum of fred is $sum_fred \n";

my $sum_barney = sum(@barney);
print "sum of barney is $sum_barney \n";

sub sum {
    my $sum = 0;   
    foreach (@_) { 
       $sum += $_ ;
    }
    $sum;
}

sub above_average {
    my $average = ( (&sum(@_)) / ($#_));
    my @other = "";
    foreach (@_) {
       if ($average < $_) { push (@other, $_)};
    }
    @other;
}

the output i get :

Code:
@fred is  7 8 9 10. 
shoud be 6 7 8 9 10. 
@barney is  100
(Should be just 100)
sum of fred is 34 
sum of barney is 100

Sum of fred should be 55 and sum of barney should be 77.5 [77 if its truncated to the lower integer I think]. I might be to tired right now to see my mistake, so if anyone spots it please let me know.

Thanks in advance.

PS : Its not a homework , its just an exercise I'm trying to do from "Learning Perl", I check the appendix for the answer but I wanted to try it this way too, if possible.
 
Old 10-07-2014, 05:44 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
1) "$#_" is returning N-1, so "9" for your first call and "10" for your second. If you use "@_" instead you will get the correct number.

2) Why do you think sum of fred should be 55 and sum of barney should be 77.5? Sum of fred should be 40 (6+7+8+9+10) and sum of barney should be 100.
 
1 members found this post helpful.
Old 10-08-2014, 01:16 AM   #3
thras0
LQ Newbie
 
Registered: Jul 2012
Location: Romania
Distribution: CentOS , Redhat , Ubuntu , Linux Mint
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
1) "$#_" is returning N-1, so "9" for your first call and "10" for your second. If you use "@_" instead you will get the correct number.

2) Why do you think sum of fred should be 55 and sum of barney should be 77.5? Sum of fred should be 40 (6+7+8+9+10) and sum of barney should be 100.
First of all thank you for your help.

You are correct about the sum being 40, in my mind I added all the numbers ( 1 to 10 , hence the 55 ), but it was actually only the sum of the above average elements that I calculated in that subroutine. I just couldn't see it. Silly me

Thanks and all the best
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how calling subroutine in Perl shifter Programming 1 08-19-2010 11:12 AM
help with perl subroutine kdelover Programming 10 10-28-2009 06:56 AM
Perl Subroutine help abs_77 Programming 4 05-06-2009 02:07 AM
perl subroutine call shifty_eyes Programming 2 10-01-2005 08:43 PM
Perl Subroutine problem sykkn Programming 0 02-20-2002 09:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration