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

조회 수: 2 (최근 30일)
Mairi Robertson
Mairi Robertson 2015년 10월 27일
답변: Sudhanshu Bhatt 2015년 10월 29일
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
dpb
dpb 2015년 10월 27일
Reread the last sentence in the problem again.
The error is closely related.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sudhanshu Bhatt
Sudhanshu Bhatt 2015년 10월 29일
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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by