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
@Mikail this is a correct output for 99, 88, at least according to the question posted at https://www.mathworks.com/matlabcentral/answers/471264-write-a-function-called-eligible-that-helps-the-admission-officer-of-the-graduate-school-decide-whet#comment_723801 where is says that the individual scores must be over 88% . Over 88% means that 88% exactly is not eligible.
This question is locked.
Answers (9)
VIGNESH B S on 13 Oct 2021
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
function res = eligible(v,q)
pass = logical(0);
if (avg>= 92 && v >88 && q>88)
pass = logical(1);
0 Comments
Steven Lord on 11 Jul 2019
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
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
Moeez ur Rehman Qureshi on 12 Jul 2019
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
I have rewritten my function but still it is giving errors
function admit=eligible(v,q);
admit=false
Rik on 12 Jul 2019
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
Where are you getting those numbers from in your code? Is that clear from previous context?
Steven Lord on 13 Jul 2019
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
Reread the question. There are three criteria that the test scores need to satisfy in order for the student to be admitted. You have one of the three correct, one incorrect, and you're missing the third.
Your code rejects the student, but by the criteria in the question they should be admitted.
Chech Joseph on 5 Sep 2019
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
Not sure why it is falling the various input test. Please help.
function [admit] = eligible(v , q)
gre_av = mean([v , q]);
admit = true;
admit = false
if (gre_av >= 92) && (v >= 88 && q >=88);
admit = true;
admit = false;
Walter Roberson on 5 Sep 2019
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
What is the point of assigning true and then false to admit ?
Note that "over 88%" does not include 88% .
nitish Yadav on 23 Sep 2019
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
function admit=eligible(v,q)
if (q+v)/2 >= 92 && (q>88 && v>88)
Walter Roberson on 23 Sep 2019
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
q and v are numeric values. When you use the && operator between them, admit=q&&v is defined as
admit = true;
admit = false;
admit = false;
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.
Aramis on 5 Feb 2024
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
This is the BEST FCKN ANSWER
function admit = eligible(v, q)
admit = mean([v q]) >= 92 && min([v q]) > 88;
1 Comment
DGM on 5 Feb 2024
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
Edited: DGM on 5 Feb 2024
I was skeptical of the bold claim, but I'm pretty sure this is the cleanest out of all four threads. At least it breaks the pattern of bad practices that everyone keeps recycling.
Jake Armitage on 14 Apr 2021
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
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;
5 Comments
Walter Roberson on 14 Apr 2021
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
As far as MATLAB is concerned,
means the same as
which is like
result is false
result is true
result is false
In MATLAB, the compact way to test if scalars v and q are both > 88 would be
but you might as well just use
Jake Armitage on 14 Apr 2021
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
Thank you Walter. I have edited the code but still return incorrect logical for two of the three calls of the function. I'm evidently still missing something.
This is what I currently have:
function admit = eligible(v,q)
if mean([v,q])>=92 && v>88 && q>88
admit = true;
admit = false;
Walter Roberson on 14 Apr 2021
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
admit = eligible(96,89)
admit = logical
That is correct, the mean is 92.5 and each entry is more than 88
admit = eligible(88,99)
admit = logical
That is correct, the mean is 93.5 but the first value, 88, is not more than 88
admit = eligible(92,91)
admit = logical
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;
admit = false;
ashokkumar rathinam on 7 Jul 2021
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
May I know the reason for two end statement in the programme sir? is it one end statement enough?
Rik on 7 Jul 2021
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
One closes the if, the other closes the program.
While using an end to close the program is optional, it is recommended that you do. If functions are explicitly closed with an end, you can use nested functions and you can define local functions in a script.
Tushar Nagar on 13 Dec 2021
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
Edited: DGM on 3 Mar 2023
function admit=eligible(v,q)
rv=false;rq=false;
if v>88 && avg>=92
if q>88 && avg>=92
1 Comment
DGM on 3 Mar 2023
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
(A & B) & (C & B) is the same as A & B & C
Ankit Sharma on 14 Jun 2022
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
Edited: DGM on 3 Mar 2023
creating a function to get the results
function admit = eligible(v,q) % taking input as question required
avg = (v+q)/2; % we have to find the average so we can compare it with required variables
if (v>88 && avg>=92) && (q>88 && avg>=92) % condition check (logically) wheather v is greater
% then 88 and average is greater than 92 and same we are going
% to check logically for q and submitting the result in admit
admit = true;
else % if above conndition not satisfied then running else function and return false if condition not satisfied
admit = false;
0 Comments
Alexandar on 28 Jun 2022
Direct link to this answer
Cancel Copy to Clipboard
Direct link to this answer
Cancel Copy to Clipboard
function admit = eligible(v,q)
if avg > 92
admit = true(1);
elseif v >= 88 && q >= 88
admit = false(0);
else admit = 0;
I have no clue why this keeps failing. Can somebody please explain why?
2 Comments
Alexandar on 28 Jun 2022
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
function admit = eligible(v,q)
gre_avg = (v+q)/2;
if (gre_avg >= 92) && (v > 88 && q > 88)
Wait, please check why this code is incorrect. Thank you!
DGM on 3 Mar 2023
Direct link to this comment
Cancel Copy to Clipboard
Direct link to this comment
Cancel Copy to Clipboard
The output needs to be of class 'logical'. In this example, the outputs are 1 or 0 -- which are numeric.
The functions false() and true() are functions used to create constant-valued logical arrays, much like zeros() and ones() are used to create constant-valued numeric arrays. In the first example, true(1) creates a logical scalar, whereas false(0) creates an empty logical array.