Midterm exam - sub Count() { open

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Midterm exam 1. #This program finds repeated words in an input file and prints the repeated word and the line number on which it was found $i = 0; while (<STDIN>) { $a[$i]=$_; $i++; $hash {$_}++; } foreach $x(keys (%hash)) { if ($hash{$x} != 1) { print("\n$x --- $hash{$x} Times"); $i = 0; foreach(@a) { if($x eq $a[$i]) { $ii = $i + 1; print("\nat line $ii"); } $i++; } } print("\n"); } 2. #write a variance program
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
use strict; use warnings; my @x = " "; my $n = 100; for(my $i=0;$i<$n;$i++) { my $rand = int(rand(10)+10); $x[$i] = $rand; } sub mean() { my $sum = 0; foreach(@x) { $sum = $sum + $_; } $sum = $sum / $n; return $sum; } my $m = mean(); print("Mean :: $m\n"); sub Var() { my $sum = 0; foreach(@x) { $sum = $sum + (($_-$m)*($_-$m)); } $sum = $sum/($n-1); return $sum; } my $var = Var();
Background image of page 2
print("Varience :: $var\n"); sub SD() { my $sd=sqrt($var); return $sd; } my $Sd = SD(); print("Standard Deviation :: $Sd\n"); 3. # Count and Sort command using Command line argument
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sub Count() { open (f1,&quot;&lt;$ARGV[0]&quot;)||die(&quot;Please enter valid file name. ...!!!!\n&quot;); print(&quot;::: Command Count :::\n&quot;); @a = &quot; &quot;; $i = 0; while (&lt;f1&gt;) { $a[$i]=$_; $i++; $hash {$_}++; } foreach $x(keys (%hash)) { if ($hash{$x} != 1) { print(&quot;\n$x --- $hash{$x} Times&quot;); $i = 0; foreach(@a) { if($x eq $a[$i]) { $ii = $i + 1; print(&quot;\nat line $ii&quot;); } $i++; } } print(&quot;\n&quot;); } close(f1); } sub Sort() { open (f1,&quot;&lt;$ARGV[0]&quot;)||die(&quot;Please enter valid file name. ...!!!!\n&quot;); @file = &lt;f1&gt;; @sorted = sort { $a cmp $b } @file; print(&quot;::: Command Sort :::\n&quot;); print @sorted; close(f1); } #open (f1,&quot;&lt;$ARGV[0]&quot;)||die(&quot;Please enter valid file name. ...!!!!\n&quot;); $cmd = $ARGV[1]; if($cmd eq &quot;count&quot;) { Count(); } elsif($cmd eq &quot;sort&quot;) { Sort(); } elsif($cmd eq &quot;both&quot;) { Count(); Sort(); } else{print(&quot;Please enter valid command. ..!!!\n&quot;);}...
View Full Document

Page1 / 5

Midterm exam - sub Count() { open

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online