Answer:
clc;clear;
isLooping = 1;
x = [];
while isLooping
newVal = input("Enter a new value or a negative number to exit loop: ", 's');
newVal=str2num(newVal)
if newVal >= 0
x = [x, newVal];
else isLooping = 0;
end
end
A = x(x>= 90);
B = x(x>= 80 & x<90);
C = x(x>= 70 & x<80);
D = x(x>= 60 & x<70);
F = x(x<60);
sprintf("There are %d elements in A", numel(A))
sprintf("There are %d elements in B", numel(B))
sprintf("There are %d elements in C", numel(C))
sprintf("There are %d elements in D", numel(D))
sprintf("There are %d elements in F", numel(F))
sprintf("The maximum grade is %d", max(X))
sprintf("The minimum grade is %d", min(X))
sprintf("The average grade is %f", mean(X))
Step-by-step explanation:
The first line clears the workplace, just in case. Then, you defined a variable that will decide if we have to loop or not, and we initialize the grades array as null.
while the lopping flag is on, we ask for a new value, convert it to number, and we check: if it's greater than zero we append it to the grades array, otherise we switch the looping flag off.
At this point, the grades array is filled, and we can compute the required values. To determine the range, we can use a feature in matlab: the line
x>10
will return the positions of the elements in x that are greater than 10. So, if we use that line as a selector:
x(x>10)
we will fetch the actual elements. Using this feature, we can easily fill the range arrays A, B, C, D and F.
Finally, the number of elements in a vector is given by the numel() function, and the max, min and average are given by the built-in functions max, min and mean.