117k views
2 votes
Create a function that performs the following tasks. Create a function called sortArray that creates an array of random integer numbers between 0 - 200. The number of elements in the array will be given by the input parameter called Size. The program should return the sorted array in ascending order. You can not use any built-in Matlab function for the sorting process.Example: o Size = 10 o Random_array = [66, 2, 8, 75, 1, 10, 49, 20, 0, 84] o Output_array = [0, 1, 2, 8, 10, 20, 49, 66, 75, 84]

User Insaner
by
5.4k points

1 Answer

7 votes

Answer:

function sortArray()

Size =input('Enter the array size::');%this line used to take size of the array form the user

Random_array=int8((200-0).*rand(1,Size));%this line generates the random array with 1x5 size in range 0 to 200

disp('Random array:')%this line display's the content Random array

disp(Random_array)%this line display's the random numbers array

for i=1:length(Random_array)%this loop access each element in the array and stores index value in i

for j=(i+1):length(Random_array)%this loop access each element next to the index i and stores index value in j

if Random_array(i) > Random_array(j)%if value at index i greater than value at index j then this condition executed

temp = Random_array(i);%value at index position i store in the temp variable

Random_array(i) = Random_array(j);%we stores the value at index j at index position i

Random_array(j) = temp;%we stores the temp variable at index position in j

end

end

end

disp('Output array')%this displays the string in the in braces

disp(Random_array)%this displays the sorted array

end

sortArray()

Step-by-step explanation:

User Tom Baxter
by
5.4k points