In for loop, All the students are getting same grades?

Hello, I was making code to sum, average and grade students based on marks of Maths, Chemistry and Physics. Sum and average is done but grading is not working. It is returning same grade to all students. Kindly Help, Regards.

1 个评论

How are we supposed to help if you don't provide us with any code that you are using?! You could be doing any one of a number of things incorrectly.

请先登录,再进行评论。

 采纳的回答

You made the same mistake that people in the past have made asking this very same question.
Sum is an array so in a loop you need to use
Sum(i)
in all your elseif statements. You have a for loop looping round variable i, but you don't use i so you basically do the same thing every time round the loop.
There are neater ways to do it, but I'll ignore those for now and just do the minimal correction to the code you have.

更多回答(1 个)

clear all
clc
C=input('Marks of students in Chemistry = ');
P=input('Marks of students in Physics = ');
M=input('Marks of students in Maths = ');
Sum = C + P + M ;
for n=1:size(C);
fprintf('Total Marks of Student = %0.2f\n', Sum);
end
AVG= (C+P+M)/3 ;
for n=1:size(C);
fprintf('Average Marks of Student = %0.2f\n', AVG);
end
for i= 1 : length(Sum);
if Sum>=275
fprintf('The grade of Student is "A-1",\n')
else if Sum>=250;
fprintf('The grade of Student is "A",\n ')
else if Sum>=225;
fprintf('The grade of Student is "B",\n')
else if Sum>=200;
fprintf('The grade of Student is "C",\n')
else if Sum>=175;
fprintf('The grade of Student is "D",\n')
else if Sum>=150;
fprintf('The grade of Student is "E",\n')
else
fprintf('The grade of Student is "F",\n')
end
end
end
end
end
end
end

1 个评论

Please add this as a comment of the original question or edit it into the question. It isn't an answer.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Physics 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by