MATLAB Answers

Calculating grades in a while loop

10 views (last 30 days)
Angel Sanchez
Angel Sanchez on 13 Feb 2020
Commented: Angel Sanchez on 13 Feb 2020
If I have a array of students grade scores, score=[82, 75, 78, 82, 90, 87], and i want to calculate the number of A's, B's. How would i do that for example to be a grade of A it must be greater or equal to 90, which on the score is shows 2. How can i output that which it will give me the value of 2, as well as for b which would give me 3 which is greater or equlat to 80.

  0 Comments

Sign in to comment.

Answers (2)

Bhaskar R
Bhaskar R on 13 Feb 2020
grade_A = sum(score>=90);
grade_B = sum(score>=80 & score<90);
For more details on loops and conditionings read

  0 Comments

Sign in to comment.


Jakob B. Nielsen
Jakob B. Nielsen on 13 Feb 2020
You don't need a while loop for that - it is easy with logic indexing, like so:
score=[82 75 78 82 90 87];
A=score(score>=90); %take the indexes of score, for which that same index of score is greater than or equal to 90.
numA=numel(A);
B=score(score>=80 & score<90);
numB=numel(B);

  3 Comments

Angel Sanchez
Angel Sanchez on 13 Feb 2020
correct but on my hw problem its mentioning using a loop.
Jakob B. Nielsen
Jakob B. Nielsen on 13 Feb 2020
Weird homework to ask you to do more than you need. You should be cheeky, and hand in the above solution as well as the loop :D
Must it be a while loop, specifically, or can it be a for loop?

Sign in to comment.

Sign in to answer this question.