Re^2: Averages in bowling
by BrowserUk (Patriarch) on Mar 21, 2006 at 06:11 UTC
|
until (...) { ... } is not Perl. It doesn't work.
Ahem :)
[0] Perl> until( $i == 5 ) { print $i++; };;
Use of uninitialized value in numeric eq (==) at
0
1
2
3
4
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
|
perl -e "until (0) { print "a\n" }
(I figured I'd just Ctrl-C it)
| [reply] [d/l] |
|
|
Try perl -e "until (0) {print qq(a\n)}" and then smack yourself when you notice the two errors that caused you to have problems. :-P
| [reply] [d/l] |
|
|
|
|
|
|
|
Re^2: Averages in bowling
by Andrew_Levenson (Hermit) on Mar 21, 2006 at 16:37 UTC
|
I can't give the entire program now because I do not have it on hand, but for now:
In the program, each value is initialized at 0.
Averages is an array of averages of the scores from the array @scores.
Scores is a multidimensional array consisting of arrays of bowlers, each with an array of scores. Scores are entered by the user.
The main problem is that there are 4 bowlers, but only 3 games, so it makes looping while raising values a little difficult. | [reply] |
|
|
So the average is by bowler? Then you only need two counter variables, not three. If you'd give your vars more meaningful names, it would go much easier. I'm going to try to do this intructively.
-
What is it you want to do? You want to calculate the average score of every bowler.
-
That means you need to loop for every bowler:
for my $bowler (0..3) {
...
}
Better yet, let's not assume we know how many games there are:
for my $bowler (0..$#scores) {
...
}
We will be working with the scores of a specific bowler, so let's create a shortcut:
for my $bowler (0..$#scores) {
my $bscores = $scores[$bowler];
...
}
-
Now, we need to calculate the average score of a bowler:
$averages[$bowler] = int(($bscores->[0] +
$bscores->[1] +
$bscores->[2])/3);
That's ok, but it could be better. We could use a loop to remove the redundancy and to avoid assuming we know how many games there are:
my $sum = 0;
for my $game (0..$#$bscores}) {
$sum += $bscores->[$game];
}
$averages[$bowler] = int($sum/@$bscores);
We can clean it up a bit by using a foreach loop (since $game is only used as an index into @$bscores):
my $sum = 0;
$sum += $_ foreach @$bscores;
$averages[$bowler] = int($sum/@$bscores);
-
All together now:
for my $bowler (0..$#scores) {
my $bscores = $scores[$bowler];
my $sum = 0;
$sum += $_ foreach @$bscores;
$averages[$bowler] = int($sum/@$bscores);
}
-
You could even use List::Util to do the sum:
use List::Util qw( sum );
for my $bowler (0..$#scores) {
my $bscores = $scores[$bowler];
$averages[$bowler] = int(sum(@$bscores)/@$bscores);
}
(Why isn't that module part of core?)
| [reply] [d/l] [select] |
|
|
I fixed it up, and even used your advice to make the program more versatile (i.e. let the user define how many bowlers and games there are as opposed to making it just 4 and 3, respectively.)
In case you're interested, here's the entire {ugly} script.
{my @score; my @ave; my @han;
my $i=0; my $j=0; my $k=0; my $l=0; my $o=0;
my $teamhand;}
print "How many bowlers are there?\n"; chomp(my $n=<>); $n-=1;
print "\nHow many games did the bowlers play?\n"; chomp(my $m=<>); $m-
+=1;
for $i(0..$n){
for $j(0..$m){
$l=$i+1;
$o=$j+1;
print "\nPlease enter bowler $l s score for game $o: ";
chomp($score[$i][$j]=<>);
}
}
for $i(0..$n){
undef $k;
for $j(0..$m){
$k+=$score[$i][$j];
}
push @ave, int($k/($m));
}
for $k(0..$n){
if($ave[$k]<=200){
$han[$k]=int((200-$ave[$k])*(.85));
}
if($ave[$k]>=200){
$han[$k]=0;
}
}
for $i(0..$n){$teamhand+=$han[$i];}
for $i(0..$n){
$j=$i+1;
print "\nBowler $j s Average: $ave[$i] Bowler $j s Handicap: $han
+[$i]\n";
}
print "\nThe team's handicap is: $teamhand\n";
Thanks again! | [reply] [d/l] |
|
|
|
|