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

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)

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

Thank you for your response! I think I get what you are saying, but I am very new to matlab. I would like to display my results as an array as in, "85 = B" and such. This is the code I have now
if true
grades = [82, 90, 75, 94, 88, 99, 45, 90]
for a=1:numel(grades)
if grades(a) <= 60
fprintf ('grade = F')
elseif grades(a) >=60 && grades<70
fprintf ('grade = D')
elseif grades(a) >=70 && grades<80
fprintf ('grade = C')
elseif grades(a) >=80 && grades<90
fprintf ('grade = B')
elseif grades(a) >=90
fprintf ('grade = A')
doc disp(grades)
end
end
Ok, so if you want "85 = B" type of output then you should use
doc sprintf
This uses a formatted string that allows you to put a placeholder that will later be replaced by e.g. a number i.e.
disp( sprintf( '%d = B', grades(a) ) )
will output
85 = B
if grades(a) is equal to 85.
Don't put
doc disp(grades)
in your code though. doc instructions are ones (without arguments like you have added) that you can type in command window to take you to the help page for the given function. You should never just copy code without understanding it so I am pointing you to the help pages for functions I am using so you can look at them and understand why I am suggesting them. The 'See Also' section at the bottom of each is also a vital source of learning to find similar functions that may do a better job for your purpose.
Ok so this is what I Have now.
if true
grades = [82, 90, 75, 94, 88, 99, 45, 90]
for a=1 : numel(grades)
if grades(a) <= 60
disp( sprintf( '%d = B', grades(a) ) )
elseif grades(a) >=60 && grades<70
disp( sprintf( '%d = B', grades(a) ) )
elseif grades(a) >=70 && grades<80
disp( sprintf( '%d = B', grades(a) ) )
elseif grades(a) >=80 && grades<90
disp( sprintf( '%d = B', grades(a) ) )
else grades(a) >=90
disp( sprintf( '%d = B', grades(a) ) )
end
end
And how close is it to giving what you need?
It just outputs this
if true
grades =
82 90 75 94 88 99 45 90
end
and I am not sure why
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.

You need a logical operator here. A comma is not one:
elseif grades>=60 && grades<70
... and so for the rest.
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')

Asked:

on 11 Apr 2016

Commented:

on 12 Apr 2016

Community Treasure Hunt

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

Start Hunting!