Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use a while loop to accept and store numerical grades of five students from the

ID: 3830992 • Letter: U

Question

Use a while loop to accept and store numerical grades of five students from the instructor. The numerical grades should be in between 0 to 100. Program must display an error message and let the instructor to enter all five grades again without terminating the program if the instructor enters a value which is not in between 0 and 100. After all five numbers are entered the program should produce and display the corresponding letter grade as below. Use 'fprintf' command to show the corresponding numerical grade and the letter grade in a table as shown below. Use a for loon to perform this task. Find and display the class average also. P x greaterthanorequalto 70 D 50 lessthanorequalto x

Explanation / Answer


%%create a vector to store grades
grades=[];
%%prompt user to enter grades values
for index=1:5
    prompt = 'Enter the grade from 0 to 100:';
    grade = input(prompt);
    %%re prompt if user enters invalid grades
    while(grade<0 || grade>100)
        fprintf('Invalid grade ');
        prompt = 'Enter the grade from 0 to 100:';
        grade = input(prompt);
    end
    grades(index)=grade;
end

gradeLetter='';
total=0;
%%print heading
fprintf('numeric grade letter grade ');
for index=1:5
    %%if condition checking
    if grades(index)>=70
        gradeLetter='P';
    elseif grades(index)>=50 && grades(index)<70
        gradeLetter='D';
    else
        gradeLetter='F';
    end
  
    %%print grades and grade letter with left alignment
    fprintf('%-10d %-10s ',grades(index),gradeLetter);
    total=total+grades(index);
end
fprintf('The class average is %5.2f',total/5.0);

--------------------------------------------------------------------------------

Sample Output:

Enter the grade from 0 to 100:89
Enter the grade from 0 to 100:74
Enter the grade from 0 to 100:26
Enter the grade from 0 to 100:55
Enter the grade from 0 to 100:100
numeric grade letter grade
89          P
74          P
26          F
55          D
100         P
The class average is 68.80>>