\documentclass[10pt]{article}

\usepackage{fullpage}
\usepackage{times} 
\usepackage{mathptm}
\usepackage{epsf}

\textwidth 6.5in
\textheight 9.25in
\topmargin +.15in
\oddsidemargin 0in
\evensidemargin 0in

\def\BeginAnswer{\vspace{2ex}\noindent {\bf Answer}:}
\def\AnswerOn{} % dummy; no effect
\def\AnswerOff{\long\def\BeginAnswer##1\EndAnswer{}}
\def\EndAnswer{}
\long\def\BeginComment#1\EndComment{}
\def\EndComment{}

% Specify either \AnswerOn or \AnswerOff
\AnswerOn

\input{misc}

\begin{document}

\parindent 0pt
\parskip 5pt

\begin{center}
\begin{picture}(432,35)(-18,0)
\thicklines
\put(0,0){\framebox(396,36)}
\thinlines
\put(-2,-1.85){\framebox(400,40)}
\large
\put(3,5){\bf CSE 399/004 Spring 2006}
\put(393,5){\makebox(0,0)[br]{\bf DUE: Mon. 20 February 2006}}
\put(3,31){\makebox(0,0)[tl]{\bf Unix/Linux Skills}}
\put(393,31){\makebox(0,0)[tr]{\bf Homework 6 Solutions}}
\end{picture}
\end{center}
\normalsize

\vspace{.25in}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In this homework, you will need to work on the eniac-l.seas.upenn.edu
server.  If you need help logging in to this machine see the following
webpage: \underline{http://www.seas.upenn.edu/cets/answers/remote.html}
(note: you need to swap eniac-l.seas.upenn.edu for eniac.seas.upenn.edu).

You should write up this assignment using latex (doing the editing in emacs).
In each question, you will be asked to write some code either in perl or
latex.  You should copy the code to the latex source file (after the
appropriate question), adding formatting as necessary.  Note, some 
characters (for example, `\$') will need to be handled specially.

When you are finished with the assignment, attach the latex source file
(which should be compilable using the command `pdflatex') to an email
and send to \emph{mcorliss@cis.upenn.edu}.  The subject of the email should 
be ``cse399 - hw6'' (without the quotes).  The homework is due by the 
beginning of Monday's class.  Total points: 50.

\begin{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

Preliminary.

\begin{enumerate}

\item Change your location to the 'cse399' directory you created in 
homework 1 (or create it again if you deleted it).  Copy the directory
/home1/m/mcorliss/teaching/cse399/hw6 and 
all its subcontents to your current location.  Change your location to
this new directory (hw6).  In this directory, you will find files `hw6.tex'
and `misc.tex'.  Use these files to write up your homework in latex.  Use 
the command `pdflatex' to test your source file and to generate a pdf.

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

$[$30 Points$]$ Perl.

\begin{enumerate}

\item Write a perl program called `n-fact.pl'.  `n-fact.pl' should take
as input a number $n$ and output $n!$ (\ie, $n*(n-1)*(n-2)*...*2*1$).

\BeginAnswer
\begin{verbatim}
#!/usr/bin/perl

$n = @ARGV[0];

if ($n < 0) {
  print "Error: input must be >= 0\n";
  exit 1;
}

for ($result = 1; $n > 0; $n--) {
  $result *= $n;
}

print "$result\n";

exit 0;
\end{verbatim}
\EndAnswer

\item Write a program `sort-count.pl' that reads from standard input
a list of numbers, sorts those numbers, and returns the total number
of occurrences of each number (even if there are 0 occurrences).  If
the largest number is $n$, then `sort-count.pl' will print out the 
occurrences for numbers 0 through $n$.  For example, if the following 
was read from standard input:\\
\\
2\\
1\\
2\\
5\\
2\\
\\
Then `sort-count.pl' would return:\\
\\
1: 1\\
2: 3\\
3: 0\\
4: 0\\
5: 1\\
\\
Your program should *not* use the perl \emph{system} function.\\

\BeginAnswer
\begin{verbatim}
#!/usr/bin/perl

open (FH, "-");
@input = <FH>;

for ($i = 0; $i < scalar(@input); $i++) {
  if (@input[$i] >= 0) {
    @array[@input[$i]]++;
  }
  else {
    print "Each input must be >= 0\n";
    exit 1;
    }
}

for ($i = 0; $i < scalar(@array); $i++) {
  if (@array[$i] == 0) {
    @array[$i] = 0;
  }
  print "$i:@array[$i]\n";
}

exit 0;
\end{verbatim}
\EndAnswer

\item Write the program `get-sim-stat.sh' in perl.  Your program should
*not* use the perl \emph{system} function.\\

\BeginAnswer
\begin{verbatim}
#!/usr/bin/perl

$num = @ARGV[0];
$stat = @ARGV[1];

for ($i = 2; $i < scalar(@ARGV); $i++) {
  open (FH, @ARGV[$i]);
  @input = <FH>;
  $occurrence = 0;
  for ($j = 0; $j < scalar(@input); $j++) {
    if (@input[$j] =~ /^$stat / && ++$occurrence == $num) {
      print "@input[$j]";
      last;
    }
  }
}

exit 0;
\end{verbatim}
\EndAnswer

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

$[$20 Points$]$ Latex.

\begin{enumerate}

\item Write up homework 6 in latex, using the files `hw6.tex' and
`misc.tex'.  You should add a new macro to `misc.tex' that produces
an {\it et at.} whenever you use the command `$\backslash$etal'.

\BeginAnswer
\\
See the file hw6-sol.tex.  To add a new macro, simply add the 
following line to `misc.tex':\\
\begin{verbatim}
\newcommand{\etal}{{\it et al.}} 
\end{verbatim}
\EndAnswer

\item Reproduce the document in `ex.pdf' using latex.

\BeginAnswer
\begin{verbatim}
\documentclass{article}

\date{\small\it May 16, 2000}
\title{Example}
\author{ Marc Corliss}



\begin{document}

\maketitle

\begin{abstract}
Something interesting here ...
\end{abstract}



\section{Introduction}

Something interesting here ...\\
\\
An equation:\\
$g(N,\mu,\beta) = {\frac{{{{\frac{1}{N}}}^
       {1 - {\frac{\beta }{\mu }}}}\,
     ({{{\frac{\beta }{\mu }}})^{\beta }}\,
     {{(\log {\frac{1}{N}})}^
       { \beta -1}}}{
      \Gamma(\beta )}} \ \ \ {\rm for} \  \mu \  > 0 \ {\rm and} \ \beta \geq 1
  \label{loggamma}$\\
\\
A bulleted list:
\begin{itemize}
\item A
\item B
\item C
\end{itemize}

\section{Tables}\label{table}
A table:\\
\\
\begin{tabular}{c || c | c }
$n$ & $S^n$ & $R^n$ \\ \hline
1 & 1 & 1 \\
2 & 1 & 1 \\
3 & 1 & 1 \\
4 & 1 & $\infty$ \\
5 & 1 & 1 \\
6 & 1 & 1 \\
7 & 28 & 1 \\
8 & 2 & 1 \\
9 & 8 & 1 \\
10 & 6 & 1 \\
\end{tabular}

\end{document}
\end{verbatim}
\EndAnswer

\end{enumerate}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\item 

About this assignment.

\begin{enumerate}

\item Approximately, how long did it take you to complete this homework?

\item Would you classify this assignment as easy, straight-forward,
or difficult?

\end{enumerate}



\end{enumerate}

\end{document}

