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

107 ビュー (過去 30 日間)
function admit=eligible(v,q);
if v>=92 && q>=88;
fprintf('true')
else
fprintf('false')
end
  6 件のコメント
Walter Roberson
Walter Roberson 2023 年 11 月 13 日
eligible(99,88)
ans = logical
0
function admit = eligible(v,q);
avperc = mean([v,q]);
if avperc>= 92 && (v>88&&q>88)
admit = true;
else admit = false;
end
end
@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.

サインインしてコメントする。

回答 (9 件)

VIGNESH B S
VIGNESH B S 2021 年 10 月 13 日
function res = eligible(v,q)
pass = logical(0);
avg = (v+q)/2;
if (avg>= 92 && v >88 && q>88)
pass = logical(1);
end
res = pass;
end

Steven Lord
Steven Lord 2019 年 7 月 11 日
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 件のコメント
nitish Yadav
nitish Yadav 2019 年 9 月 23 日
function admit=eligible(v,q)
if (q+v)/2 >= 92 && (q>88 && v>88)
admit=q&&v;
else
admit=~q&&v;
end
Walter Roberson
Walter Roberson 2019 年 9 月 23 日
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.

サインインしてコメントする。


Aramis
Aramis 2024 年 2 月 5 日
This is the BEST FCKN ANSWER
function admit = eligible(v, q)
admit = mean([v q]) >= 92 && min([v q]) > 88;
end
  1 件のコメント
DGM
DGM 2024 年 2 月 5 日
編集済み: DGM 2024 年 2 月 5 日
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
Jake Armitage 2021 年 4 月 14 日
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
  5 件のコメント
ashokkumar rathinam
ashokkumar rathinam 2021 年 7 月 7 日
May I know the reason for two end statement in the programme sir? is it one end statement enough?
Rik
Rik 2021 年 7 月 7 日
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
Tushar Nagar 2021 年 12 月 13 日
編集済み: DGM 2023 年 3 月 3 日
function admit=eligible(v,q)
avg=(v+q)/2;
rv=false;rq=false;
if v>88 && avg>=92
rv=true;
end
if q>88 && avg>=92
rq=true;
end
admit=rv && rq;
end

Ankit Sharma
Ankit Sharma 2022 年 6 月 14 日
編集済み: DGM 2023 年 3 月 3 日
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;
end
end

Alexandar
Alexandar 2022 年 6 月 28 日
function admit = eligible(v,q)
avg = (v+q)/2;
if avg > 92
admit = true(1);
elseif v >= 88 && q >= 88
admit = false(0);
else admit = 0;
end
I have no clue why this keeps failing. Can somebody please explain why?
  2 件のコメント
Alexandar
Alexandar 2022 年 6 月 28 日
function admit = eligible(v,q)
gre_avg = (v+q)/2;
if (gre_avg >= 92) && (v > 88 && q > 88)
admit = 1;
else
admit = 0;
end
Wait, please check why this code is incorrect. Thank you!
DGM
DGM 2023 年 3 月 3 日
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.
You could cast the given numeric values to logical using logical()
admit = logical(1);
... or you could directly assign the appropriate logical scalar
admit = true;
... or you could realize that the output of a comparison is already a logical value.
function admit = eligible(v,q)
gre_avg = (v+q)/2;
admit = (gre_avg >= 92) && (v > 88 && q > 88);
end

サインインしてコメントする。


Idris jae
Idris jae 2022 年 9 月 25 日
編集済み: DGM 2023 年 3 月 3 日
Thank you for bringing this here. All this while the solution was in the error message '' the variable admit must be a logical data type".
function admit = eligible (v,q)
avg= (v+q)/2;
if (avg>=92) && (v>88 && q>88)
admit=logical(1); %most previous contributions here are thesame except at this point
else
admit=logical(0); %... and here
end
end
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 25 日
移動済み: DGM 2023 年 3 月 3 日
logical(1) can be written as true and logical(0) can be rewritten as false

サインインしてコメントする。


Arah Cristal
Arah Cristal 2022 年 10 月 12 日
function admit=eligible(v,q);
avg = (v+q)/2;
if (avg>=92 && v>88 && q>88);
admit = true
else
admit = false
end
  1 件のコメント
DGM
DGM 2022 年 10 月 12 日
The output of the combined comparisons is a logical value, so there really isn't any need to conditionally assign the output. You already have it.
function admit = eligible(v,q)
avg = (v+q)/2;
admit = avg>=92 && v>88 && q>88;
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by