Just one thing with your program, as I recall with
standard deviations you should have the average
calculated over the whole data range, not just the elements
that have come before.
Here is what I would do:
my @data;
my $avg;
while (<IN>) {
chomp;
my $tmp = [split /:/];
$avg += $tmp->[1];
push @data, $tmp;
}
$avg /= @data;
my $std = 0;
for (@data) { $std += abs($_->[1] - $avg) }
$std /= @data;
open(OUT, ">pa5c.dat") || die ("Can't open file $!\n");
for (@data) {
my $grade;
my $dev = ($_->[1] - $avg) / $std;
if ($dev >= 1) { $grade = 'A' }
elsif ($dev >= (1/3)) { $grade = 'B' }
elsif ($dev >= (-1/3)) { $grade = 'C' }
elsif ($dev > -1) { $grade = 'D' }
else { $grade = 'E' }
print OUT join(':', $_->[0], $_->[1], $grade) . "\n";
}
close OUT;
Which produces records like
Smith:70:C
Jones:74:B
Rider:80:A
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|