I am having trouble getting a code to read in elements of an array. Please help?

1 view (last 30 days)
Ok so I need to design a function that reads in grades ( 82, 90, 75, 94, 88, 99, 45, 90 ) , and outputs them as an array, and displays what each grade is as a letter grade. This is what I have so far. Please help
if true
grades = [82, 90, 75, 94, 88, 99, 45, 90]
for a=1; grades(:)
if grades<60
fprintf ('grade = F')
elseif grades>=60 , grades<70
fprintf ('grade = D')
elseif grades>=70 , grades<80
fprintf ('grade = C')
elseif grades>=80 , grades<90
fprintf ('grade = B')
elseif grades>=90
fprintf ('grade = A')
fprintf(grades)
end
end
end

Answers (3)

Adam
Adam on 11 Apr 2016
Edited: Adam on 11 Apr 2016
A bunch of thoughts on what you have so far...
If this is a function aren't the grades supposed to be inputs to the function? I'm not sure how this would be though if you are supposed to put them into an array within the function since an array would be the only sensible way to pass such grades into the function.
for a = 1;
does nothing. If you are trying to loop round the array of grades you need something more like:
for a = 1:numel( grades )
if grades(a) < 60
...
elseif
...
...
end
There are better ways than a big series of if and elseif clauses for something like this, but it depends what stage of learning you are at. If you are at the very beginning then the more advanced syntaxes may be something for learning later rather than for this task.
elseif grades>=60 , grades<70
is not valid syntax though - you have to create a logical condition to test e.g.
elseif grades(a) >= 60 && grades(a) < 70
Overall though it isn't clear from your question exactly what you inputs and outputs to/from the function are supposed to be. Are you wanting to output results in an array or just print them to the command line or to file?
doc fprintf
is used for writing to file, but expects a file id as first argument. I assume you do not intend to write to file.
doc disp
would suffice for what you appear to be outputting.
  6 Comments
Kyle Skelly
Kyle Skelly on 11 Apr 2016
It just outputs this
if true
grades =
82 90 75 94 88 99 45 90
end
and I am not sure why
Adam
Adam on 12 Apr 2016
Actually looking at it again
fprintf( '%d = B\n', grades(a) )
works fine. I am used to using that for printing to file, but it can also be used to print to command window rather than disp( sprintf(...) ).
There are a few other things wrong with your answer, but they should all be obvious. Your editor should underline syntax errors for you and you should double-check all your statements for things like missing array indexing, missing 'if' and just copy-pasting the same statement into every clause of he if statement.

Sign in to comment.


Star Strider
Star Strider on 11 Apr 2016
You need a logical operator here. A comma is not one:
elseif grades>=60 && grades<70
... and so for the rest.

Azzi Abdelmalek
Azzi Abdelmalek on 11 Apr 2016
Edited: Azzi Abdelmalek on 11 Apr 2016
grades = [82, 90, 75, 94, 88, 99, 45, 90];
for a=grades
if a<60
display ('grade = F')
elseif a>=60 & a<70
display('grade = D')

Categories

Find more on Get Started with 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!