MATLAB program which will take input a charater ‘a’ value from user. You have to use switch statement to decide if value of a is ‘t’ then you have to call table function, if value of a is ‘f’ then call factorial function, if value of a is ‘p’ then call prime function, if value of a is ‘s’ then call search function.
Posted in matlab
No comments
Friday, August 1, 2014 By Anonymous
Write
a MATLAB program which will take input a charater ‘a’ value from user. You have to use switch statement to decide if
value of a is ‘t’ then you have to call table function, if value of a is ‘f’
then call factorial function, if value of a is ‘p’ then call prime function, if
value of a is ‘s’ then call search function.
You
have to write four functions in your program;
Table(n1,n2)
Factorial(n3)
Prime(n4)
Search(char n5[], char c, char choice)
Table function will print the table of n1 from 1 to n2.
Factorial function will print the factorial of n3 if n3 is multiple of 2.
Prime function will tell that n4 is prime or composite.
Search function will take first argument n5 as an array of characters and
second element a character to be search in the array and third element a
character to decide which searching algorithm to be used.i.e. if user has
passed the value of c as ‘s’ then Search function will perform the sequential
search but if value of c is something else then Search function will perform
binary search.
Structure of your program will be like
this. You have to make it exactly working.
Switch(a)
{
case ‘f’:
factorial();
break;
case ‘p’:
prime();
break;
case ‘t’:
table();
break;
case ‘s’:
search();
break;
}
Solution:
function
QUETION5()
%Start the function
disp('[t] TABLE'); %Display the Menu
disp('[f] FACTORIAL');
disp('[p] PRIME');
disp('[s] SEARCH');
c=input('ENTER CHOICE: ','s'); %Choice input
switch (c) %Switch cases check
case 't'
n1=input('ENTER NO: '); %input from user
n2=input('ENTER LIMIT: ');
TABLE(n1,n2); %Call table function
case 'f'
n3=input('ENTER THE NO: '); %input
from user
FACTORIAL(n3); %Call factorial function
case 'p'
n4=input('ENTER THE NO: '); %input from user
PRIME(n4); %Call prime function
case 's'
n=input('ENTER NO OF ELEMENTS:
'); %input from user
c=input('ENTER ELEMENT TO SEARCH:
'); %input from user
for i=1:n
value=input('ENTER VALUE:
'); %Input Array from user
n5(i)=value;
end
disp('[s] SEQUENTIAL'); %Choice
to which algo apply
disp('[b] BINARY');
ch=input('ENTER CHOICE: ','s');
SEARCH( n5, c , ch);
otherwise
disp('SELECT ANOTHER ONE');
end
end
function
TABLE(n1,n2) %Table function
for n=1:n2
disp([num2str(n1),'*',num2str(n),'=',num2str(n1*n)]);
%Print table
end
end
function
FACTORIAL(n3)
%Factorial function
if(mod(n3,2)==0) %Checking factorial condition
fact=1;
for i=n3:-1:1
fact=fact*i; %Calculate factorial
end
disp(fact); %Printing factorial
else
disp('ERROR!!!');
end
end
function
PRIME(n4)
%Prime function
if(mod(n4,n4)==0
&& mod(n4,2)~=0)
%if(isprime(n4)==1)
disp('YES PRIME');
else
disp('NOT PRIME');
end
end
function
SEARCH( n5, c , ch) %Search function
count=0;
if(ch=='s') %Giving Condition for sequential search
for i=1:length(n5) %Using Sequential search
algorithm
if(c==n5(i))
count=count+1;
end
end
end
if(count==0)
disp('NOT FOUND !!!');
else
disp('FOUND');
end
if(ch=='b') %Condition for Binary
search is used here
low=1; %Use of Binary search Algorithm
high=length(n5);
index=0;
while
low<=high && index==0
mid=floor((low+high)/2);
if n5(mid)==c
index=mid;
disp(['FOUND AT INDEX :',num2str(index)]);
elseif c<n5(mid)
high=mid-1;
else
low=mid+1;
end
end
end
end
Related posts
Share this post
0 comments: