using MATLAB only using the code below, complete the question. please show all d
ID: 3884265 • Letter: U
Question
using MATLAB only
using the code below, complete the question. please show all details and proof
% Input the number of elements to be sorted num=input ('Enter the number of numbers to be sorted: % Create an array arr= [ ] ; % Input numbers to the array for i-1:num % Input the numbers arr (i)-input ('Enter the number ' end % For each pass for j-1:1:num-1 $Compare adjacent elements for i-1:1: num-1 % If adjacent elements in wrong order if arr(i) arr (i+1) % Swap the elements tem-arr (i) arr (i)-arr (i+1); arr ( i +1) =ten; end end end % Output the array fprintf ('The elements in ascending order given below:n') disp (arr)Explanation / Answer
stringorder(s1,s2) function returns true if s1 and s2 are not in alphabetical order. Other wise it returns false
Create a file stringorder.m and pasted given code into it. [Filename must be stringorder.m]
stringorder.m
function [flag] = stringorder(s1,s2)
if(strcmp(s1,s2) == true)
flag = false;
elseif(size(s1,2) >= size(s2,2))
for i = 1:size(s2,2)
flag = true;
if(s1(1,i) < s2(1,i))
flag = false;
break
elseif(s1(1,i) > s2(1,i))
flag = true;
break
end
end
else
for i = 1:size(s1,2)
flag = false;
if(s1(1,i) < s2(1,i))
flag = false;
break
elseif(s1(1,i) > s2(1,i))
flag = true;
break
end
end
end
end
bubble2.m
clc;
clear all;
n = input('Enter number of strings to be sorted:');
arr = cell(1,n);
for i = 1:n
arr(1,i) = input('Enter the string! ');
end
for j = 1:1:n-1
for i = 1:1:n-1
if( stringorder(arr{i},arr{i+1} ) )
tmp = arr{i};
arr{i} = arr{i+1};
arr{i+1} = tmp;
end
end
end
fprintf('Names in sorted order are! ');
arr
Sample Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.