Is this working right

4 views (last 30 days)
Radoslav Gagov
Radoslav Gagov on 13 Mar 2017
Commented: zehra ülgen on 18 Oct 2020
Hey guys. I am trying to do this Assignment and i am pretty sure its correct but it keeps saing its now when i run the emulator that checks it.
Here is the assignemnt:
Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output arguments in nondecreasing order, i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element. NOTE: Your function may not use any built-in functions, e.g., sort, min, max, median, etc.
my code
function [a,b,c] = sort3(v)
if v(1) >= v(2) && v(2) >= v(3)
a = v(1); b = v(2); c = v(3);
elseif v(1) >= v(3) && v(3) >= v(2)
a = v(1); b = v(3); c = v(2);
elseif v(2) >= v(1) && v(1) >= v(3)
a = v(2); b = v(1); c = v(3);
elseif v(2) >= v(3) && v(3) >= v(1)
a = v(2); b = v(3); c = v(1);
elseif v(3) >= v(1) && v(1) >= v(2)
a = v(3); b = v(1); c = v(2);
elseif v(3) >= v(2) && v(2) >= v(1)
a = v(3); b = v(2); c = v(1);
end
end

Answers (8)

Jan
Jan on 13 Mar 2017
Edited: Jan on 26 Jul 2018
This can be written shorter with less chances for typos:
function [a, b, c] = sort3(v)
a = v(1);
b = v(2);
c = v(3);
if a > b, [a, b] = swap(a, b); end
if b > c, [b, c] = swap(b, c); end
if a > b, [a, b] = swap(a, b); end
end
function [b, a] = swap(a, b) % empty function body
end
Prefer one command per line in real code. I've move the if block to single lines only to emphasize the pattern optically here.

Adam
Adam on 13 Mar 2017
The question says to return them in 'non-decreasing' order. I ran your function with [1 2 3] and it returns 3, 2, 1 so you may just have your logic the wrong way round.

Radoslav Gagov
Radoslav Gagov on 13 Mar 2017
O right :D Thank you.

Steven Lord
Steven Lord on 13 Mar 2017
One way to increase the confidence that your code is doing what you expect is to pass in test cases for which you know the correct answer and check that you get the output you expected. Adam's test case of [1 2 3] is one example. Others include:
  • [2 3 1] (the input is not sorted already)
  • [1 2 1] (duplicate elements)
  • [3 3 3] (all the elements are duplicates)
  • [-1 0 -2] (negative numbers and zero)
  • [exp(1) pi sqrt(2)] (non-integer values)

Walter Fanka
Walter Fanka on 23 Oct 2017
Edited: Walter Roberson on 17 Oct 2020
function [s1,s2,s3] = sort3(v_3)
a = v_3(1); b = v_3(2); c = v_3(3);
if a <= b && a <= c s1 = a; if b <= c s2 = b; s3 = c; else s2 = c; s3 = b; end end
if b < a && b <= c s1 = b; if a <= c s2 = a; s3 = c; else s2 = c; s3 = a; end end
if c < a && c < b s1 = c; if a < b s2 = a; s3 = b; else s2 = b; s3 = a; end end
end
  1 Comment
Jan
Jan on 24 Oct 2017
Something must be unnecessary here: If the first two conditions "a <= b && a <= c" and "b < a && b <= c" have been rejected before, it cannot be required to check for the third "c < a && c < b": If this third condition would be false, s1, s2, s3 would be undefined and the function would crash. So either this third condition is not needed, or the code fails for some input.
Do you see the bunch of warnings in the editor? Insert commas to calm down the MLint code checker:
if a <= b && a <= c, s1 = a; if b <= c, s2 = b; s3 = c; else, s2 = c; s3 = b; end, end
% ^ ^ ^ ^
Or better use line breaks:
if a <= b && a <= c
s1 = a;
if b <= c
s2 = b;
s3 = c;
else
s2 = c;
s3 = b;
end
end
I know, I have used multiple command per line also, but this was a bad example.

Sign in to comment.


Vignesh M
Vignesh M on 4 May 2018
Edited: Walter Roberson on 17 Oct 2020
The question says to return them in 'non-decreasing' order. Your function is for decreasing order.
function [u1,u2,u3] = sort3(v)
if v(1) <= v(2) && v(2) <= v(3);
u1=v(1);u2=v(2);u3=v(3);
elseif v(1) <= v(3) && v(3) <= v(2);
u1=v(1);u2=v(3);u3=v(2);
elseif v(2) <= v(1) && v(1) <= v(3);
u1=v(2);u2=v(1);u3=v(3);
elseif v(2) <= v(3) && v(3) <= v(1);
u1=v(2);u2=v(3);u3=v(1);
elseif v(3) <= v(1) && v(1) <= v(2);
u1=v(3);u2=v(1);u3=v(2);
else v(3) <= v(2) && v(2) <= v(1);
u1=v(3);u2=v(2);u3=v(1);
end
  2 Comments
Heirleking
Heirleking on 25 Jul 2018
Are the commas after the if required? I had the same as you with different letters, when I erased them the simulator worked perfectly
Jan
Jan on 26 Jul 2018
Edited: Jan on 18 Oct 2020
@David Silva: There are no commas, but semicolons. They are not required. If you are in doubt, remove them and see what happens.
As described in a comment above, commas help, if you append the body of the if branch to the same line:
if a==b disp('match') end % ?!? Confusing!
if a==b, disp('match'); end % Better! Less confusion.
if a==b % Best! This is purely clear
disp('match')
end

Sign in to comment.


Mohamed Ahmed Khedr
Mohamed Ahmed Khedr on 14 Oct 2018
Your code working in a good way if you just make the following edit>>> change the arrangement of outputs
function [c,b,a] = sort3(v)

zehra ülgen
zehra ülgen on 17 Oct 2020
Edited: Jan on 18 Oct 2020
function A = sort3(x,y,z)
if x < y && x < z % x is the smallest one
if y < z
A = [x y z];
else
A = [x z y];
end
elseif y < x && y < z % y is the smallest one
if x < z
A = [y x z];
else
A = [y z x];
end
elseif z < x && z < y % z is the smallest one
if x < y
A = [z x y];
else
A = [z y x];
end
end
  2 Comments
Jan
Jan on 18 Oct 2020
The original question has a vector as input and wants 3 scalars as output.
Your function fails if two inputs are equal.
zehra ülgen
zehra ülgen on 18 Oct 2020
Sorry, you right! I couldn't realize that these questions are not same.
"Write a function called sort3 that takes three unequal scalar arguments (the function does not have to check the format of the input or the inequality of the arguments). It uses if-statements, possibly nested, to return the three values of these arguments in a single row vector in increasing order, i.e., element one of the output vector equals the smallest input argument and element three of the output vector equals the largest input argument. NOTE: The function may not use the built-in function sort."

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!