11.5. Exercise Solutions#

Solution to Exercise 11.1

% 11. Loops Exercises

clear % Clear all variables
clc   % Clear command window

% Exercise 11.1
fprintf("Exercise 11.1\n-------------\n")
for i = 1 : 10
    fprintf("hello world\n")
end

Output

Exercise 11.1
-------------

hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

Solution to Exercise 11.2

% -----------------------------------------------------------------------------
% Exercise 11.2
n = 52;
n_factorial = 1;

for i = 1 : n
    n_factorial = n_factorial * i;
end

fprintf("\nExercise 11.2\n-------------\n")
fprintf("%d! = %d\n", n, n_factorial)

Output

Exercise 11.2
-------------

52! = 8.065818e+67

Solution to Exercise 11.3

% -----------------------------------------------------------------------------
% Exercise 11.3
x = pi / 4;
sinx = 0;

for n = 0 : 4
    sinx = sinx + (-1) ^ n / factorial(2 * n + 1) * x ^ (2 * n + 1);
end

fprintf("\nExercise 11.3\n--------------\n")
fprintf("sin(pi/4) = %0.16f \n", sinx)

Output

Exercise 11.3
--------------

sin(pi/4) = 0.7071067829368671 

Solution to Exercise 11.4

% -----------------------------------------------------------------------------
% Exercise 11.4
i = 1;
fprintf("\nExercise 11.4\n------------\n")

while i < 11
    fprintf("hello again\n")
    i = i + 1;
end

Output

Exercise 11.4
------------

hello again
hello again
hello again
hello again
hello again
hello again
hello again
hello again
hello again
hello again

Solution to Exercise 11.5

% -----------------------------------------------------------------------------
% Exercise 11.5
x = 100;
num_steps = 0;

fprintf("\nExercise 11.5\n-------------\n")
fprintf(" step |   n \n--------------\n   0  | %4d", x)

while x > 1
    if mod(x, 2) == 0
        x = x / 2;
    else
        x = 3 * x + 1;
    end

    num_steps = num_steps + 1;
    fprintf("%4d  | %4d \n", num_steps, x)

end

Output

 step |   n 
--------------
   0  |  100
   1  |   50 
   2  |   25 
   3  |   76 
   4  |   38 
   5  |   19 
   6  |   58 
   7  |   29 
   8  |   88 
   9  |   44 
  10  |   22 
  11  |   11 
  12  |   34 
  13  |   17 
  14  |   52 
  15  |   26 
  16  |   13 
  17  |   40 
  18  |   20 
  19  |   10 
  20  |    5 
  21  |   16 
  22  |    8 
  23  |    4 
  24  |    2 
  25  |    1 

Solution to Exercise 11.6

% -----------------------------------------------------------------------------
% Exercise 11.6
numbers = [1009, 2123, 6269, 8441];
fprintf("\nExercise 11.6\n-------------\n")

for n = numbers
    prime = true;

    for i = 2 : n - 1
        if mod(n, i) == 0
            prime = false;
            break
        end
    end

    if prime
        fprintf("%d is prime \n", n)
    else
        fprintf("%d is not prime \n", n)
    end
end

Output

Exercise 11.6
-------------

1009 is prime 
2123 is not prime 
6269 is prime 
8441 is not prime 

Solution to Exercise 11.7

% -----------------------------------------------------------------------------
% Exercise 11.7
fprintf("\nExercise 4.7\n------------\n")
fprintf("I'm thinking of a number between 0 and 100, guess what it is. \n")

n = randi(100);
guesses_left = 5;

while false
    guess = input(sprintf("\nYou have %d guesses remaining > ", guesses_left));
    guesses_left = guesses_left - 1;

    if guess == n
        fprintf("Congratulations, you win! \n")
        break

    elseif guess < n
        fprintf("Unlucky, your guess is less than my number \n")

    elseif guess > n
        fprintf("Unlucky, your guess is greater than my number \n")

    end

    if guesses_left == 0
        fprintf("Oh dear, you lose. My number was %d \n", n)
        break
    end
end

Output

Exercise 11.7
------------
I'm thinking of a number between 0 and 100, guess what it is.

You have 5 guesses remaining > 50
Unlucky, your guess is less than my number.

You have 4 guesses remaining > 75
Unlucky, your guess is greater than my number.

You have 3 guesses remaining > 66
Unlucky, your guess is greater than my number.

You have 2 guesses remaining > 60
Unlucky, your guess is greater than my number.

You have 1 guesses remaining > 57
Unlucky, your guess is less than my number.
Oh dear, you lose.

Solution to Exercise 11.8

% -----------------------------------------------------------------------------
% Exercise 11.8
fprintf("\nExercise 11.8\n-------------")
A = [3, 2, -1, 4 ; 7, -4, 0, 2];
B = [1, 0 ; 3, -2 ; 3, 6 ; -1, 4];
AB = zeros(size(A, 1), size(B, 2));

for i = 1 : size(A, 1)
    for j = 1 : size(B, 2)
        for k = 1 : size(A, 2)
            AB(i, j) = AB(i, j) + A(i, k) * B(k, j);
        end
    end
end
AB

BA = zeros(size(B, 1), size(A,2));
for i = 1 : size(B, 1)
    for j = 1 : size(A, 2)
        for k = 1 : size(B, 2)
            BA(i, j) = BA(i, j) + B(i, k) * A(k, j);
        end
    end
end

BA

Output

Exercise 11.8
-------------

AB =

     2     6
    -7    16



BA =

     3     2    -1     4
    -5    14    -3     8
    51   -18    -3    24
    25   -18     1     4