COMP 364 - Quiz 1

Instructions: This quiz is open notes and open textbook, but you should not use Perl on your computer to test or write any programs. There are five questions, 1 on linux, 2 program-interpretation questions, and 2 program-writing questions. Partial credit will be given, so write something for every problem, even if you're not sure you have it quite right or have fully solved the problem. For the program-writing questions, do not use arrays, as they are not covered on this quiz.

  1. Backing up a directory (10 points)

    Consider the directory listing below: [perkins][lab2-2][~/Quiz1] ls -l total 12 -rw------- 1 perkins 17260 25 Feb 12 10:28 Apple.txt -r-------- 1 perkins 17260 25 Feb 12 10:28 Banana.txt ---x------ 1 perkins 17260 47 Feb 12 10:29 Chew.pl [perkins][lab2-2][~/Quiz1] This describes a directory called "Quiz1", which is located in my (perkins's) home directory, and contains three files: "Apple.txt", "Banana.txt", and "Chew.pl". Suppose you are perkins and have a terminal open in the "Quiz1" directory. Describe a sequence of terminal commands that would create a complete copy of the "Quiz1" directory in a new directory called "Quiz1Backup". This backup directory should contain the same three files with the same permissions, and should also be located in the home directory. The "Quiz1" directory should be the same after the backup, though you may change it (especially the permissions) as needed in order to perform the backup, as long the permissions are set back to their original values afterward. So, in the end, you should have two directories with identical contents, as shown below.

    [perkins][lab2-2][~/Quiz1] ls -l
    total 12
    -rw------- 1 perkins 17260 25 Feb 12 10:28 Apple.txt
    -r-------- 1 perkins 17260 25 Feb 12 10:28 Banana.txt
    ---x------ 1 perkins 17260 47 Feb 12 10:29 Chew.pl
    [perkins][lab2-2][~/Quiz1] cd ..
    [perkins][lab2-2][~] cd Quiz1Backup
    [perkins][lab2-2][~/Quiz1Backup] ls -l
    total 12
    -rw------- 1 perkins 17260 25 Feb 12 10:32 Apple.txt
    -r-------- 1 perkins 17260 25 Feb 12 10:32 Banana.txt
    ---x------ 1 perkins 17260 47 Feb 12 10:32 Chew.pl
    [perkins][lab2-2][~/Quiz1Backup] 
    
  2. Program interpretation (10 points)

    Describe what happens if you run the following Perl program:

    $Count = 1;
    while ($Count < 10) {
      print "$Count\n";
      $Count += $Count;
      $Count = $Count % 10;
    }
    
  3. Program interpretation (10 points)

    Describe what happens if you run the following Perl program, entering 2 as input? 3 as input? 4 as input? Describe what the program does in general, with a number N as input?

    print "Enter a number: ";
    $Num = <>;
    chomp $Num;
    for ($Count=1; $Count<=($Num**2); $Count++) {
      if ($Count % 2) {
        print "X";
      } else {
        print "O";
      }
      if (($Count % $Num)==0) {
        print "\n";
      }
    }
    
  4. Ordering three strings (10 points)

    Write a Perl program that takes three strings as input and prints them out in increasing ASCII-betical order. (The same ordering used by the string comparison operations.) Write the program without using arrays and the "sort" operation, which we only covered in later classes. For example, runs of your program might look like:

    [perkins][lab2-2][~/Quiz1] perl ThreeSort.pl
    Enter string 1: apple
    Enter string 2: banana
    Enter string 3: grape
    Sorted ASCII-betically: apple banana grape
    [perkins][lab2-2][~/Quiz1] perl ThreeSort.pl
    Enter string 1: grape
    Enter string 2: apple
    Enter string 3: banana
    Sorted ASCII-betically: apple banana grape
    [perkins][lab2-2][~/Quiz1] perl ThreeSort.pl
    Enter string 1: banana
    Enter string 2: Banana
    Enter string 3: Apricot
    Sorted ASCII-betically: Apricot Banana banana
    [perkins][lab2-2][~/Quiz1]
    
  5. Mean and standard deviation (10 points)

    If x1, x2, ... , xN are a set of N numbers, recall that the (arithmetic) mean of the numbers is:

    M = (x1+x2+...+xN)/N

    and the standard deviation is

    S = sqrt((x12+x22+...+xN2)/N - M2)

    Write a Perl program that asks the user for how many numbers he would like to input, and then takes each of those numbers as input. After all the numbers have been input, your program should print out the mean and standard deviation of the numbers. Remember, this quiz doesn't cover arrays, so please write your program without the use of arrays. An example run of your program might look like:

    [perkins][lab2-2][~/Quiz1] perl MeanStd.pl
    How many numbers would you like to enter? 4
    Enter number 1: 1     
    Enter number 2: 5
    Enter number 3: 2
    Enter number 4: 3
    The mean is 2.75 and the standard deviation is 1.4790199457749.
    [perkins][lab2-2][~/Quiz1]