Azzera filtri
Azzera filtri

Undefined function or variable - How can I fix my function?

2 visualizzazioni (ultimi 30 giorni)
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 Commento
dpb
dpb il 27 Ott 2015
Reread the last sentence in the problem again.
The error is closely related.

Accedi per commentare.

Risposte (1)

Sudhanshu Bhatt
Sudhanshu Bhatt il 29 Ott 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

Categorie

Scopri di più su Introduction to Installation and Licensing in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by