How to I fix this Index error?

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)

Change your lines to this :
M=input('value of M? ')
scores=randi([40,100],1,M);
for i = 1:numel(scores)

3 Comments

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
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.)
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?

Sign in to comment.

Walter Roberson
Walter Roberson on 1 Nov 2018
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

Asked:

on 1 Nov 2018

Edited:

on 1 Nov 2018

Community Treasure Hunt

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

Start Hunting!