MAE-10 Winter 2010 Final Exam: Code Analysis and Problem Solving
MAE-10 Winter Quarter 2010 Final Examination
Instructions: You have until the end of the class period to complete the exam. Notes on both sides of an 8.5”x11” sheet of paper are allowed. Closed book. No calculators.
Section 1: Short Answer (2 points each)
(1.1) How many rows & columns in arrays W & Z?
X = [-1 : 2 : 3];
Y = [3 : -2 : -1];
W = [X Y];
Z = [X; Y];
(1.2) How many data lines will be plotted on the figure generated by the following code?
T = (0:1:10);
X = T.^2;
Y = X.^2;
plot(T, X, Y, X, T, T)
(1.3) What value(s) is/are stored in Y?
Array = [1 3 5 6 8 9 10 11];
Y = numel(Array);
(1.4) What value(s) is/are stored in X & Y?
Array = [1 3 5 6 8 9 10 11];
[X, Y] = size(Array);
(1.5) What value(s) is/are stored in Y?
X = [1 3 4, -2, -1];
Y = find(X >= 3);
(1.6) Convert the following binary (base-2) numbers to decimal (base-10) numbers. The decimal number system is what we use every day to count (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,…).
00001001
00111101
Section 2: Identify Errors in the Following Segments of Code
If an error exists, give an explanation of the error. If you believe that there are no errors, write “No Errors.” (2 points each)
(2.1)
X = (2 : -1.5 : -1);
Y = (6 : 2 : 12);
W = X + Y;
X(2) > X(3) & Y(1) ~= Y(2);
(2.2)
cheese = 'mouse';
Goose = 'rat';
Mouse = 'goose';
Couscous = 'house';
Moose = [cheese(1:1) goose(2:5)];
Fprintf('mouse is %s', couscous);
(2.3)
X = [2:2:10; -1:1:3];
A = min(X(1,:));
B = max(X(2,:));
if (A >= B)
disp(A)
else
disp(B + A)
if (X(2,2) ~= X(1,1) & X(1,1) > X(1,1))
disp(B)
if (A ~= B)
disp(A * B)
end
end
end
(2.4)
X = [1 5 7 8 9 1, -12];
Y = [2 4 6 8 10];
for k = 1:4
W(k) = X(2*k) + Y(k);
end
for m = k:4
H(m) = H(m) + W(k);
end
Fprintf('%7.2f %3i', H);
(2.5)
Y = [2 4 6 8 10];
K = 2;
while (Y(K) <= 6)
disp(Y(K))
K = K + 1;
end
(2.6)
animal = 'moose';
switch (animal)
case {'moose', 'moose'}
disp(animal)
case {'goosehorse'}
disp('gooseorhorse')
case {'hoof', 'antlers'}
disp('moo')
end
(2.7)
T = (0 : 0.1 : 1);
X = T.^2;
Y = T - 1;
Z = T.^-2;
subplot(2, 1, 3)
plot(Y, X)
subplot(2, 1, 2)
plot(T, T)
xlabel('dy-axis')
subplot(2, 1, 1)
plot(X, Y)
grid on
(2.8)
In “main” program:
A = 3.3;
B = 4.4;
[C, D] = banana(A, B);
Fprintf('a & b %5.2f & %5.2f', A, B);
In m-file containing the function banana:
function [result] = banana(b, a)
A = b;
B = a;
result = b + a;
endfunction
(2.9)
In “main” program:
A = 3.3;
B = 4.4;
Answer = gorilla(A, B);
Fprintf('answer is %7.2f', Answer);
Fprintf('c & d %7.2f %7.2f', C, D);
In m-file containing the function gorilla:
function [result] = gorilla(c, d)
C = 100;
D = 1000;
result = c + d;
endfunction
(2.10)
In “main” program:
A = 2;
B = 4;
Answer = funky(A) + funky(B);
Answer = Answer / monkey(funky(A), B);
In program containing the function funky:
function [X] = funky(Y)
X = 3;
for i = 1:Y
X = X + i;
end
endfunction
In program containing the function monkey:
function [Y] = monkey(b, a)
Y = a;
for i = b:a
Y = Y / i;
end
endfunction
Section 3: Write the Output from the Following Segments of Code
Write the output exactly as it would appear if executed in MATLAB or Octave. Indicate blank spaces with an underscore. Assume that there are no errors. (4 points each)
(3.1)
A = [1 3 5 7];
B = [5 7 8 9];
C = [A B]';
C = C(2:3:2);
Fprintf('%3i\n', C);
(3.2)
A = [1 2 2, -2, 10];
B = (A(2)^A(5) + A(2) / A(3) - A(1)) / A(2) + A(1);
C = A(5) - A(3) / A(2) - A(1)^A(5);
D = 2;
for i = 1:numel(A)
D = D + A(i)^A(-2*A(3));
end
Fprintf('%6.1f\n', B, C, D);
(3.3)
A = (1 : 3 : 101);
K = 1;
while (K < 5)
Fprintf('%7.2f%5.2f\n', A(1:K));
K = K + 2;
end
%note: there are no spaces between %7.2f & %5.2f
(3.4)
cheese = 'swiss';
A = [99 98 97 96 95];
switch cheese
case {'swiss', 'cheese'}
disp(cheese(2:5))
Fprintf('%5i', A(3:5))
case {'cheddar', 'swiss'}
disp(cheese(1:4))
Fprintf('%3i', A(1:3))
case {'swiss'}
disp(cheese(1:5))
Fprintf('%3i', A(1:5))
otherwise
disp(cheese)
Fprintf('%3i', A(2:3))
end
(3.5)
for col = 1:2
for row = 1:3
B(col, row) = col + row;
end
end
Fprintf('%5i%4i\n', B);
%note: there are no spaces between %5i & %4i
(3.6)
A = (1:4);
B = A * 2;
C = B * 2;
if (C(2) < A(4) || C(2) == A(4))
A = A + 1;
if (C(1) <= B(2) & C(2) > A(4))
Fprintf('helloworld\n')
A = A + 1;
elseif (C(1) > A(2) | C(3) > 9)
Fprintf('holamundo\n')
B = B - 1;
else
C = C * 2;
end
else
B = B * 2;
if (C(1) == B(1))
Fprintf('hallowelt\n')
C = C - 1;
elseif (C(4) / A(1) > 3)
Fprintf('howdymundo\n')
A = A + 3;
end
end
Fprintf('%2i%3i\n', A, C);
%note: there are no spaces between %2i & %3i
(3.7)
In “main” program:
A = (10:2:15);
B = (11:2:15);
Fprintf('%3i%3i\n', A(2:3));
Fprintf('%3i%3i\n', B(1:2));
[B, A] = func1(A, B);
Fprintf('%3i%3i\n', A(2:3));
Fprintf('%3i%3i\n', B(1:2));
%note: there are no spaces between %3i & %3i
In m-file containing the function func1:
function [A, B] = func1(B, A)
Temp = A;
A = B;
B = Temp;
endfunction
(3.8)
In “main” program:
A = (1:1:3);
B = (2:2:6);
Table1 = [A; B];
Fprintf('%2i%2i\n', Table1);
[X, Y] = func2(A, B);
Table2 = [X; Y];
Fprintf('%2i%2i\n', Table2);
%note: there are no spaces between %2i & %2i
In m-file containing the function func2:
function [A, B] = func2(X, Y)
for i = 1:2
A(i) = Y(i);
B(i) = X(i+1);
end
endfunction
(3.9)
In “main” program:
A = 4.5;
B = 2.5;
C = f4(B) / f3(A, B);
D = f4(A) * f4(A);
Fprintf('%6.1f', C, D, f4(A));
In m-file containing the function f3:
function [output] = f3(X, Y)
output = X + Y;
output = output - 3;
return
output = output / 2;
endfunction
In m-file containing the function f4:
function [output] = f4(X)
output = 1;
for i = 1:2
output = output + X;
end
endfunction
(3.10)
In “main” program:
A = 2;
B = 3;
C = f5(A, B) + f6(B, A);
Fprintf('%3i\n', C);
In m-file containing the function f5:
function [X] = f5(X, Y)
A = X + Y;
B = X - Y;
X = f6(A, B) + f6(B, A);
Fprintf('%3i%3i\n', A, B);
endfunction
In m-file containing the function f6:
function [X] = f6(X, Y)
A = X * Y;
B = X + A;
X = B + A;
Fprintf('%3i%3i\n', A, B);
endfunction
Section 4: Write a Program to Complete the Following Tasks
Do not write the output of the code.
(4.1) (7 points) You are provided with a data file called “medal.txt” containing Olympic medal counts for 100 nations.
In “medal.txt”:
1 5 7
2 4 0
5 1 0
...
2 9 0
1 1 1 1
Each row contains information about the # of gold, silver, & bronze medals won by each nation. The 1st column is the # of gold medals, the 2nd column is the # of silver medals, & the 3rd column is the # of bronze medals. For example, nation #2 has 2 gold medals, 4 silver medals, & zero bronze medals.
Create a program that will read the data from “medal.txt” & store it in an array. Then find out the following information & display the answers to the screen:
- Which nation (1-100) had the highest # of gold medals?
- Which nation (1-100) had the highest # of silver medals?
- Which nation (1-100) had the highest # of bronze medals?
- Which nation (1-100) had the highest total # of medals?
(4.2) (4 points) The expressions for X, Y, & Z are as follows:
X = t2 + 2
Y = t – 1
Z = X * Y
Create a program that calculates X, Y, & Z from t = 0 seconds to t = 10 seconds in increments of 0.1 seconds. Plot the following information on 3 separate subplots:
- In the upper-right section of the plotting window, plot X versus T.
- In the center section of the plotting window, plot Y versus X.
- In the center-right section of the plotting window, plot Z versus Y.
X versus T
Y versus X
Z versus Y
(4.3) (5 points) A program calls a function named switchrow
:
A = switchrow(A, m, n);
Create a function named switchrow
that exchanges the 1st & last rows of an M x N array (called A). For example, if the array A is:
4 5 8 1
0 3 6 7
2 3 5 1
0 0 0 9
Then the function will create an array with the following elements:
0 0 0 9
0 3 6 7
2 3 5 1
4 5 8 1
Make your function general for any M x N array. You cannot use any of MATLAB/Octave’s built-in functions for this problem except functions that determine the # of elements in an array.
(4.4) (4 points) A program calls a function named remainder
:
Remain = remainder(X, Y);
Create a function named remainder
that will calculate the remainder of the variable X divided by the variable Y. You cannot use MATLAB/Octave’s built-in function rem()
for this problem.
(4.5) (7 points) A program calls a function named sortscores
:
Sortedscores = sortnums(scores);
Create a function named sortnums
that takes as input a 1 x N array called scores
which contains test scores from a class. This function returns as output a 1 x N array called sorted
which contains the same scores sorted from lowest to highest. For example, if scores
contains the values [84.5, 90.1, 56.0, 92.9, 77.0], then sortedscores
should contain the values [56.0, 77.0, 84.5, 90.1, 92.9]. Make your function general for any size array. You may assume that all the scores are unique (there aren’t duplicate numbers).
You cannot use any of MATLAB/Octave’s built-in functions except those functions that determine the # of elements in an array.
(scratch paper) =>