Undefined function or variable - How can I fix my function?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have written a function to satisfy the following question for my homework:
Write a function called letter_grades that takes a positive integer called score as its input argument and returns a letter grade according to the following scale: A: 91 and above; B: 81-90; C: 71-80; D: 61-70; F: below 61. Remember that to assign a letter to a variable, you need to put it in single quotes, as in: grade = 'A'
I have written the following:
function a = letter_grades(score)
if 91 <= (score) && (score) <= 100
a = A;
elseif 81 <= (score) && score <= 90
a = B;
elseif 71 <= score && score <= 80
a = C;
elseif 61 <= score && score <= 70
a = D;
elseif 0 <= score && score <=60
a = F;
end
However I receive the error:
'Undefined function or variable 'A'.
Error in letter_grades (line 3)
a = A;
What exactly does this mean, and can someone please explain how and why to fix my function?
Thank you
- A new, struggling and unfortunately not innately computer programmer.
1 commentaire
Réponses (1)
Sudhanshu Bhatt
le 29 Oct 2015
Hi Mairi Robertson,
The issue is that you are assigning grades as a variable, and not as a letter as mentioned in the problem statement i.e. your code does a = A. It should be changed to:
a = 'A'.
If A is written without single quotes, MATLAB will treat it as a variable.
Also, the conditional statements can be modified to:
if score>=91
a = 'A'
elseif score >=81
a = 'B'
elseif score >= 71
a = 'c'
elseif score >= 61
a = 'd'
else
a ='F'
Thanks
Sudhanshu Bhatt
0 commentaires
Voir également
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!