Midterm exam - sub Count...

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

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

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

View Full Document
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();
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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sub Count() { open (f1,"<\$ARGV[0]")||die("Please enter valid file name. ...!!!!\n"); print("::: Command Count :::\n"); @a = " "; \$i = 0; while (<f1>) { \$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"); } close(f1); } sub Sort() { open (f1,"<\$ARGV[0]")||die("Please enter valid file name. ...!!!!\n"); @file = <f1>; @sorted = sort { \$a cmp \$b } @file; print("::: Command Sort :::\n"); print @sorted; close(f1); } #open (f1,"<\$ARGV[0]")||die("Please enter valid file name. ...!!!!\n"); \$cmd = \$ARGV[1]; if(\$cmd eq "count") { Count(); } elsif(\$cmd eq "sort") { Sort(); } elsif(\$cmd eq "both") { Count(); Sort(); } else{print("Please enter valid command. ..!!!\n");}...
View Full Document

{[ snackBarMessage ]}

Page1 / 5

Midterm exam - sub Count...

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

View Full Document
Ask a homework question - tutors are online