write a matlab code that generates a one dimensional array o
write a matlab code that generates a one dimensional array of dimension 20 whose elements aer random numbers between (-10:10) then write a code that sorts these numbers from the smallest to the largest and from largest to the smallest using while if and for commands
Solution
clc
% A= required random array
A = -10 + 20 * rand(10,1)
% Number of entries, n
n=length(A);
% making (n-1) passes
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if A(i)>A(i+1);
temp=A(i);
A(i)=A(i+1);
A(i+1)=temp;
end
end
end
disp(‘ ‘)
disp (‘OUTPUT’)
disp (‘The ascending matrix is’)
disp(A)
