MATLAB Answers

Write a function called eligible that helps the admission officer of the Graduate School decide whether the applicant is eligible for admission based on GRE scores. The function takes two positive scalars called v and q as in

368 views (last 30 days)
Moeez ur Rehman Qureshi
Moeez ur Rehman Qureshi on 11 Jul 2019
Commented: Walter Roberson on 14 Apr 2021 at 19:18
function admit=eligible(v,q);
if v>=92 && q>=88;
fprintf('true')
else
fprintf('false')
end
  3 Comments

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 11 Jul 2019
Nowhere in your function do you define the variable admit the function returns as its output. You haven't shown the full text of the homework question but I suspect it tells you to have your function return true or false instead of printing the text "true" or "false". To do that assign a value to the variable.
admit = true;
admit = false;
  7 Comments
Walter Roberson
Walter Roberson on 23 Sep 2019
q and v are numeric values. When you use the && operator between them, admit=q&&v is defined as
if q ~= 0
if v ~= 0
admit = true;
else
admit = false;
end
else
admit = false;
end
However, the assignment makes no mention of testing for zero or not.
In particular when you get to your else branch, admit=~q&&v then that would be true if q was 0 and v was non-zero, leading you to admit someone with a score of 0 for q when the assignment would require that they be rejected because 0>88 is false.

Sign in to comment.


Jake Armitage
Jake Armitage on 14 Apr 2021 at 0:11
After enough struggle I am wondering who can help me understand why I'm returning errors from this program. Thanks
function admit = eligible(v,q)
avperc = mean([v,q]);
if avperc>= 92 && (v&&q>88)
admit = true;
else admit = false;
end
end
  3 Comments
Walter Roberson
Walter Roberson on 14 Apr 2021 at 19:18
admit = eligible(96,89)
admit = logical
1
That is correct, the mean is 92.5 and each entry is more than 88
admit = eligible(88,99)
admit = logical
0
That is correct, the mean is 93.5 but the first value, 88, is not more than 88
admit = eligible(92,91)
admit = logical
0
This is correct, the mean is less than 92
So at the moment I do not see any problems with the code
function admit = eligible(v,q)
if mean([v,q])>=92 && v>88 && q>88
admit = true;
else
admit = false;
end
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!