47.0k views
1 vote
Write a MATLAB program to:

Prompt the user to enter numeric grades (one at a time) within a while loop. Any number of grades can be entered. The user should enter a negative number to indicate that there are no more grades.

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 grade ranges.

User Vromanch
by
5.0k points

1 Answer

6 votes

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.

User Zardilior
by
5.3k points