% alex gerbessiots Exam1 Fall   2015
% from the MATLAB command window run
% clear;clc
% e1101f15
%
% follow instructions
%
function e1101f15()
 global X Y Z R S ;
 X = [ 2 1 2 ; 2 1 2 ; 1 0 1 ];
 Y = [ 1 1 2 ; 2 1 1 ];
 Z = [ 1 2 1 2];
 R = [ 1 ; 2 ; 1 ];
 S = [ 1 2 3 ];

fprintf('1.  *******************  Problem 1. (60 points= 5x12)\n')
      ex1p1;
      input('Press ENTER to Continue');
%
%
fprintf('2.  *******************  Problem 2. (30 points)\n')
      ex1p2;
%      input('Press ENTER to Continue');
%
%
fprintf('3.  *******************  Problem 3. (90 points)\n')
      ex1p3;
      input('Press ENTER to Continue');
%
%
fprintf('4.  *******************  Problem 4. (24 points) \n')
      ex1p4;
     %input('Press ENTER to Continue');
%
%
fprintf('5.  *******************  Problem 5. (36 points)\n')
      ex1p5;
      %input('Press ENTER to Continue');
%
%
fprintf('6.  *******************  Problem 6. (36 points)\n')
      ex1p6;
      %input('Press ENTER to Continue');
%
%
fprintf('7.  *******************  Problem 7. (24 points)\n')
      ex1p7;
      %input('Press ENTER to Continue');
%
%
fprintf('8.  *******************  Problem 8. (30 points)\n')
      ex1p8;
      %input('Press ENTER to Continue');
%
fprintf('9.  *******************  Problem 9. (15 points)\n')
      ex1p9;
      %input('Press ENTER to Continue');
%
%
%
%
end

%%%%%
%%%%%
  function ex1p1
   global X Y Z R  S;

    fprintf('1.1 *********  2KiB     = %d\n', 2*1024)
    fprintf('1.2 *********  1Kib     = %s\n', 'nonsense')
    ans=int32  (1); zorro=whos('ans');
    fprintf('1.3 *********  1int32   = %d\n', zorro(1).bytes)
    ans=char  (1); zorro=whos('ans');
    fprintf('1.4 *********  1char    = %d\n', zorro(1).bytes) 
    ans=logical(1);zorro=whos('ans');
    fprintf('1.5 *********  1logical = %d\n', zorro(1).bytes)
    fprintf('1.6 ********* uint8      :  min =  %d , max= %d values = %d \n',uint8(0   ),uint8(255),256);
    fprintf('1.7 *********  Y(end-1,end-1) = %d\n', Y(end-1,end-1));
    fprintf('1.8 *********  X(end-3)       = %d\n', X(end-3)     );
    fprintf('1.9 *********  40 in hexadecimal = 0x%s\n', dec2base(40,16));
    fprintf('1.10*********  8 bits    \n');
    clear j;
    fprintf('1.11********* i*i*i*i = %d\n', i*i*i*i );
    fprintf('1.12*********  40 in binary      = 00%s\n', dec2base(40,2 ));
    %

  end


  function ex1p2
   global X Y Z R  S;
  
   p2a=   5 == 5  == 5 ;             ;zorro=whos('p2a');
   fprintf('2a          p2a  = %2d  %2d x %2d  %2d    %s\n', ...
          p2a, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);

   p2b=  int32(5*5-5);         ;zorro=whos('p2b');
   fprintf('2b          p2b  = %2d  %2d x %2d  %2d    %s\n', ...
          p2b, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
  
   p2c=   5>5+ 5 ; zorro=whos('p2c');
   fprintf('2c          p2c  = %2d  %2d x %2d  %2d    %s\n', ...
          p2c, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);

   p2d=   5&5+5           ; zorro=whos('p2d');
   fprintf('2d          p2d  = %2d  %2d x %2d  %2d    %s\n', ...
          p2d, zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
  
   p2e=   12:-2:1 ;zorro=whos('p2e');
   fprintf('2e          p2e  = %2s  %2d x %2d  %2d    %s\n', ...
          mat2str(p2e), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);

   p2f=   R*S ;zorro=whos('p2f');
   fprintf('2f          p2f  = %s  %2d x %2d  %2d    %s\n', ...
          mat2str(p2f), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);


  end


  function ex1p3
   global X Y Z R  S;
  
   p3a= 2: 2 :11;
   zorro=whos('p3a');
   fprintf('3a  p3a  = %-19s  \n', mat2str(p3a));
%   fprintf('3a  p3a  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3a), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3b= size(Y);
   zorro=whos('p3b');
   fprintf('3b  p3b  = %-19s  \n', mat2str(p3b));
%   fprintf('3b  p3b  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3b), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3c=length(Y);
   zorro=whos('p3c');
   fprintf('3c  p3c  = %-19s  \n', mat2str(p3c));
%   fprintf('3c  p3c  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3c), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3d= 2*eye(3)+4*ones(3)-2;
   zorro=whos('p3d');
   fprintf('3d  p3d  = %-19s  \n', mat2str(p3d));
%   fprintf('3d  p3d  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3d), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3e=sum(Y);
   zorro=whos('p3e');
   fprintf('3e  p3e  = %-19s  \n', mat2str(p3e));
%   fprintf('3e  p3e  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3e), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3f=diag(Y);
   zorro=whos('p3f');
   fprintf('3f  p3f  = %-19s  \n', mat2str(p3f));
%   fprintf('3f  p3f  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3f), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3g=X(1:3, 2:end);
   zorro=whos('p3g');
   fprintf('3g  p3g  = %-19s  \n', mat2str(p3g));
%   fprintf('3g  p3g  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3g), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3h=X .* transpose(X);
   zorro=whos('p3h');
   fprintf('3h  p3h  = %-19s  \n', mat2str(p3h));
%   fprintf('3h  p3h  = %-19s  %2d x %2d  %2d    %s\n', ...
%           mat2str(p3h), zorro(1).size(1),zorro(1).size(2),zorro(1).bytes,zorro(1).class);
   %
   p3i=diag(diag(Y));
   zorro=whos('p3i');
   fprintf('3i  p3i  = %-19s  \n', mat2str(p3i));
   %
   %p3j=sum(X(:));
   %zorro=whos('p3j');
   %fprintf('3j  p3j  = %-19s  \n', mat2str(p3j));
   %
   %p3k= transpose(R) * R;
   %zorro=whos('p3k');
   %fprintf('3k  p3k  = %-19s  \n', mat2str(p3k));
   %
   %p3l= S*R ;
   %zorro=whos('p3l');
   %fprintf('3l  p3l  = %-19s  \n', mat2str(p3l));
   %
   %p3m= R * S ;
   %zorro=whos('p3m');
   %fprintf('3m  p3m  = %-19s  \n', mat2str(p3m));
   %
   %p3n= 2:-1:2;
   %zorro=whos('p3n');
   %fprintf('3n  p3n  = %-19s  \n', mat2str(p3n));
   %
   %p3o=length(['1234' '5678']);
   %zorro=whos('p3o');
   %fprintf('3o  p3o  = %-19s  \n', mat2str(p3o));
   %

   end


  function ex1p4
   global X Y Z R  S;
   fprintf('4a Column Major order of X is   %30s\n', mat2str(X(:)));
   p5=X';
   fprintf('4b Row    Major order of X is   %30s\n', mat2str(p5(:)));
  end

  function ex1p5
   global X Y Z R  S;
   B=X;disp(B);
   fprintf('5a  Copy X into B: B=X \n B= %19s\n',mat2str(B));
   fprintf('5b  Third row ofB: B(3,:) = %19s\n',mat2str(B(3,:)));
   fprintf('5c  Second colofB: B(:,2) = %19s\n',mat2str(B(:,2)));
   B(1,:)=[];
   fprintf('5d  Del 1strowofB: B(1,:) =[] \n B= %19s\n',mat2str(B));
   fprintf('5e  X(:) is column major X as vector/column  %19s\n',mat2str(X(:)));
   fprintf('5f  (1:10).^3 is %19s\n',mat2str((1:10).^ 3));
  end


  function ex1p6
    p6a=10;
    p6b=15;
    p6c=20;
    fprintf('6i  Initially  p6a = %2d p6b= %2d p6c = %2d;',p6a,p6b,p6c);
    p6c=p6a;
    p6a=p6b;
    p6b=p6c;
    fprintf('At the end p6a = %2d p6b= %2d p6c = %2d \n',p6a,p6b,p6c);
    p6d=10;p6e=15; p6f=25;
    fprintf('6ii Initially  p6d = %2d p6e= %2d p6f = %2d;',p6d,p6e,p6f);
    p6d= p6d+p6e+p6f;
    p6e= p6d+p6f;
    p6f= p6d+p6e+p6f;
    fprintf('At the end p6d = %2d p6e= %2d p6f = %2d \n',p6d,p6e,p6f);
  end


  function ex1p7
    n=input('Order of approximation to order n ');
    a= [1:n];                                         % Line 2 modified
    a= 1 ./ a;                                        % Line 3 modified
    a= sum(a);
    c= a - log(n);                                    % Line 5 modified -log(n) added
    fprintf('p7: c(n) = '); disp(c)                   % We inserted an fprintf before disp(c) in solutions
  end

  function ex1p8
   p8a=25:-4:-3;
   fprintf('8a   p8a = 25:-4:-3 = %30s\n',mat2str(p8a));
   p8b =  sum(sum( (3*ones(400,400)+2*eye(400)-1) == 2)); 
   fprintf('8b   p8b = %10s\n',mat2str(p8b));
   p8c=zeros(5,5);p8c(:)=(1:25);
   fprintf('8c   p8c = %25s\n',mat2str(p8c));
  end

  function ex1p9
   a= floor(20*rand(5,6));
   fprintf('9a   a= %70s\n',mat2str(a));
   c1 = a > 10; c2= a == 10; c3 = a<10;
   b  = c1 .* a -10 * c2 + 20 * c3;
   fprintf('9a   b= %70s\n',mat2str(b));
  end
