0% found this document useful (0 votes)
62 views9 pages

Assignment 03: Computer Programming and Application (ME-214)

This document contains the solutions to 10 questions from a MATLAB assignment on Chapter 6. The questions cover topics like loops, vectors, input/output, conditional statements, and unit conversions. The solutions demonstrate how to use these MATLAB programming concepts and functions to solve the given problems.

Uploaded by

Asad Mirza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views9 pages

Assignment 03: Computer Programming and Application (ME-214)

This document contains the solutions to 10 questions from a MATLAB assignment on Chapter 6. The questions cover topics like loops, vectors, input/output, conditional statements, and unit conversions. The solutions demonstrate how to use these MATLAB programming concepts and functions to solve the given problems.

Uploaded by

Asad Mirza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CPA Assignment 03 Name: Asad Javed

Chapter 06 Roll no.: ME-19062


Section: B

Computer Programming and Application (ME-214)


Assignment 03
Chapter 06

Question: 01
Script:
for i=1:4
for j=1:6
M(i,j)= (2*i)-(3*j);
end
end
disp(M)
Answer:

Question: 02
Script:
v=input('Enter an abitrary vector: ');
n=length(v);
for i=1:n
if v(i) > 0
x(i)=2*v(i);
end
if v(i) < 0
x(i)= 3*v(i);
end
end
fprintf('Random vector\n')
disp(v)
fprintf('Modified vector\n')
disp(x)
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B

Answer:

Question: 03
Script:
clear all, clc;
NYC=[33 33 18 29 40 55 19 22 32 37 58 54 51 52 45 41 45 39 36 45 33 18 19 19
28 34 44 21 23 30 39];
DEN=[39 48 61 39 14 37 43 38 46 39 55 46 46 39 54 45 52 52 62 45 62 40 25 57
60 57 20 32 50 48 28];
%part(a)
av_NYC=round(mean(NYC)); av_DEN=round(mean(DEN));
fprintf('The average temperature in New York City is %d F.\n',av_NYC) fprintf('The
average temperature in Denver is %d F.\n',av_DEN)
%part(b) z=av_NYC<NYC;
days_NYC=sum(z);
fprintf('For %d days the temperature was above average in New York
City.\n',days_NYC)
x=av_DEN<DEN; days_DEN=sum(x);
fprintf('For %d days the temperature was above average in
Denver.\n',days_DEN)
%part(c) c=DEN>NYC;
days_c=sum(c);
fprintf('For %d days the temperature in Denver was higher than the temperature
in New York City.\n',days_c)
Answer:

Question: 04
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B

Script:
v=[0 1];
for n=3:25
v(n)=v(n-1)+v(n-2);
end
v

Answer:

Question: 05
Script:
clear all, clc;
x=[9 -1.5 13.4 13.3 -2.1 4.6 1.1 5 -6.1 10 0.2];
n=length(x);
for k=1:n-1
for m=1:n-k
if x(m)>x(m+1)
B=x(m); S=x(m+1);
x(m+1)=B; x(m)=S;
end
end
end
x

Answer:
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B

Question: 06
Script:
clear all, clc;
s=[72 81 44 68 90 53 80 75 74 65 50 92 85 69 41 73 70 86 61 65 79 94 69];
Av=round(mean(s));
Sd=round(std(s));
fprintf('The Average score is %d%% and the Standard deviation is %1.2f%% \n',Av,Sd)
n=length(s);
for k=1:n
if s(k)>=(Av+1.3*Sd)
fprintf('%3i%% \t Letter grade A \n',s(k))
elseif ((Av+0.5*Sd)<=s(k))&(s(k)<(Av+1.3*Sd))
fprintf('%3i%% \t Letter grade B \n',s(k))
elseif ((Av-0.5*Sd)<=s(k))&(s(k)<(Av+0.5*Sd))
fprintf('%3i%% \t Letter grade C \n',s(k))
elseif ((Av-1.3*Sd)<=s(k))&(s(k)<(Av-0.5*Sd))
fprintf('%3i%% \t Letter grade D \n',s(k))
elseif s(k)<(Av-1.3*Sd)
fprintf('%3i%% \t Letter grade F \n',s(k))
end
end

Answer:
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B

Question: 07
Script:
for x=100:999
a=fix(x/100);
b=fix((x-(a*100))/10);
c=x-(a*100)-(b*10);
Prod=a*b*c;
Sum=a+b+c;
if Prod==Sum
x
end
end
Answer:

Question: 08
Script:
disp(' ')
W=input('Enter weight in lb: ');
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B
H=input('Enter height in inches: ');
BMI=(703*W)/(H^2);
if BMI<18.5
fprintf('Your BMI value is %1.1f, which classifies you as Underweight.\n',BMI)
elseif (BMI>=18.5)&(BMI<=24.9)
fprintf('Your BMI value is %1.1f, which classifies you as Normal.\n',BMI)
elseif (BMI>=25)&(BMI<=29.9)
fprintf('Your BMI value is %1.1f, which classifies you as Overweight.\n',BMI)
elseif BMI>=30
fprintf('Your BMI value is %1.1f, which classifies you as Obese.\n',BMI)
end
Answer:

Question: 09
Script:
Type_of_car=menu('Car type','Sedan','SUV')
n_o_d=input('Enter the no. of days the car was rented: ');
miles=input('Enter no. of miles driven: ');
switch Type_of_car
case 1
if (n_o_d>=1)&(n_o_d<=6)
rate=79; fmpd=80; cam=0.69;
if miles<=fmpd
cost=rate*miles;
else
cost=(rate*fmpd)+((miles-fmpd)*cam)
end
elseif (n_o_d>=7)&(n_o_d<=29)
rate=69; fmpd=100; cam=0.59;
if miles<=fmpd
cost=rate*miles;
else
cost=(rate*fmpd)+((miles-fmpd)*cam)
end
elseif n_o_d>=30
rate=59; fmpd=120; cam=0.49;
if miles<=fmpd
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B
cost=rate*miles;
else
cost=(rate*fmpd)+((miles-fmpd)*cam)
end
end
case 2
if (n_o_d>=1)&(n_o_d<=6)
rate=84; fmpd=80; cam=0.74;
if miles<=fmpd
cost=rate*miles;
else
cost=(rate*fmpd)+((miles-fmpd)*cam)
end
elseif (n_o_d>=7)&(n_o_d<=29)
rate=74; fmpd=100; cam=0.64;
if miles<=fmpd
cost=rate*miles;
else
cost=(rate*fmpd)+((miles-fmpd)*cam)
end
elseif n_o_d>=30
rate=64; fmpd=120; cam=0.54;
if miles<=fmpd
cost=rate*miles;
else
cost=(rate*fmpd)+((miles-fmpd)*cam)
end
end
end
fprintf('The payable rent of the car is $ %1.2f \n',cost)

Answer:
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B

Question: 10
Script:
disp(' ')
x=input('Enter the value of area to be converted: ');
unit_in=menu('Current units','m^2','cm^2','in^2','ft^2','yd^2','acre')
unit_out=menu('New units','m^2','cm^2','in^2','ft^2','yd^2','acre')
switch unit_in
case 1
Acm=x*(100^2);
case 2
Acm=x;
case 3
Acm=x*6.4516;
case 4
Acm=x*929.03;
case 5
Acm=x*8361.27;
case 6
Acm=x*4.047E7;
end
switch unit_out
case 1
Aout=Acm/(100^2);
fprintf('Area=%d m^2\n',Aout)
case 2
CPA Assignment 03 Name: Asad Javed
Chapter 06 Roll no.: ME-19062
Section: B
Aout=Acm;
fprintf('Area=%d cm^2\n',Aout)
case 3
Aout=Acm/6.4516;
fprintf('Area=%d in^2\n',Aout)
case 4
Aout=Acm/929.03;
fprintf('Area=%d ft^2\n',Aout)
case 5
Aout=Acm/8361.27;
fprintf('Area=%d yd^2\n',Aout)
case 6
Aout=Acm/4.047E7;
fprintf('Area=%d acre^2\n',Aout)
end

Answer:

You might also like