Grade Roster (condition looping through vector elements)

3 次查看(过去 30 天)
Code has already been provided to create a function named assignGrade that defines a vector of integer exam scores to the input variable scores. Add code to the function that uses the values in scores to create a character array (string) named grade with the same number of rows as scores. Each element of the grade should contain a score from the input vector. The elements of grade should have strings that give the corresponding letter grade according to the following grading scale:
  • Score values ranging from 90 to 100 receive an A.
  • Score values ranging from 80 to 89 receive a B.
  • Score values ranging from 70 to 79 receive a C.
  • Score values ranging from 60 to 69 receive a D.
  • Score values ranging from 0 to 59 receive an F.
Note the values in scores are defined as an input to the function. Do not overwrite these values in your code. Be sure to assign values to each element of the function output variable.

回答(1 个)

Parth Trehan
Parth Trehan 2021-4-6
One of the possible methods to approach this problem is by using the concept of vectorization.
I have assumed that the expected output of the provided function is a character array. I hope the snippet provided below helps in this case.
function grade = assignGrade(scores)
grade(scores>=0) = 'F';
grade(scores>=60) = 'D';
grade(scores>=70) = 'C';
grade(scores>=80) = 'B';
grade(scores>=90) = 'A';
end

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by