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 commentaire

Adam
Adam le 31 Oct 2016
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.

Connectez-vous pour commenter.

 Réponse acceptée

Adam
Adam le 31 Oct 2016
Modifié(e) : Adam le 31 Oct 2016
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 commentaire

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

Connectez-vous pour commenter.

Plus de réponses (1)

Muhammad Maaz Bin Tahir
Muhammad Maaz Bin Tahir le 31 Oct 2016
Modifié(e) : Adam le 31 Oct 2016
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 commentaire

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by