CSCI 4230
Software Tools
Spring 2000
Suugested Solution to Homework #2

(1)    For example,

#   This program computes statistic using NAS student
#   evaluation forms.
use strict;

my $NUM_QUESTIONS = 9;
my $LOW_GRADE = 1;
my $HIGH_GRADE = 7;

die "Usage: ProcessEvalData.pl inputdatafile.\n" if (@ARGV < 1) ;
open (DATA, $ARGV[0]) || die "Error: input file $ARGV[0] cannot be open.";
$ARGV[0] =~ /\./;
my $outfilename = $` . "out." . $';
open (REPORT, ">$outfilename") || die "Error: output file $ARGV[1] cannot be open.";

my @lines = <DATA>;
close DATA;
#   Print header line.
print REPORT shift @lines;
print REPORT "\nLowest possible rating: $LOW_GRADE, highest possible rating: $HIGH_GRADE.\n";
print REPORT "Total number of questions = $NUM_QUESTIONS.\n";
my $nrec = @lines;
print REPORT "Total number of respondents = $nrec.\n\n";
print REPORT "Raw data:\n";

my @nQ = ();       #  Number of valid repsonses for each question.
my @sumQ = ();     #  Sum of response rating for each question.
my @sumsqQ = ();   #  Square of sum of response rating for each question.
my @respQ = ();    #  Count of individual responses of questions.
my $i;
my $j;
for ($i=0; $i<$NUM_QUESTIONS; $i++) {
    $respQ[$i] = {};
}

#   Compute statistic and print raw data
foreach (@lines) {
    print REPORT;
    chomp;
    my @data = split //;
    for ($i=0; $i<$NUM_QUESTIONS; $i++) {
        if ($LOW_GRADE <= $data[$i] && $data[$i] <= $HIGH_GRADE) {
            $nQ[$i]++;
            $sumQ[$i] += $data[$i];
            $sumsqQ[$i] += $data[$i] * $data[$i];
            $respQ[$i]{$data[$i]}++;
        }
    }
}

#   Print statistics
print REPORT "\nStatistics:\n\n";
print REPORT "Item                  ";
for ($i=1; $i<=$NUM_QUESTIONS; $i++) {
   print REPORT "Q$i   ";
}
print REPORT "mean\n";
print REPORT "========================================================================\n";

print REPORT "Number              ";
my $tempmean =0;
for ($i=0; $i<$NUM_QUESTIONS; $i++) {
   printf REPORT"%4d ", $nQ[$i];
   $tempmean += $nQ[$i];
}
printf REPORT "  %2.1f \n", $tempmean/$NUM_QUESTIONS;

print REPORT "Mean                ";
$tempmean =0;
for ($i=0; $i<$NUM_QUESTIONS; $i++) {
   my $mn = $sumQ[$i] / $nQ[$i];
   printf REPORT "%1.2f ", $mn;
   $tempmean += $mn;
}
printf REPORT "  %1.2f \n", $tempmean/$NUM_QUESTIONS;

print REPORT "Standard Deviation  ";
$tempmean =0;
for ($i=0; $i<$NUM_QUESTIONS; $i++) {
   my $sd = sqrt (($sumsqQ[$i]-$sumQ[$i]*$sumQ[$i]/$nQ[$i])/($nQ[$i]-1));
   printf REPORT "%1.2f ", $sd;
   $tempmean += $sd;
}
printf REPORT "  %1.2f \n", $tempmean/$NUM_QUESTIONS;
printf REPORT "\n\n";

print REPORT "Response  ";
for ($i=1; $i<=$NUM_QUESTIONS; $i++) {
   print REPORT "Q$i   ";
}
print REPORT "Total\n";
print REPORT "=============================================================\n";

for ($i= $HIGH_GRADE; $i>=$LOW_GRADE; $i--) {
    printf REPORT "$i       ";
    my $tempsum = 0;
    for ($j=0; $j<$NUM_QUESTIONS; $j++) {
        printf REPORT "%4d ", $respQ[$j]{$i};
        $tempsum += $respQ[$j]{$i};
    }
    printf REPORT "   %4d\n", $tempsum;
}

close REPORT;
exit 0;