MATLAB: Need help with a while loops problem

averagegradehomeworkloopsmaxminno attemptwhile

So in my homework I was given this problem:
"Prompt the user to enter numeric grades (one at a time) within a while loop. Any number of grades can be entered.
• Determine the number of grades in each grade range using a standard 10 point scale: A (90 or higher), B (80-90), C (70-80), D (60-70), and F (0-60).
• Determine the maximum, minimum, and average grade.
• Display the results including the number of grades, maximum grade, minimum grade, and the number of grades in each grade range.
• Test the program for cases including 4 grades, 10 grades, and 20 grades with the grades reasonably distributed between the grades ranges."
And I could really use some help getting started!
Thanks so much!

Best Answer

Hugh - delay adding your grade to the array until you know for sure whether it is valid or not. For example, you can replace your while loop with the following
k=1;
allGrades = [];
while true
grade = input(prompt);
if grade < 0
break;
end
allGrades(k) = grade;
k=k+1;
end
Note how k is used instead of i (MATLAB uses i and j to represent the imaginary number). Also, we break out of the loop once we know that the grade is negative. I've avoided using the total and counter variables because you can use sum and length to determine both.
As for counting your grades, since there are five (A,B,C,D,F) then use an array initialized with five elements to manage the counts for these grades. Loop over your allGrades array and increment the appropriate index that corresponds to the letter grade of the mark. (Or just do this whenever the user enters in a new grade.)