CS130R: Daily Quizzes

Each daily quiz consists of a few lines of MATLAB code. Your job is to interpret the code and provide the appropriate output.

Contents

Quiz 1: Monday, 2016-01-25

myvar = 6
myvar = myvar + 1;
myvar == 7

Quiz 2: Wednesday, 2016-01-27

vec1 = [1, 3, 5];
vec2 = [4, 2, 0];
sumvec = vec1 + vec2
prodvec = vec1 .* vec2

Quiz 3: Monday, 2016-02-01

mat = [3, 5; 23, 19];
x = mat(2,:)
length(x)

Quiz 4: Wednesday, 2016-02-03

fprintf('Let''s try to print a variable\n')
myvar = 2.3;
fprintf('Here it is %.2f\n', myvar)

Quiz 5: Monday, 2016-02-08

x = [1, 2];
y = [1, 2];

%plot the points (1,1) and (2,2)
plot(x,y)
title('My Plot')
xlabel('X')
ylabel('Y')

Quiz 6: Wednesday, 2016-02-10

if 'a'
    disp('action1')
elseif 3 > 1
    disp('action2')
else
    disp('action3')
end

Quiz 7: Monday, 2016-02-15

myvar = 'd';

switch myvar
    case 'a'
        disp('you entered an a')
    case 'b'
        disp('you entered a b')
    case 'c'
        disp('you entered a c')
    otherwise
        disp('not a, b, or c')
end

Quiz 8: Wednesday, 2016-02-17

for i = 1:5
    disp(i+1)
end

Quiz 9: Monday, 2016-02-22

val = 3;

for i = -val:val
    if i ~= -1
        stars = ones(1,abs(i))*42;
        disp(char(stars))
    end
end

Quiz 10: Wednesday, 2016-02-24

player = 1;

if player == 1
    player = -1;
else
    player = 1;
end

disp(player)

Quiz 11: Wednesday, 2016-03-16

x = 1;

Quiz 12: Monday, 2016-03-21

string = 'The quick brown fox jumps over the lazy dog';
string = strrep(string,'lazy', 'sleeping');
disp(string)

Quiz 13: Wednesday, 2016-03-23

cellvec = {4, 'the', 1:3};
cellvec{3}(1)

Quiz 14: Monday, 2016-03-28

jane = struct('name', 'jane', 'birthday', '01/02/03', 'eye', 'brown');
disp(jane.eye)

Quiz 15: Wednesday, 2016-03-30

fid = fopen('/path/to/file/that/doesnt/exist', 'r');
disp(fid)

Quiz 16: Monday, 2016-04-04

fid = fopen('/path/to/file.txt', 'w');
cr = fclose(fid);
disp(cr)

Quiz 17: Monday, 2016-04-11

myfunc = @(x) x+2;
myfunc(3)

Quiz 18: Wednesday, 2016-04-13

intmin('uint8')
intmax('uint8')

Quiz 19: Monday, 2016-04-18

img1 = imread('/path/to/image.jpg');
tlcrop = @(x) x(1:floor(end/2), 1:floor(end/2), :);
img2 = tlcrop(img1);
if size(img1, 1) > size(img2, 1) && size(img1, 2) > size(img2, 2)
    disp('cropped');
end

Quiz 20: Wednesday, 2016-04-20

minmax = @(x) [min(x) max(x)];
vec = [12 3 24 7];
minmax(vec)