how to write matlab statements to sum the number of a's (grades of 90-100), b's (80-89), and c's (70-79). given the array grades=[91 85 72 78 99 83 89 93 77 92 89 90 100 78 88 76 98 76 85 92]; in matlab

You can use conditional statements if to check if each grade falls in the range of A, B, and C. Then you can use variable to store the number of each grade.

main.m
grades=[91 85 72 78 99 83 89 93 77 92 89 90 100 78 88 76 98 76 85 92];
num_a = 0; % initialize counter for A's
num_b = 0; % initialize counter for B's
num_c = 0; % initialize counter for C's

for i = 1:length(grades) % loop through each grade in the array
    if grades(i) >= 90 && grades(i) <= 100 % check if grade is in range of A
        num_a = num_a + 1; % increment A counter
    elseif grades(i) >= 80 && grades(i) <= 89 % check if grade is in range of B
        num_b = num_b + 1; % increment B counter
    elseif grades(i) >= 70 && grades(i) <= 79 % check if grade is in range of C
        num_c = num_c + 1; % increment C counter
    end
end

% display the number of A's, B's, and C's
fprintf('Number of A''s: %d\n', num_a);
fprintf('Number of B''s: %d\n', num_b);
fprintf('Number of C''s: %d\n', num_c);
815 chars
20 lines

Output:

main.m
Number of A's: 7
Number of B's: 7
Number of C's: 6
51 chars
4 lines

gistlibby LogSnag