Clear Filters
Clear Filters

Grade Roster (condition looping through vector elements)

2 views (last 30 days)
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.

Answers (1)

Parth Trehan
Parth Trehan on 6 Apr 2021
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

Community Treasure Hunt

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

Start Hunting!