% alex gerbessiots Exam2 Spring 2015
% from the MATLAB command window run
% clear;clc
% e2101s15
%
% follow instructions
% Px means Problem x  ; PSy  means Problem Set (with Solutions) y
%
% P1 is similar to P1 of PS5
% P2 is P4 of PS5
% P3 is from last year's exam and also variation of the tangle*.m M-files and P5 of PS6
% P4 is from P1 from PS 6
% P5 is from last year's exam modified and also class code
% P6 is  variation of code done in class and Sub7 code in particular p10_*, p11_*, p13_*
% P7 is similar to P1 of PS7 and variation of a last year's exam problem
% P8 is one case (c) of P9 of PS7 slightly changed; also P4 of PS6
% P9 is one case (b) of P9 of PS7; also P4 of PS6

function e2101s15()

fprintf('1.  *******************  Problem 1. (25 points)\n')
      ex2p1;
%     input('Press ENTER to Continue');
%
%
fprintf('2.  *******************  Problem 2. (25 points)\n')
      ex2p2;
%     input('Press ENTER to Continue');
%
%
fprintf('3.  *******************  Problem 3. (25 points)\n')
      ex2p3;
%     input('Press ENTER to Continue');
%
%
fprintf('4.  *******************  Problem 4. (25 points) \n')
      ex2p4;
%     input('Press ENTER to Continue');
%
%
fprintf('5.  *******************  Problem 5. (25 points)\n')
      ex2p5;
%     input('Press ENTER to Continue');
%
%
fprintf('6.  *******************  Problem 6. (25 points)\n')
      ex2p6;fprintf('\n');
%      input('Press ENTER to Continue');
%
%
fprintf('7.  *******************  Problem 7. (25 points)\n')
      ex2p7;
      fprintf('>>|End of output is on the left of the bar ...\n');
%      input('Press ENTER to Continue');
%
%
fprintf('8.  *******************  Problem 8. (25 points)\n')
      ex2p8;
%      input('Press ENTER to Continue');
%
%
fprintf('9.  *******************  Problem 9. (25 points)\n')
      ex2p9;
%      input('Press ENTER to Continue');
%
%
fprintf('10. *******************  Problem 10.(25 points)\n')
      ex2p10;
%      input('Press ENTER to Continue');
%
%
end

%%%%%
%%%%%
  function ex2p1
  fprintf('To help with indentation\n');
  fprintf('1234567890123456789012345\n');
  fprintf('a=%4d b=%-2d c=%03d \n',10,2,3);
  fprintf('%7.3f %+07.1f\n', 12.34, 5.60);
  fprintf('1234567890123456789012345\n');
  end


  function ex2p2
    function ex2p2a
      if (var1)
        fprintf('Block 1\n');
      elseif (var2)
        fprintf('Block 2\n');
      elseif (var3)
        fprintf('Block 3\n');
      else
        fprintf('Block 4\n');
      end
    end
     var1  = 1;       var2 = 0;        var3 = 0;
     ex2p2a;

     var1  = 0;       var2 = 1;        var3 = 1; 
     ex2p2a;

     var1  = 1;       var2 = 1;        var3 = 0; 
     ex2p2a;

     var1  = 0;       var2 = 0;        var3 = 1; 
     ex2p2a;

     var1  = 0;       var2 = 0;        var3 = 0; 
     ex2p2a;

   end


  function ex2p3
   a= 58+2*round(rand);
%   b= input('a=58 or 60, b= ');
b=58;
   c=180-a-b;

   if  ( a==b & b~=c | a==c & c~=b | b==c & a~=b                )                     % LINE 1
        disp('isosceles');                                                            % LINE 2
   elseif ( a==b & b==c                                         )                     % LINE 3
        disp('equilateral');
   else
        disp('scalene');
   end

   %
  end

  function ex2p4
    x=10;
    y=20;
    z=15;
    if ( x+z  >  2*y )
       fprintf('this\n');
    elseif ( 3*x < 4*z)
       fprintf('that\n');
    else
       fprintf('what\n');
    end

  end

  function ex2p5
    function ex2p5a
      disp('Code A');
      % Code A            
      for ii= 2:4        
       fprintf('%d\n',50-ii*10);
      end              
    end

    function ex2p5b
      disp('Code B');
      % Code B             
      for ii= -3:-1:-1    
       fprintf('%d\n',-ii*10);
      end       
    end

    function ex2p5c
      disp('Code C');
      % Code C
      for ii= [60 40 20]
       fprintf('%1d\n',ii/2);
      end
    end

  ex2p5a;
  ex2p5b;
  ex2p5c;
  fprintf(' A  and C print 30,20,10 one per line \n');
  end

  function ex2p6
    function ex2p6a
     fprintf(' P6.a: 3 variables: A, n, k \n');
    end
    function ex2p6b
      for ii=1:2:11
        fprintf('%d\n',ii);
        if ii==7
          continue;
        end
      end
    end
    function ex2p6c
      for ii=1:3:11
        fprintf('%d\n',ii);
        if ii==7
          break;
        end
      end
    end

   ex2p6a;
   fprintf('P6.b : \n'); ex2p6b;
   fprintf('P6.c : \n'); ex2p6c;
  end

  function ex2p7
 A= [ 6 18  15 30  6 13   15 7  6 15 11 ];               %
 n= length(A);                                           %
 for k = 1:n                                             %
  switch mod(A(k),9)                                     %
    case 0                                               %
     fprintf('do');                                      %
    case 1                                               %
     fprintf('you');                                     %
    case 2                                               %
     fprintf('?');                                       %
    case 3                                               %
     fprintf('I');                                       %
    case 4                                               %
     fprintf('like');                                    %
    case 5                                               %
     fprintf('\n');                                      %
    case 6                                               %
     fprintf(' ');                                       %
    otherwise                                            %
     fprintf('matlab');                                  %
   end     
  end
 end


  function ex2p8
   n=input('value of n = '); 
    AAA=1;
    ii=1;
    while (ii <  n)
      ii  = ii+1;
      AAA = ii^3 + AAA  ;
    end
    disp(AAA);
  end


  function ex2p9
   n = input('value of n = ');                                                % FIRST
   Stot=0;
   for ii=1:n
     Stot=Stot+ii;
   end
   disp(Stot);                                                                % LAST 
  end

  function ex2p10
   % 4 down 1
  limit = 5    ;
  ii= limit    ;                     % THIS IS LINE 2
  while (ii >= 2    )                % THIS IS LINE 3
    ii= ii - 1 ;
    fprintf('%d\n',ii);
  end

  end
