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

1 visualización (últimos 30 días)
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!

Respuestas (1)

Kirby Fears
Kirby Fears el 11 de Abr. de 2016
Editada: Kirby Fears el 11 de Abr. de 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.

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by