Quiz 1 Sample Solutions

  1. Backing up a directory

    Here is one sequence of commands that does the trick. An explanation follows.

     
    mkdir ../Quiz1Backup
    chmod u+r Chew.pl
    cp * ../Quiz1Backup
    chmod u-r Chew.pl
    chmod u-r ../Quiz1Backup/Chew.pl
    
    The first command creates the backup directory. The second command adds read permission to the Chew.pl file so we can copy it. The third command copies all files in the current directory to the backup directory. The fourth and fifth commands take away the read permission from Chew.pl in the current directory and the backup directory. Of course, many other sequences of commands would also achieve the task. You may choose, for example, to copy the files one by one, instead of all at once. Another approach would be to make copies of the files in the Quiz1 directory, and then move those copies to the backup directory.

  2. Program interpretation

    You just have to follow this one through, one step at a time. $Count starts at 1. The while loop tests if 1<10, and it is. So it prints 1. Then it adds $Count to itself, making it 1+1=2. Then it computes 2 % 10. This is just 2, because 10 divides 2 zero times, leaving 2 left over. So at the end of the while loop $Count is 2.

    The while loop begins again, because 2<10, so the program prints 2, then adds $Count to itself, making it 4, and sets $Count equal to 4 % 10, which is just 4.

    The while loop begins again, because 4<10, so the program prints 4, then adds $Count to itself, making it 8, and sets $Count equal to 8 % 10, which is just 8.

    The while loop begins again, because 8<10, so the program prints 8, then adds $Count to itself, making it 16, and sets $Count equal to 16 % 10, which is 6. (Because 10 divides 16 one time, with 6 left over.)

    The while loop begins again, because 6<10, so the program prints 6, then adds $Count to itself, making it 12, and sets $Count equal to 12 % 10, which is 2. (Because 10 divides 12 one time, with 2 left over.)

    The while loop begins again, because 2<10, so the program prints 2, then adds $Count to itself, making it 4, and sets $Count equal to 4 % 10, which is just 4.

    At this point you may recognize that the program is in an infinite loop. $Count has changed from 1, to 2, to 4, to 8, to 6, to 2, to 4, etc. So this program never ends, and it prints out the sequence of $Count just mentioned:

    1
    2
    4
    8
    6
    2
    4
    8
    6
    2
    4
    8
    ...
    
  3. Program interpretation

    For input 2, the output is:

    XO
    XO
    
    For input 3, the output is:

    XOX
    OXO
    XOX
    
    For input 4, the output is:

    XOXO
    XOXO
    XOXO
    XOXO
    
    In general, the program outputs a square of alternating X's and O's. The size of the square (height and width) is the input number. To see this, notice that the for loop takes $Count from 1 to the square of the input number. The first if inside the loop causes an X or an O to be printed, alternating. More specifically, when $Count is odd, an X is printed, and when $Count is even, an O is printed. Every $Num characters, a newline character is printed, because we have filled out another row of the square.

  4. Ordering three strings

    As with any programming task, there are many correct answers. Perhaps the most straightforward in this case is to simply read in the three strings, and then check which of the possible orderings is the correct way to print it out.

    print "Enter string 1: ";
    $Str1 = <>;
    chomp $Str1;
    
    print "Enter string 2: ";
    $Str2 = <>;
    chomp $Str2;
    
    print "Enter string 3: ";
    $Str3 = <>;
    chomp $Str3;
    
    if (($Str1 le $Str2) && ($Str2 le $Str3)) {
      print "Sorted ASCII-betically: $Str1 $Str2 $Str3\n";
    } 
    elsif (($Str1 le $Str3) && ($Str3 le $Str2)) {
      print "Sorted ASCII-betically: $Str1 $Str3 $Str2\n";
    } 
    elsif (($Str2 le $Str1) && ($Str1 le $Str3)) {
      print "Sorted ASCII-betically: $Str2 $Str1 $Str3\n";
    } 
    elsif (($Str2 le $Str3) && ($Str3 le $Str1)) {
      print "Sorted ASCII-betically: $Str2 $Str3 $Str1\n";
    } 
    elsif (($Str3 le $Str1) && ($Str1 le $Str2)) {
      print "Sorted ASCII-betically: $Str3 $Str1 $Str2\n";
    } 
    else {
      print "Sorted ASCII-betically: $Str1 $Str3 $Str2\n";
    } 
    
  5. Mean and standard deviation

    Below is one solution. First, we take as input the number of numbers that will be entered. Then we read in the numbers one at a time, computing their sum and the sum of their squares as we go. And at the end we compute mean and standard deviation.

    print "How many numbers would you like to enter? ";
    $N = <>;
    
    $XSum = 0;
    $XSumSqr = 0;
    
    for ($Count=1; $Count<=$N; $Count++) {
      print "Enter number $Count: ";
      $Num = <>;
      $XSum += $Num;
      $XSumSqr += $Num**2;
    }
    print "$XSum $XSumSqr\n";
    
    $Mean = $XSum / $N;
    $Std = sqrt($XSumSqr/$N - $Mean**2);
    print "The mean is $Mean and the standard deviation is $Std.\n";