You are on page 1of 5

Name: Matric No: Section:

SKEE 1022 Scientific Programming (2018-19-2)


Test 1 (50 min). Answer all questions in the exam paper.
Q.1 (a) Given the following algorithm of a coffee machine,
1. Take input from user on type of coffee (either
latte, cappuccino, or black coffee), with or
without sugar, and number of cups of coffee.
2. Pour the coffee in a cup
3. If user choose cappuccino or latte, then add
milk
4. If user want sugar, then add sugar
5. Output the coffee to the user.
6. If more cup of coffee wanted, then repeat back
from Step 2.
Convert the algorithm into a flowchart form.
(5 m)

(b) Evaluate the following MATLAB code and determine the value of X and
Y. Show your work step-by-step.
A = [-1 3 2; -5 2 4; 0 3 4]
B = [0 -3 2; 0 2 0; 0 3 4]
X = A(logical(B))
Y = B(A(:,[2,3])
(5 m)
Name: Matric No: Section:

Q.2 (a) Write a function named LCONV to execute a length conversion from metric
unit (in meter) to U.S. customary unit (in foot, mile, inch). The conversion
rates are given as follow:
1 feet = 0.3048 meter
1 mile = 1609.34 meter
1 inch = 0.0254 meter
This function needs to have a help information. An example of the function
call is;

>> help LCONV


this function is used for length conversion
from meter to foot or mile or inch
>> [foot, mile, inch] = LCONV (10)
foot =
32.8084
mile =
0.0062
inch =
393.7008
(4 m)

(b) Write an appropriate MATLAB code to prompt the user to enter the
length value and display the results in 2 decimal places using fprintf
as follows:

>> Please enter the length value in meter :10


10.00 m is equal to 32.81 feet
10.00 m is equal to 0.01 miles
10.00 m is equal to 393.70 inches
(6 m)
Name: Matric No: Section:

Q3 (a) Consider the following MATLAB code.

A = [true,false; true,true; false,false; false,true]


B = A(1:4,1)
C = A(1:4,2)
D = B+C
Res(D==1,:) = A(D==1,:)
Res

Show the matrices A, B, C and D. Determine the final value of Res.


(5 m)

(b) Given the following MATLAB code. Change the code from while-loop into an
equivalent for-loop code.
(5 m)

str=['M' ,'K', 'A', 'B'; 'T' ,'D' ,'L' ,'F'; 'A'


,'H', 'B', 'J'];
i=1;
k=1;
C='';
while i<=3
j=1;
while j<=4
if (j==1||j==3)
C(k)=str(i,j);
end
j=j+1;
k=k+1;
end
i=i+1;
end
Name: Matric No: Section:

Solution
Q1 (a) As in the flowchart 5
(b)
>> A = [-1 3 2; -5 2 4; 0 3 4] 5
A =
-1 3 2
-5 2 4
0 3 4
>> B = [0 -3 2; 0 2 0; 0 3 4]
B =
0 -3 2
0 2 0
0 3 4
>> X = A(logical(B))
X =
3
2
3
2
4
>> Y = B(A(:,[2,3]))
Y =

0 0
0 -3
0 -3

Q2 (a) 4
function [feet, mile, inch]= LCONV (m)
%this function is used for length conversion
%from meter to foot or mile or inch
feet = m/0.3048;
mile = m/1609.34;
inch = m/0.0254;
end

(b)
m = input('Please enter the length value in meter :');
[feet, mile, inch]= LCONV (m);
fprintf ('%.2f m is equal to %.2f feet\n',m,feet);
fprintf ('%.2f m is equal to %.2f miles\n',m,mile); 6
fprintf ('%.2f m is equal to %.2f inches\n',m,inch);

Q3 (a) 5
A =

1 0
1 1
0 0
0 1
B =

1
1
0
0
C =

0
1
0
1
D =
Name: Matric No: Section:

1
2
0
1

Res =

1 0

0 0

0 0

0 1

(b) 5
str=['M' ,'K', 'A', 'B'; 'T' ,'D' ,'L' ,'F'; 'A' ,'H', 'B', 'J'];

C='';

for i=1:3

for j=1:4

if (j==1||j==3)

C(k)=str(i,j);

end

end

end

You might also like