< Day Day Up > |
Now that you've learned about subroutines, you should begin to see the benefits of encapsulating code in self-contained subroutines. They provide code that can be easily reused. In this exercise, three subs provide some analysis on groups of numbers . Just to refresh your memory from school, the mean, also called an arithmetic mean or average, of a set of numbers is simply the sum of all the numbers in the set divided by how many numbers there are in the set. The median is the number that would be in the middle if you were to sort the set numerically ; with an even number of elements, the median is the average of the two numbers that would be in the middle. The standard deviation gives an idea of how "bunched" the numbers are around the mean. A high standard deviation that means the numbers are widely distributed; a small one means they're bunched tightly around the average. In many sets of numbers commonly found in nature, the mean, plus or minus the standard deviation, represents about 68 percent of the set of the numbers; plus or minus two standard deviations, 95 percent of the set of numbers. Here is a program that finds the mean, median, and standard deviation for a set of numbers that the user types at the keyboard. Using your text editor, type the program from Listing 8.1 and save it as Stats . Again, do not type line numbers, and if possible, be sure to make the program executable according to the instructions you learned in Hour 1, "Introduction to the Perl Language." When you're all done, try running the program by typing the following at a command line: stats or, if your system cannot make the program executable, perl Stats Listing 8.1. Complete Listing for Stats Program1: #!/usr/bin/perl -w 2: 3: use strict; 4: sub mean { 5: my(@data) = @_; 6: my $sum; 7: foreach(@data) { 8: $sum += $_; 9: } 10: return($sum / @data); 11: } 12: sub median { 13: my(@data)=sort { $a <=> $b} @_; 14: if (scalar(@data) % 2) { 15: return($data[@data / 2]); 16: } else { 17: my($upper, $lower); 18: $lower=$data[@data / 2]; 19: $upper=$data[@data / 2 - 1]; 20: return(mean($lower, $upper)); 21: } 22: } 23: sub std_dev { 24: my(@data)=@_; 25: my($sq_dev_sum, $avg)=(0,0); 26: 27: $avg = mean(@data); 28: foreach my $elem (@data) { 29: $sq_dev_sum += ($avg - $elem) **2; 30: } 31: return(sqrt($sq_dev_sum / ( @data - 1 ))); 32: } 33: my($data, @dataset); 34: print "Please enter data, separated by commas: "; 35: $data = <STDIN>; chomp $data; 36: @dataset = split(/[\s,]+/, $data); 37: 38: print "Median: ", median(@dataset), "\n"; 39: print "Mean: ", mean(@dataset), "\n"; 40: print "Standard Dev.: ", std_dev(@dataset), "\n";
Listing 8.2 shows a sample of the statistics program's output. Listing 8.2. Sample Output from Stats Please enter data, separated by commas: 14.5,6,8,9,10,34 Median: 9.5 Mean: 13.5833333333333 Standard Dev.: 10.3943093405318 |
< Day Day Up > |