#!/usr/bin/perl -w
@labels = qw(a b c d e f g h i j k l m n o);
# Generate 15 different integers from 10 to 99
@nums = ();
while (@nums < 15) {
$nextnum = 10 + int rand 90;
$found = 0;
for ($i=0; $i<@nums; $i++) {
if ($nextnum == $nums[$i]) {
$found = 1;
last;
}
}
if (!$found) {
push @nums, $nextnum;
}
}
# Ask whether the user prefers to sort into increasing
# or decreasing order, and generate the correct list
print "For increasing order type 'i', for decreasing type 'd': ";
chomp( $choice = STDIN> );
while ( ($choice ne 'i') && ($choice ne 'd') ) {
print "$choice is not a valid choice. Please enter 'i' or 'd': ";
chomp( $choice = );
}
@check = sort @nums;
if ( $choice eq 'd' ) {
@check = reverse @check;
}
# print the random numbers with labels a) theough o)
for ($i=0; $i<15; $i++) {
print "$labels[$i]) $nums[$i]\n";
}
# Ask the user to enter the labels in the proper order
# and check against the correct values in @check
@timing = ();
if ( $choice eq 'i' ) {
print "Enter the label of the smallest number: ";
} else {
print "Enter the label of the largest number: ";
}
for ($i=0; $i<15; $i++) {
$start = time();
chomp( $letter = );
$correct = 0;
while ( !$correct ) {
$position = 0;
while ($labels[$position] ne $letter) {
$position++;
if ( $position == 15 ) { last; }
}
if ( $position == 15 ) {
print "$letter is not a valid label. Try again: ";
chomp( $letter = );
}
elsif ( $nums[ $position ] != $check[$i] ) {
print "$letter is not the correct label. Try again: ";
chomp( $letter = );
}
else {
$end = time();
push @timing, $end - $start;
$correct = 1;
print "Correct\n";
if ( $i < 14 ) {
print "Enter the label of the next ";
if ( $choice eq 'i' ) { print "larger"; }
else { print "smaller"; }
print " number: ";
}
}
}
}
# Print times and average
print "\nNumber Time\n";
print "------ ----\n";
$total = 0;
for ($i=0; $i<15; $i++) {
print " $check[$i] $timing[$i]\n";
$total += $timing[$i];
}
$average = $total / 15;
print "Average = $average\n";