Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

how can i determine why my code is accepting a non integer when it's not suppose to

1 vue (au cours des 30 derniers jours)
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola le 11 Juin 2016
Clôturé : MATLAB Answer Bot le 20 Août 2021
I'm solving problem that is supposed to only accept an integer as input but its accepting a non integer. I'm attaching the problem and the code i wrote . please help me check what could be wrong thanks

Réponses (2)

Roger Stafford
Roger Stafford le 11 Juin 2016
Modifié(e) : Roger Stafford le 11 Juin 2016
Your instructions specified that if the input is invalid, you were to return an empty string. This you did not do. Instead your code proceeds just as if the input is valid. When you do an ‘if’ operation terminated by ‘end’, the code proceeds from the point after the ‘end’ unless a ‘break’ or 'continue' has been called. There is no ‘break’ or 'continue' in your code.
Also the test for integer is wrong, in any case. What ‘isinteger’ tests for is whether the input is of TYPE integer which is a different thing from a floating point number being an integer.
In addition you were supposed to test that the input integer does not exceed 3000, otherwise it is invalid.
  4 commentaires
OLUBUKOLA ogunsola
OLUBUKOLA ogunsola le 12 Juin 2016
this is what it returned : Operands to the and && operators must be convertible to logical scalar values.
Roger Stafford
Roger Stafford le 13 Juin 2016
You can only use the "short-circuit" logical operators on scalars, not vectors. Use | and & instead for vectors.

Image Analyst
Image Analyst le 11 Juin 2016
Try something like this:
if y - int32(y) == 0
% It's an integer.
else
% It's not an integer
message = sprintf('%f is not an integer', y);
uiwait(errordlg(message));
cent = []; % Return null.
return;
end
  6 commentaires
Image Analyst
Image Analyst le 12 Juin 2016
Your directions were not clear. With 2 numbers, you could have returned 2 numbers, or you could have returned null or some other error condition. Apparently your robo-grader wanted you to just return null if you passed it two numbers because that's what you did and it accepted it.

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by