MPPPS Chapter3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

Chapter 3

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;

fprintf('The next hour will be %d\n',hour);

else
hour=hour+1;
fprintf('The next hour will be %d\n',hour);
end

Output
12 hour clock

Enter a hour=3

The next hour will be 4


12 hour clock

Enter a hour=12

The next hour will be 1

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;

fprintf('The volume is %0.3f\n',volume);

Output
This program will calculate the volume of pyramid

The units are in inches

Enter a length=9

Enter a width=4

Entera heigth=8

The volume is 96.000

Sol

Algorithm

1. User enters a string.


2. Display the output according to input.

Mfile
letter=input('Are you an engineer(Y/N)=','s');

if letter=='Y'||letter=='y'

disp('Ok, God bless you');

else

disp('Sorry,We can,t do any thing');


end

Output

Are you an engineer(Y/N)=y


Ok, God bless you
Are you an engineer(Y/N)=n
Sorry,We can,t do any thing

Sol

Sol

Algorithm

1. User enters numerator.


2. User enters denominator
3. Use nested if else
4. If numerator or denominator is zero ,show the result.
5. Else show the fraction.

M file

numerator=input('Enter numerator=');

if numerator==0

disp(' Numerator is zero so fraction is zero'); %Nested if else


else
denominator=input('Enter denominator=');
if denominator==0

disp('Denominator is zero So fraction is at infinity');

else

fraction=(numerator/denominator);
fprintf('The fraction is %f\n',fraction);
end
end

Output
Enter numerator=9

Enter denominator=8

The fraction is 1.125000

Enter numerator=0

Numerator is zero so fraction is zero

Enter numerator=8

Enter denominator=0

Denominator is zero So fraction is at infinity


Sol

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

systolic=input('Enter the systolic blood pressure=');

diastolic=input('Enter the diastolic blood pressure=');

if systolic<120

disp('You are a candidate for experiment'); %Nested if else

if diastolic<80

disp('You are a candidate for experiment');


end

else

fraction=(systolic)/(diastolic);

fprintf('Your are not candidate for experiment%f\n ',fraction)

end

Output

Enter the systolic blood pressure=121

Enter the diastolic blood pressure=78

Your are not candidate for experiment1.551282

Enter the systolic blood pressure=117


Enter the diastolic blood pressure=76

You are a candidate for experiment

You are a candidate for experiment

Enter the systolic blood pressure=123

Enter the diastolic blood pressure=85

Your are not candidate for experiment1.447059

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

disp('This software uses continuity equation to compare velocity at in and


outlet')

A1=input('Enter the area at inlet=');


A2=input('Enter the area at outlet=');

a=(A1/A2);

V1=input('Enter the velocity at inlet=');

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

disp('Velocity at outlet is greater than inlet');

end

OUTPUT

This software uses continuity equation to compare velocity at in and outlet

Enter the area at inlet=100

Enter the area at outlet=94

Enter the velocity at inlet=25

Velocity at outlet is greater than inlet

This software uses continuity equation to compare velocity at in and outlet

Enter the area at inlet=94

Enter the area at outlet=100

Enter the velocity at inlet=25

Velocity at outlet is less than inlet

This software uses continuity equation to compare velocity at in and outlet

Enter the area at inlet=100

Enter the area at outlet=100


Enter the velocity at inlet=25

Velocity is same at inlet and outlet

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')

m=input('Enter a value of m=');

n=input('Enter a value of 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

Enter a value of m=8

Enter a value of n=5

8 7 6 5 >>

This software return a vector from m to n

Enter a value of m=6

Enter a value of n=6


6

This software return a vector from m to n

Enter a value of m=4

Enter a value of n=5

4 5 >>

Algorithm
Already in the question

Mfile

letter=input('Enter your answer=','s');

if letter=='Y'||letter=='y'

disp('Ok continuing');

else if letter=='N'||letter=='n'

disp('Ok, halting');

else
disp('Error');

end
end
OUTPUT

Enter your answer=y


Ok continuing
Enter your answer=Y
Ok continuing
Enter your answer=n
Ok, halting
Enter your answer=N
Ok, halting
Enter your answer=b
Error

Sol

Algorithm
Already in question before

M file

letter=input('Enter your answer','s');

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');

disp('The speed of sound is 314m/s.');

vsound=314;

vaeroplane=input('Enter the speed of aeroplane=');


machno=(vaeroplane/vsound) ;

if machno>1
disp('The flow is supersonic');

elseif machno<1

disp('The flow is subsonic');

else

disp('The flow is transonic');

end

OUTPUT
This software calculates and shows the condition of Mach number

The speed of sound is 314m/s.

Enter the speed of aeroplane=315

The flow is supersonic

This software calculates and shows the condition of Mach number

The speed of sound is 314m/s.

Enter the speed of aeroplane=313

The flow is subsonic

This software calculates and shows the condition of Mach number

The speed of sound is 314m/s.

Enter the speed of aeroplane=314

The flow is transonic


Sol

Algorithm

1. Display software use


2. User enter temperature in Celsius
3. User enters the choice of temperature conversion.
4. Make the logic.
5. Use if and elseif command
6. Display the desired output.

M file
celsius=input('Enter temperature in celsius :');

fk=input('Do you want F or K ?','s');

F=9/5*(celsius)+32;

K=celsius+273.15;
if fk=='f'||fk=='F'

fprintf('The temperature in fahrenheit is%8.3f\n',F);

elseif fk=='k'||fk=='K'

fprintf('The temperature in kelvin is %8.3f\n',K);

end

Output

This software converts from celsius to fahrenheit and kelvin


Enter temperature in celsius :100
Do you want F or K ?f
The temperature in fahrenheit is 212.000
This software converts from celsius to fahrenheit and kelvin
Enter temperature in celsius :100
Do you want F or K ?k
The temperature in kelvin is 373.150

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

disp('Entered integers should make a pythagorean triple')


a=input('Enter first integera=');

b=input('Enter second integerb=');

c=input('Enter third integerc=');

if (a^2+b^2)==c^2

disp('1')

else

disp('0')

end

Output

Entered integers should make a pythagorean triple


Enter first integera=3
Enter second integerb=4
Enter third integerc=5
1
Entered integers should make a pythagorean triple
Enter first integera=5
Enter second integerb=5
Enter third integerc=6
0

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

disp('This software depicts the condition of a storm');

windspeed=input('Enter windspeed in milesperhour=');

if windspeed<38

disp('It is a tropical depression');

elseif (windspeed>=39)&&(windspeed<=73)

disp('It is a tropical storm');

elseif windspeed>=74

disp('it is a hurricane');

end
Output

This software depicts the condition of a storm


Enter windspeed in milesperhour=23
It is a tropical depression
This software depicts the condition of a storm
Enter windspeed in milesperhour=56
It is a tropical storm
This software depicts the condition of a storm
Enter windspeed in milesperhour=76
it is a hurricane

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');

windspeed=input('Enter the wind speed=');

if windspeed>=74&&windspeed<=95

disp('The hurricane category number is 1');


disp('Typical storm surge is 4-5');

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

1.Display software use


2.User enters height of clouds
3.Use if ifelse.
4.Display the output .

Mfile
disp('This software shows the classification on heigth of clouds given')

heigth=input('Enter height of clouds in feet=');

if heigth>0&&heigth<=6499

disp('low')

elseif heigth>=6500&&heigth<=20000

disp('middle')

elseif heigth>20000
disp('high')

end

OUTPUT

This software shows the classification on heigth of clouds given


Enter height of clouds in feet=6700
middle
This software shows the classification on heigth of clouds given
Enter height of clouds in feet=4500
low
This software shows the classification on heigth of clouds given
Enter height of clouds in feet=2001
low
This software shows the classification on heigth of clouds given
Enter height of clouds in feet=20001
High

Sol

Algorithm
1. Covert switch statements to if elseif

M file

disp('From switch to if elseif')


disp('Please enter a string')
letter=input('Enter a letter=','s');

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

height=input('Enter cylinder height=');


radiusc=input('Enter cylinder radius=');

areac=(2*pi*radiusc*height)+2*pi*(radiusc)^2;

fprintf('The area of cylinder is%8.3f\n',areac);

elseif area==2

radiuscircle=input('Enter circle radius=');

areacircle=pi*(radiuscircle)^2;

fprintf('The area of circle is%8.3f\n',areacircle);

elseif area==3

length=input('Enter rectangle length=');

width=input('Enter rectangle width=');

arearectangle=length*width;

fprintf('The area of rectangle is%8.3f\n',arearectangle);

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

height=input('Enter cylinder height=');


radiusc=input('Enter cylinder radius=');

areac=(2*pi*radiusc*height)+2*pi*(radiusc)^2;

fprintf('The area of cylinder is%8.3f\n',areac);

case 2

radiuscircle=input('Enter circle radius=');

areacircle=pi*(radiuscircle)^2;
fprintf('The area of circle is%8.3f\n',areacircle);

case 3

length=input('Enter rectangle length=');

width=input('Enter rectangle width=');

arearectangle=length*width;

fprintf('The area of rectangle is%8.3f\n',arearectangle);

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)');

x=input('Enter angle in degrees=');

if values==1

a=sind(x);

fprintf('The value of sin(x) is%8.3f\n',a);

elseif values==2

b=cosd(x);

fprintf('The value of cos(x) is%8.3f\n',b);

elseif values==3

c=tand(x);

fprintf('The value of tan(x) is%8.3f\n',c);

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)');

x=input('Enter angle in degrees=');

switch values

case 1

a=sind(x);

fprintf('The value of sin(x) is%8.3f\n',a);

case 2

b=cosd(x);

fprintf('The value of cos(x) is%8.3f\n',b);


case 3

c=tand(x);

fprintf('The value of tan(x) is%8.3f\n',c);

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');

x=input('Enter the value of argument=');

switch values
case 1

a=ceil(x);

fprintf('The value is%6.3f\n',a);

case 2

b=round(x);

fprintf('The value is%6.3f\n',b);

case 3

c=sign(x);

fprintf('The value is%6.3f\n',c);

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');

x=input('Enter the value of argument=');

if values==1

a=ceil(x);

fprintf('The value is%6.3f\n',a);

elseif values==2

b=round(x);

fprintf('The value is%6.3f\n',b);

elseif values==3

c=sign(x);

fprintf('The value is%6.3f\n',c);

else
disp('Error');

end

output

Enter the value of argument=3.6


The value is 4.000
Enter the value of argument=4.3
The value is 4.000
Enter the value of argument=-7
The value is-1.000
Matlab
>> 4>3+1
ans =
0
>> 'e'=='d'+1
ans =
1
>> 3<9-2
ans =
1
>> (3<9)-2
ans =
-1
>> 4==3+1&&'d'>'c'
ans =
1
>> 3>=2||'x'=='y'
ans =
1
>> xor(3>=2,'x'=='y')
ans =
1
>> xor(3>=2,'x'~='y')
ans =
0

You might also like