Solutions to the quiz due in class on Monday, September 27, 2004 1. Here's a solution to Exercise 1. #!/bin/csh set divisor = $argv[1] foreach num ( $argv ) @ remainder = $num % $divisor if ( $remainder == 0 ) echo $num end Here's an implementation of the 'powers' command using a nested loop and a flag 'f' to exit the inner loop when appropriate. #!/bin/csh set d = $argv[1] foreach i ( $argv ) set n = $i set f = 1 while ( $f ) @ r = $n % $d if ( $r > 0 ) then set f = 0 else @ n = $n / $d endif if ( $n == 1 ) then echo $i set f = 0 endif end end Here's the 'primes' command also using a nested loop but this time I use the 'break' command to exit the inner loop. I could have used a flag as I did in the case of 'powers' but I wanted to show you an alternative method for exiting a loop. #!/bin/csh foreach n ( $argv ) @ d = $n / 2 while ( $d > 1 ) @ r = $n % $d if ( $r == 0 ) break @ d -- end if ( $d == 1 ) echo $n end 2. A solution to Exercise 2 #!/bin/csh set one = ( `cat $argv[1]` ) set two = ( `cat $argv[2]` ) if ( $#one != $#two ) then echo "The two columns of numbers must be of equal length" exit endif set i = 1 while ( $i <= $#one ) @ sum = $one[$i] + $two[$i] echo $sum @ i ++ end