Drill 1 - due July 22, in class or by 5:00 PM in my office (259 Avery).

The first 9 questions are based on last semester's mid-term exam.
Go through the exam first, then complete the drill.  I strongly suggest
running every example on the computer to verify your answers.

1.  Arrays and Subscripts
Let x = [3 5 1 7 6 2 4].   What happens with each of the following, and why?  When
subscripts are Logical (as in h-j) instead of Integer (as in a-g), Matlab behaves
quite differently.  Explain.

a)  x(2)
b)  ii = 2;  x(ii)
c)  x(2:4)
d)  x([2 3 3])
e)  jj = [2 3 3];  x(jj)
f)   x([3 5 1 7 6 2 4])
g)  x(x)
h)  x([true false true])
i)   kk = [true false false true true false false];   x(kk)
j)   y = x > 3;  x(y)

2.  Operations on Arrays
Let a = [0 0 0 4 2 5 7] and b = [0 0 2 1 9 9 2].   What happens with each of the following,
and why?  'while' includes a test, such as:   while a > b   The test yields a logical response,
either true or false, which appears as 1 or 0 if you actually look at the response.  What is
the function 'any' really testing for?  In what situation does a 'while' loop actually perform
its looping statements, and what makes it quit performing them?  As in 2nd grade, write
out how you would add 4257 and 21992.  Carefully show the carries and the sum digits.
Can you see the similarity between this and what problem two on the exam was doing?
What is different about them?

a)  s = a + b
b)  c = s > 9
c)  any([false false false true false])
d)  any([true true true true true])
e)  any([false false false false false])
f)   s = mod( s, 10)
g)  s(1:end-1)
h)  c(2:end)
i)   x = [0 0 2 5 1 4];   y = [0 0 0 1 1 0];  z = x + y
k)  ii = [-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]
l)   ii(1:6) = x + y
m) s(1:6) = s(1:end-1) + c(2:end)

3.  Nested Loops
Run and explain what happens with the following code.

for ii = 1:2
       fprintf('\nii is now %d\n', ii)
       for jj = 1:2
            fprintf('    ii is still %d but jj is %d (tick!)\n', ii, jj)
       end
       fprintf('ii is still %d and jj is surprisingly %d\n', ii, jj)
end

Here is an odometer program.  Explain how it works.  How many times is the fprintf in
the middle executed?  How can you easily compute that number?  Can you rewrite this
so it displays a clock that runs from 1:0:0 up to 12:59:59?

for th= 0:9
    for hu= 0:9
        for tens = 0:9
             for wuns = 0:9
                  for tenths = 0:9
                        fprintf('%1d,%1d%1d%1d.%1d\n', th, hu, tens, wuns, tenths)
                  end
             end
       end
   end
end

4.  Trickier Nested Loops
What happens in the following code?  What if we try negative steps, replacing the first
line with       for a = 5:-1:1           Explain!

for a = 1:5
     fprintf('inner loop should count from a (= %d) to 5\n', a)
     for b = a:5
          fprintf('a is %d and b is %d\n', a, b)
     end
     fprintf('just finished inner loop with a being %d\n', a)
end

5.  Functions
What happens with the following?  Note that linspace, cos and sin are built-in functions.  
Explain how it happens.

a)  linspace(0, 10, 5)
b)  cos(pi/3)^2 + sin(pi/3)^2
c)  for ii = 0, 2, 10
           ii
      end
d)  for ii = linspace(0, 10, 5)
           ii
      end
e)  for ii = linspace(0, 2*pi, 24)
           cos(ii)^2 + sin(ii)^2
      end

Type the following (between the dashes) into a file with the name multiOp.m before
trying next examples.  Can you rewrite the function so that it uses 'if else' instead of
'switch'?
---------------------
function x = multiOp(a, b, op)
switch op
     case '+'
          x = a + b;
     case '-'
          x = a - b;
     case '*'
          x = a * b;
     case '/'
          x = a / b;
     otherwise
          x = 0;
end
----------------------
f)   multiOp(25, 35, '+')
g)  x = multiOp(25, 35, '+')
h)  y = multiOp(x, 2, '/')
i)   z = multiOp(multiOp(25, 35,'+'), 2, '/')

6.  Literals and Variables
Let f = ['%d is odd \n'; '%d is even\n'], being very careful that each line is the same length.
What does 'whos' tell you about f?  What happens with the following.  Explain.

a)  f(1, 1:end)
b)  f(1, :)
c)  f(2, :)
d)  fprintf('%d is odd\n', 25)
e)  fprintf(f(1,:), 25)
f)   fprintf(f(2,:), 26)
g)  mod(25,2)+1
h)  mod(26,2)+1
i)   fprintf(f(mod(25,2)+1,:), 25)
j)   fprintf(f(mod(26,2)+1,:), 26)

7.  While and For Loops
'while' loops can do anything that 'for' loops can do, and even some things 'for' loops can not
do.  'for' loops automatically build in a lot of stuff that you may never see, but which must be
explicitly spelled out for a 'while' loop.  What happens with the following.  Explain.

ii = 1;
while ii < 5
     disp(ii)
end

Fix it as follows:

ii = 1;
while ii < 5
     disp(ii)
     ii = ii + 1;
end

Now rewrite this a as 'for' loop.  Is the opening 'ii = 1' needed?  Explain.

8.  Redundant If Elseif Constructions
For each 'elseif' line, explain exactly what values g can possible be as the line is
entered.  Remember that everything is nested, and once the test becomes true, the 
resulting 'disp' is the ONLY one that will be performed.  The first one is done for you.
Compare your answer for the final 'elseif' with what that 'elseif' tests!  Would the 'input' 
accept a grade outside 0-100?

g = input('Enter grade from 0 to 100: ');
if g > 90 && g < = 100
      disp('Aced')
elseif g > 80 && g <= 90     % answer:  g could be <= 90 or > 100 since 91-100 is caught by 'if'
      disp('B - ok')
elseif g <= 60 && g >= 0
      disp('Failed')
elseif g > 60 && g <= 70
      disp('Dangit!')
elseif g > 70 && g <= 80
      disp('C - not bad')
elseif g < 0 || g > 100
      disp('not a grade')
end

9.  Short Circuit Logic
The || form of 'or' and && form of 'and' allow short circuiting.  This does not mean short
circuiting always occurs with them!  To the contrary, the right hand side is short circuited
only if the correct answer can be determined by evaluating only the left side.   Answer the
following.

a)  compare    3>2 && 5>2    and    3>2 && 2>5    Does answer depend on the right side?
b)  compare    3<2 && 5>2    and    3<2 && 2>5    Does answer depend on the right side?
c)  compare    3>2 || 5>2    and    3>2 || 2>5    Does answer depend on the right side?
d)  compare    3<2 || 5>2    and    3<2 || 2>5    Does answer depend on the right side?

Recall the 'fprintf' is really a function and actually returns the number of characters printed.
Explain when and why the message 'happy' appears and does not appear.

e)  x = fprintf('happy')
f)  compare     3>2 && x>2                               and      3>2 &&  2>x
g) compare     3>2 && fprintf('happy')>2       and      3>2 && 2>fprintf('happy')
h) compare     3<2 && x>2                              and      3<2 && 2>x
i)   compare    3<2 && fprintf('happy')>2       and      3<2 && 2>fprintf('happy')

10.  (FYI) Please note that topics not represented on last semester's first midterm include 
the following:

a)  Data representation (how different types are stored in Matlab)
b)  Infix, Prefix, and Postfix notations for binary operators
c)  Precedence and associativity of operators
d)  Three uses for files by Matlab
e)  Extensive work on nested if-then-else structures
f )  User defined functions
g)  others?

Any or all of these may be included on the midterm this summer!