Professional Documents
Culture Documents
MPPPS Chapter3
MPPPS Chapter3
MPPPS Chapter3
Sol
Command window
>> 'b'>='c'-1
ans =
>> 3==2+1
ans =
>> (3==2)+1
ans =
>> xor(5<6,8>4)
ans =
Sol
Algorithm
1. User enters a character x or other.
2. Use if else.
3. It will print an error other than x.
Mfile
letter=input('Enter a X=','s');
if (letter=='x'||letter=='X')
disp('It is X');
else
disp('Error');
end
Output
Enter a X=s
Error
Enter a X=x
It is X
Sol
Algorithm
1. Display script use.
2. User enters a hour.
3. If it is equal to or greater than 12 use if.
4. And if not than use else .
5. Show the output.
Mfile
disp('12 hour clock');
hour=input('Enter a hour=');
if hour>=12
a=1;
hour=a;
else
hour=hour+1;
fprintf('The next hour will be %d\n',hour);
end
Output
12 hour clock
Enter a hour=3
Enter a hour=12
Sol
Algorithm
1. Display software use.
2. User enters length,width and height.
3. Calculate volume of pyramid.
4. Display output
M file
disp('This program will calculate the volume of pyramid');
disp('The units are in inches');
L=input('Enter a length=');
W=input('Enter a width=');
H=input('Entera heigth=');
base=L*W;
volume=(1/3)*base*H;
Output
This program will calculate the volume of pyramid
Enter a length=9
Enter a width=4
Entera heigth=8
Sol
Algorithm
Mfile
letter=input('Are you an engineer(Y/N)=','s');
if letter=='Y'||letter=='y'
else
Output
Sol
Sol
Algorithm
M file
numerator=input('Enter numerator=');
if numerator==0
else
fraction=(numerator/denominator);
fprintf('The fraction is %f\n',fraction);
end
end
Output
Enter numerator=9
Enter denominator=8
Enter numerator=0
Enter numerator=8
Enter denominator=0
Algorithm
1. User enters systolic and diastolic pressure
2. If either systolic and diastolic pressure is less than 120 and less than 80
respectively than display” You are a candidate for experiment.’’
3. Otherwise display ‘’ You are not a candidate for experiment’’
4. Use nested if else to create script
M file
if systolic<120
if diastolic<80
else
fraction=(systolic)/(diastolic);
end
Output
Sol
Algorithm
1. Display software use
2. User enters area of inlet
3. User enters area of outlet
4. User enters velocity at inlet
5. Make the logic
6. Use elseif command to compare velocities
7. Display the output in descriptive manner.
Mfile
a=(A1/A2);
V2=(a*V1);
if V1==V2
disp('Velocity is same at inlet and outlet');
elseif V2<V1
disp('Velocity at outlet is less than inlet ');
else
end
OUTPUT
Sol
Algorithm
1. Display software use.
2. User enters first value which is m.
3. User enters second value which is n.
4. Use if elseif to make the script
5. Make a logic from mton, nto m using colon operator and if m is equal ton
6. Show the vector as a result.
Mfile
disp('This software return a vector from m to n')
a=m:1:n;
if m>n
b=m:-1:n;
fprintf('%d ',b)
elseif m<n
fprintf('%d ',a)
elseif m==n;
fprintf('%d \n',m)
end
OUTPUT
This software return a vector from m to n
8 7 6 5 >>
4 5 >>
Algorithm
Already in the question
Mfile
if letter=='Y'||letter=='y'
disp('Ok continuing');
else if letter=='N'||letter=='n'
disp('Ok, halting');
else
disp('Error');
end
end
OUTPUT
Sol
Algorithm
Already in question before
M file
switch letter
case 'y'
disp('Ok,continuing');
case 'Y'
disp('Ok,continuing');
case 'n'
disp('Ok,halting');
case 'N'
disp('Ok,halting');
otherwise
disp('Error')
end
OUTPUT
Enter your answery
Ok,continuing
Enter your answerY
Ok,continuing
Enter your answerN
Ok,halting
Enter your answern
Ok,halting
Enter your answerq
Error
Sol
Algorithm
1. Display software use
2. The speed of sound is 314m/s.
3. User enters speed of aircraft in m/s.
4. Calculate mach number.
5. Use condition statements.
6. Display the condition.
Mfile
disp('This software calculates and shows the condition of Mach number');
vsound=314;
if machno>1
disp('The flow is supersonic');
elseif machno<1
else
end
OUTPUT
This software calculates and shows the condition of Mach number
Algorithm
M file
celsius=input('Enter temperature in celsius :');
F=9/5*(celsius)+32;
K=celsius+273.15;
if fk=='f'||fk=='F'
elseif fk=='k'||fk=='K'
end
Output
Sol
Algorithm
1. User enters an integer
2. Use remainder function
3. Make logic
4. Display the output
Mfile
integer=input('Enter an integer=');
if rem(integer,4)==0
disp('1');
else
disp('0')
end
Output
Enter an integer=8
1
Enter an integer=64
1
Enter an integer=9
0
Sol
Algorithm
1. Generate a random integer in the range from1 to 1000
2. Use remainder function with if statement.
3. Display the output.
M file
integer=randint(1,1,[1,1000])
if rem(integer,2)==0
disp('1');
else
disp('0');
end
Output
integer =
36
1
integer =
850
1
integer =
934
1
integer =
679
0
Sol
Algorithm
1. Display software use
2. User enters all 3 integers.
3. Use if statement to make a logic.
4. Display the output
M file
if (a^2+b^2)==c^2
disp('1')
else
disp('0')
end
Output
Sol
Algorithm
1. Display software use
2. Use enters wind speed in milesperhour
3. Use if elseif clauses to differentiate which type of storm it is.
4. Display the output according to wind speed.
M file
if windspeed<38
elseif (windspeed>=39)&&(windspeed<=73)
elseif windspeed>=74
disp('it is a hurricane');
end
Output
Sol
Algorithm
1. Display software use.
2. User enters the windspeed.
3. Use if elseif for the chart given above
4. Display the hurricane category and typical storm surge.
Mfile
disp('This software categorizes Hurricanes on wind speed');
if windspeed>=74&&windspeed<=95
elseif windspeed>=96&&windspeed<=110
disp('The hurricane category number is 2');
disp('Typical storm surge is 6-8');
elseif windspeed>=111&&windspeed<=130
disp('The hurricane category number is 3');
disp('Typical storm surge is 9-12');
elseif windspeed>=131&&windspeed<=155
disp('The hurricane category number is 4');
disp('Typical storm surge is 13-18');
elseif windspeed>=156
disp('The hurricane category number is 5');
disp('Typical storm surge is 18');
end
Output
This software categorizes Hurricanes on wind speed
Enter the wind speed=77
The hurricane category number is 1
Typical storm surge is 4-5
This software categorizes Hurricanes on wind speed
Enter the wind speed=144
The hurricane category number is 4
Typical storm surge is 13-18
This software categorizes Hurricanes on wind speed
Enter the wind speed=156
The hurricane category number is 5
Typical storm surge is 18
Sol
Algorithm
Mfile
disp('This software shows the classification on heigth of clouds given')
if heigth>0&&heigth<=6499
disp('low')
elseif heigth>=6500&&heigth<=20000
disp('middle')
elseif heigth>20000
disp('high')
end
OUTPUT
Sol
Algorithm
1. Covert switch statements to if elseif
M file
if letter=='x'
disp('Hello');
elseif letter=='y'||letter=='Y'
disp('Yes');
elseif letter=='Q'
disp('Quit');
else
disp('error');
end
Output
From switch to if elseif
Enter a string=
Enter a letter=x
Hello
From switch to if elseif
Please enter a string
Enter a letter=X
error
From switch to if elseif
Please enter a string
Enter a letter=y
Yes
From switch to if elseif
Please enter a string
Enter a letter=Q
Quit
Sol
Sol
Sol
Algorithm
1. Use if else if
2. Algorithm already given in question
Mfile
disp('This software calculate various areas of geometrical objects');
area=menu('Areas','Cylinder','Circle','Rectangle');
if area==1
areac=(2*pi*radiusc*height)+2*pi*(radiusc)^2;
elseif area==2
areacircle=pi*(radiuscircle)^2;
elseif area==3
arearectangle=length*width;
else
disp('Type again');
end
Output
This software calculate various areas of geometrical objects
Enter cylinder height=12
Enter cylinder radius=13
The area of cylinder is2042.035
This software calculate various areas of geometrical objects
Enter circle radius=2
The area of circle is 12.566
This software calculate various areas of geometrical objects
Enter rectangle length=5
Enter rectangle width=5
The area of rectangle is 25.000
Sol
Algorithm
1. Use switch statements instead of elseif
2. Algorithm already given in previous question.
Mfile
disp('This software calculate various areas of geometrical objects using
switch command');
area=menu('Areas','Cylinder','Circle','Rectangle');
switch area
case 1
areac=(2*pi*radiusc*height)+2*pi*(radiusc)^2;
case 2
areacircle=pi*(radiuscircle)^2;
fprintf('The area of circle is%8.3f\n',areacircle);
case 3
arearectangle=length*width;
otherwise
disp('Type again');
end
output
This software calculate various areas of geometrical objects using switch
command
Enter rectangle length=4
Enter rectangle width=7
The area of rectangle is 28.000
This software calculate various areas of geometrical objects using switch
command
Enter circle radius=1
The area of circle is 3.142
This software calculate various areas of geometrical objects using switch
command
Enter cylinder height=12
Enter cylinder radius=13
The area of cylinder is2042.035
Sol
Algorithm
1. Already given in the question
M file
disp('This sofware gives the value of first 3 trogonmetric ratios')
values=menu('Trigonometric ratios','sin(x)','cos(x)','tan)(x)');
if values==1
a=sind(x);
elseif values==2
b=cosd(x);
elseif values==3
c=tand(x);
else
disp('error');
end
output
This sofware gives the value of first 3 trogonmetric ratios
Enter angle in degrees=90
The value of sin(x) is 1.000
This sofware gives the value of first 3 trogonmetric ratios
Enter angle in degrees=180
The value of cos(x) is -1.000
This sofware gives the value of first 3 trogonmetric ratios
Enter angle in degrees=90
The value of tan(x) is Inf
This sofware gives the value of first 3 trogonmetric ratios
Enter angle in degrees=270
The value of cos(x) is 0.000
Sol
Algorithm
Already given in the question
M file
disp('This sofware gives the value of first 3 trogonmetric ratios')
values=menu('Trigonometric ratios','sin(x)','cos(x)','tan)(x)');
switch values
case 1
a=sind(x);
case 2
b=cosd(x);
c=tand(x);
otherwise
disp('error');
end
Output
This sofware gives the value of first 3 trogonmetric ratios
Enter angle in degrees=90
The value of sin(x) is 1.000
This sofware gives the value of first 3 trogonmetric ratios
Enter angle in degrees=90
The value of cos(x) is -0.000
This sofware gives the value of first 3 trogonmetric ratios
Enter angle in degrees=90
The value of tan(x) is Inf
Sol
Algorithm
1.Already given in the question.
Mfile
values=menu('X','ceil','round','sign');
switch values
case 1
a=ceil(x);
case 2
b=round(x);
case 3
c=sign(x);
otherwise
disp('Error');
end
Output
Enter the value of argument=4.5
The value is 5.000
Enter the value of argument=4.3
The value is 4.000
Enter the value of argument=-8
The value is-1.000
Sol
Algorithm
Given in the previous question .Use Ifelseif instead switch.
M file
values=menu('X','ceil','round','sign');
if values==1
a=ceil(x);
elseif values==2
b=round(x);
elseif values==3
c=sign(x);
else
disp('Error');
end
output