Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Begin this problem by retrieving your own individual MATLAB header. The C

ID: 3810093 • Letter: M

Question

MATLAB

Begin this problem by retrieving your own individual MATLAB header. The C program shown on the next page implements a common algorithm for sorting a one-dimensional array of floating-point numbers. Your task in this problem is to replicate the functionality of this program using MATLAB. More specifically, you must write a MATLAB script that does the following: Requests the number of elements in the array, with a maximum number of 20 elements. Notice that the C program does not check to see if the inputted number of elements exceeds this threshold; your MATLAB script should implement this error check and terminate gracefully (after providing a relevant error message) if the threshold is exceeded. Requests input of the elements of the array, one at a time. Sorts the elements of the array in ascending order. Your MATLAB script must employ the same algorithm as is used in the C program; however, it is not necessary to implement the algorithm in MATLAB via pointers. Prints out the sorted array, one element at a time, to the screen. You do not need to implement any error checking other than what is indicated in the bullet points above. Sample execution of this program is shown below (user inputs in bold): Enter the number of terms in the array (max = 20) Enter the elements of the array: The sorted array is:

Explanation / Answer

%matlab code

size = input('Enter the number of terms in the array (max = 20): ');
if size > 20
disp('Size cannot be greate than 20, program terminates!');
quit
end

v = [];
disp('Enter elements of the array:');
for i=1:size
v(i) = input("");
end

for i=1:size
for j=1:size
if v(i) < v(j)
t = v(i);
v(i) = v(j);
v(j) = t;
end
end
end

disp('the sorted array is :');
for i=1:size
disp(v(i));
end


%{
output:
Enter the number of terms in the array (max = 20): 21
Size cannot be greate than 20, program terminates!

Enter the number of terms in the array (max = 20): 5
Enter elements of the array:
-4.5
3
4
-3.2
1
the sorted array is :
-4.5000
-3.2000
1
3
4

%}