Filter löschen
Filter löschen

I cannot get my code to output the letter grade as an array. Please help?

3 Ansichten (letzte 30 Tage)
Kyle Skelly
Kyle Skelly am 11 Apr. 2016
Kommentiert: Kyle Skelly am 11 Apr. 2016
How do I go about getting the code to output the letters of the grades as an array?
grades = [82, 90, 75, 94, 88, 99, 45, 90]
for a=1 : numel(grades)
if grades(a) <= 60
disp( sprintf( '%d = F', grades(a) ) )
elseif grades(a) >=60 && grades<70
disp( sprintf( '%d = D', grades(a) ) )
elseif grades(a) >=70 && grades<80
disp( sprintf( '%d = C', grades(a) ) )
elseif grades(a) >=80 && grades<90
disp( sprintf( '%d = B', grades(a) ) )
else grades(a) >=90
disp( sprintf( '%d = A', grades(a) ) )
end
end
Thanks in advance!

Antworten (1)

Kirby Fears
Kirby Fears am 11 Apr. 2016
Bearbeitet: Kirby Fears am 11 Apr. 2016
You can use a character array to store the letter grades for each score in order.
% Here are some grades
scores = [82, 90, 75, 94, 88, 99, 45, 90];
% Set up the grading scale
gradeScale = 'ABCDF';
gradeScaleMinScore = [90,80,70,60,0];
% Identify grade for each score
grades = gradeScale(...
arrayfun(@(g) find(g>=gradeScaleMinScore,1,'first'),scores));
Hope this helps.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by