Using if-else statement to determine which value to input into function
Show older comments
Hello,
I am trying to calculate final grades from the data given in the screenshot. From left to right columns are
1) student ID
2) test 1 grade
3) test 2 grade
4) midterm grade
5) project grade
6) final grade
Tests are worth 10% and whichever test is higher is used in final calculation.
I am using the following to calculate final grades:
if samplegrades(:,2) > samplegrades(:,3)
t = samplegrades(:,2)
else
t = samplegrades(:,3)
end
m = samplegrades(:,4)
p = samplegrades(:,5)
f = samplegrades(:,6)
finalgrades = @(t,m,p,f) (t.*.10+m.*.20 + p.*.20+f.*.50)
finalgrades(t,m,p,f)
The problem is that the if-else statement doesn't seem to be using the higher t value. It is only using values from test2 to determine final grade. Please help if you can.
1 Comment
dpb
on 13 Jun 2022
Read the doc for if, elseif, else carefully; particularly the part that explains how MATLAB determines what is TRUE.
Then see <find-array-elements-that-meet-a-condition> for introduction to logical addressing. It's possible to write the desired code w/o a for...end loop, but many beginners find that the easiest first.
Since this is a homework problem, we'll coach and let you figure out the next steps instead of just handing the solution to you...
Accepted Answer
More Answers (0)
Categories
Find more on Performance and Memory 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!