How to make a function that tell us about grades using Matlab
219 views (last 30 days)
Show older comments
Muhammad Usman Saleem
on 26 May 2015
Edited: Muhammad Usman Saleem
on 29 Oct 2022
Hi every one; I going to make a function called letter_grade that takes a positive integer called score as its input argument and returns a letter grade according to the following scale: A: 91 and above; B: 81-90; C: 71-80; D: 61-70; F: below 61. Remember that to assign a letter to a variable, you need to put it in single quotes, as in: grade = 'A'. when i run the below codes i got grade 'B' for score 95.which in not correct.Kindly guide me how can i correct this code. Please vote up if this question help!
function grade=letter_grade(n)
if n >= 91 && n>=100
grade='A';
elseif n >= 81 && n>=90
grade='B';
elseif n >= 71 && n>=80
grade='C';
elseif n >= 61 && n>=70
grade='D';
elseif n <61
grade='F';
end
end
2 Comments
Murali Krishna
on 26 May 2015
Grade B is defined when n is between 81 and 90 both inclusive. if n>81 and n<=90 grade should be B. But in your code you have written the condition as n>=81 and n>=90. The same error is for finding other grades.
Jan
on 28 Dec 2018
@Goshom Brian and Qahar Mustafa: Please do not use flags to post comments. Thanks.
Accepted Answer
Image Analyst
on 26 May 2015
Try it this way:
function grade=letter_grade(n)
if n >= 91
grade='A';
elseif n >= 81
grade='B';
elseif n >= 71
grade='C';
elseif n >= 61
grade='D';
else
grade='F';
end
end
4 Comments
Muhammad Aamir Hayat
on 1 Jul 2021
Hi there, can you please create the same program by using switch statement?
Image Analyst
on 1 Jul 2021
@Muhammad Aamir Hayat, did you try it yourself first? I imagine you did. Did you get something like this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
for n = 55:10:95
grade=letter_grade(n);
fprintf('A score of %d is an "%s".\n', n, grade);
end
fprintf('Done! Thanks Image Analyst!\n');
%=========================================================================
function grade=letter_grade(n)
n2 = floor( (n-1) / 10);
switch n2
case {9, 10}
grade='A';
case 8
grade='B';
case 7
grade='C';
case 6
grade='D';
otherwise
grade='F';
end
end
A score of 55 is an "F".
A score of 65 is an "D".
A score of 75 is an "C".
A score of 85 is an "B".
A score of 95 is an "A".
Done! Thanks Image Analyst!
More Answers (1)
SY
on 4 Aug 2021
What's the meaning of this program?
function [Average, StandardDeviation ] = F1_91(Scores)
% Prompt user to input grades(scores)
Scores = input('Enter Grades: ');
%Calculating average of grades
X = mean(Scores)
% Rounding off to nearest integer
Average = round(X)
% Calculating standard deviation of grades
SDD = std(Scores)
% Rounding off to nearest integer
StandardDeviation = round(SDD)
end
2 Comments
Image Analyst
on 5 Aug 2021
It simply computes the rounded standard deviation of a set of rounded numbers. Not sure what you mean by "meaning"? It's probably someone's homework answer.
Walter Roberson
on 5 Aug 2021
It is someone's wrong homework answer at that. It should not pass in Scores and then overwrite Scores with the input()
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!