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

Adam
Adam 2016년 10월 31일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam
Adam 2016년 10월 31일
편집: Adam 2016년 10월 31일
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

Thank you so much, It worked. Also I am new to Matlab, So i didn't know that.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Muhammad Maaz Bin Tahir
Muhammad Maaz Bin Tahir 2016년 10월 31일
편집: Adam 2016년 10월 31일
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

Adam
Adam 2016년 10월 31일
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