How to I fix this Index error?
Show older comments
Exam1 = input('What is the grade for Exam1? ');
Exam2 = input('What is the grade for Exam2? ');
Exam3 = input('What is the grade for Exam3? ');
fprintf('The grades for three exams are %3.1f , %3.1f , and %3.1f.\n',Exam1,Exam2,Exam3)
E = [Exam1; Exam2; Exam3]
%calculates average of exam scores
Avg = mean(E)
%assings letter grades to score intervals
if (Avg >= 90)
Grade = 'A';
elseif (Avg >= 80 && Avg < 90)
Grade = 'B';
elseif (Avg >= 70 && Avg < 80)
Grade = 'C';
elseif (Avg >= 60 && Avg < 70)
Grade = 'D';
else (Avg < 60)
Grade = 'F';
end
%prints out average of exams and corresponding letter grade
fprintf('The Average is %3.1f so the grade is %s.\n',Avg,Grade)
M=input('M = ');
%generates M triples of random scores between 40 and 100
scores=randi([40,100],M,3);
for i = 1:M
fprintf('Student%d\n',i)
Grade(scores(i,1),scores(i,2),scores(i,3));
end
ClassAvg = mean2(scores);
fprintf('The class average is %3.1f.\n',ClassAvg);
Now after I enter the grades it asks for M = _ I enter 3 but then it gives this error:
Index in position 1 exceeds array bounds (must not exceed 1)
How do I fix this?
Answers (2)
madhan ravi
on 1 Nov 2018
Change your lines to this :
M=input('value of M? ')
scores=randi([40,100],1,M);
for i = 1:numel(scores)
3 Comments
madhan ravi
on 1 Nov 2018
anyhow you will get an error at the second line of loop because Grades is not defined , I didn't quite interpret that because not sure what you want to do there
Walter Roberson
on 1 Nov 2018
I do not see a reference to Grades only to Grade, and Grade is defined for scalar numeric inputs (but not for nan and not necessarily for vector or array inputs of scores.)
madhan ravi
on 1 Nov 2018
now I get it sir but not certain what OP wants to do by
Grade(scores(i,1),scores(i,2),scores(i,3));
this line , maybe display the grade assigned to the score?
Walter Roberson
on 1 Nov 2018
0 votes
You start by pulling in 3 scores and you create a letter grade from them. That gets you a single character that has been assigned based upon the three values entered.
You then create an array of random scores and try to use them to index that single character as a 3d array.
You need to define your grading logic as a function.
Categories
Find more on MATLAB 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!