Hi I am trying to submit my homework problem, but the program that I have to use to check my code keeps saying it is wrong. I can't seem to figure out what is wrong with it. It is supposed to return a harmonic mean value as the output.

1 view (last 30 days)
function m = h_mean(x)
% Write your code here.
if (isvector(x)== 1 && length(x) > 1)
n=length(x);
m=(n./(1./x(1))+(1./x(2))+(1./x(3))+(1./x(4))+(1./x(5))+(1./x(6))+(1./x(7))+(1./x(8))+(1./x(9))+(1./x(10)))
else
n=1
m=(n/(1./x))
end
  3 Comments
Tushar Sinha
Tushar Sinha on 3 Nov 2015
Hi Sigourney,
I tried reproducing the error with your code but it worked fine for me and gave me the value of the harmonic mean. Can you tell me what is the exact error message you are seeing? Also, what is the "x" value that you passed to the "h_mean" function?
Thanks,
Tushar
Walter Roberson
Walter Roberson on 3 Nov 2015
Assignments often specify what to do when there are "edge cases" like the input being empty or the wrong type, or consisting only of 0. We need to see the text of the assignment to figure out why the grading software might say that the code is wrong.
For example, to me the code appears to be obviously wrong in most cases, but if the assignment only requires the small number of cases it works for, then it might be fine.

Sign in to comment.

Answers (1)

the cyclist
the cyclist on 3 Nov 2015
Here is a hint, related to Walter's comment:
Your program won't give the correct value for the vector
x = [1 2 3]
  1 Comment
Walter Roberson
Walter Roberson on 3 Nov 2015
Well we don't know if it gives the correct answer, since we have not seen the assignment wording. It might be ticky-boo except that maybe in that case the answer has to be returned as single precision. We don't know. Graders grade against the exact assignment, and we have not seen that.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!