You are on page 1of 8

Faculty of Mechanical Engineering

Universiti Teknologi Malaysia

SKMM 1013 Sem I 2013/14

TEST 2

Name:___________________________________________

Lecturer:_________________________________________

Part A (Programming C)
Q1 /5
Q2 /5
Total /10

Part B (Programming with MATLAB)

Q3 /3
Q4 /4
Q5 /4
Q6 /4
Total /15
Question 1 [5 marks]

The conversion factors from SI unit to USCS unit for length and mass are given by

1 meter (m) = 3.281 feet (ft)


1 kilogram (kg) = 2.205 pound mass (lb)

Write a complete C program to read in the volume in m3 and mass in kg of an object. The
program should compute and display the density of the object both in kg/m3 and lb/ft3. The
conversion from kg/m3 to lb/ft3 must be computed using a programmer-defined function with
the density in kg/m3 as an input argument.

#include <stdio.h>

float conv(float dsi)

float deng;

deng=dsi*2.205/(3.281*3.281*3.281);

return(deng);

main()

float vol,mass,dsi;

printf("Enter mass and volume in SI units:\n");

scanf("%f %f",&mass,&vol);

dsi=mass/vol;

printf("density SI = %f\n",dsi);

printf("density imperial = %f\n",conv(dsi));

system("pause");

}
Question 2 [5 marks]

A third order polynomial function is given as f(x) = x3 – 2x + 5 .Write a complete C program to


compute f(x) for x={-6, -3, -1, 0, 1, 3, 6}. You must store the values of x and f(x) in one-
dimensional arrays. The program must properly display all the values of x and f(x).

#include <stdio.h>

main()
{
int i;
float x[]={-6,-3,-1,0,1,3,6};
float fx[7];

for (i=0;i<=6;++i)
fx[i]=x[i]*x[i]*x[i] - 2*x[i] + 5;

for (i=0;i<=6;++i)
printf("%f %f\n",x[i],fx[i]);

system("pause");
}
Question 3 [3 marks]
What are the outputs of the following MATLAB/OCTAVE commands as they would appear in the
command window?

a) x=[-5:3:5]

x= -5 -2 1 4

b) n=length([-2:4])

n=7

c) y=linspace(0,3,7)

0.00000 0.50000 1.00000 1.50000 2.00000 2.50000 3.00000

d) a=[2 -4 0 12 5];

f=(-1)*sort(-a)

f = 12 5 2 0 -4

e) [ymin, k]=max([4 -5 0 12 -8]);

k=4

f) b=floor(-1.1)

b = -2
Question 4 [4 marks]
What are the outputs of the following segment of an OCTAVE/MATLAB program?

X = [1 2; 3 4];

Y = [2 -5 0; 4 0 8; -1 3 7];

a = Y(1,1:2) *X

b = find(Y(2,:))

c = X.*Y(2:3,1:2)

d = sum(Y(:,3))

a = 13 -16

b = 1 3

c = 4 0

-3 12

d = 15
Question 5 [4 marks]
The following table shows the mass and velocity for three objects.

object
1 2 3
Mass, m (kg) 5 3 8
Velocity, v (m/s) 18 22 15

Write MATLAB/OCTAVE codes to determine:


a) momentum (mv) for each object
b) kinetic energy (mv2/2) for each object
c) object with the smallest kinetic energy

m = [5 3 8];

v = [18 22 15];

a) momentum = m.*v = [90 66 120]

b) KE = 0.5*momentum.*v = [810 726 900]

c) Smallest KE = min(KE) = 726


Question 6 [4 marks]
Trace the following segment of an OCTAVE/MATLAB program and predict the output of the program as it
would appear in the command window.

x = [0 -3 2 -1 0 4];

n = length(x); sign_change = 0;

for j=1:n

y(j) = x(j)^2;

z(j) = sqrt(y(j));

if (z(j)~=x(j))

sign_change = sign_change + 1;

end;

end;

x - z

sign_change

y =

0 9 4 1 0 16

z =

0 3 2 1 0 4

y-z =

0 -6 0 -2 0 0

sign_change = 2

You might also like