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 日

1 投票

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 件のコメント

Muhammad Maaz Bin Tahir
Muhammad Maaz Bin Tahir 2016 年 11 月 6 日
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 日

0 投票

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 ExchangePhysics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by