1. Write a matlab script/function that takes decimal values until user inputs ze
ID: 3564480 • Letter: 1
Question
1. Write a matlab script/function that takes decimal values until user inputs zero (0). The program will display the highest number among input decimal values and will terminate.
2.Write a matlab script/function that takes decimal values until user inputs zero (0). The program will display the lowest number among input decimal values and will terminate. Assume that user will always provide positive integer values as input.
3.Write a matlab script/function that takes decimal values until user inputs zero (0). The program will display the
Explanation / Answer
clc;clear;close all;
%% Part A
%initialize
max=-10000;
x=input('Enter your number '); %first scan to check condition of while loop
while x~=0 %while loop starts with terminating condition x not exual to 0
%Check all scaned numbers if they are greater then max then change max
%to that number
if x>max
max=x;
end;
x=input('Enter your number '); %continue scaning next number
end
max
%% Part B
clear;
%initialize
min=10000;
x=input('Enter your number ');%first scan to check condition of while loop
while x~=0%while loop starts with terminating condition x not exual to 0
%Check all scaned numbers if they are less then min then change min
%to that number
if x<min
min=x;
end;
x=input('Enter your number ');%continue scaning next number
end
min
%% Part C
clear;
% initialize
max=-1000;
index=-5;
x=input('Enter your number ');%first scan to check condition of while loop
t=[];%to store all the scaned numbers
while x~=0%while loop starts with terminating condition x not exual to 0
%insert all number in t
t=[t,x];
x=input('Enter your number ');
end
[xx,yy]=size(t)% to find the dimension of matrix t and our t is column matrix so yy is total number of element
% Similarly as part A find max in t and make it 0 because we need second
% max and for this we need index of t where max is present.
for i=1:yy
if t(i)>max
max=t(i);
index=i;
end;
end;
%change element of t to 0 where max is present
t(index)=0;
%again initialize
inedx=-1;
max=-10000;
%find max
for i=1:yy
if t(i)>max
max=t(i);
index=i;
end;
end;
%now we got 2nd highest so this is our answer
disp('Second higest number is ');
disp(max);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.