Calculate Grades and Pass/Fail Status with Basic Matlab Programming

Click For Summary
SUMMARY

The discussion focuses on a MATLAB script designed to calculate student grades and pass/fail status based on weighted scores. The user encountered issues with the script not displaying the correct pass/fail status and grade. Key problems identified include improper use of relational operators in the if statement and the absence of an 'end' statement to terminate the first if loop. The corrected structure involves using separate elseif statements for each grade condition to ensure only one output is displayed.

PREREQUISITES
  • Basic understanding of MATLAB programming
  • Familiarity with MATLAB control statements
  • Knowledge of matrix operations in MATLAB
  • Understanding of conditional logic in programming
NEXT STEPS
  • Review MATLAB documentation on control statements and the if statement
  • Learn about MATLAB matrix multiplication and its applications
  • Explore best practices for structuring conditional statements in MATLAB
  • Investigate debugging techniques for MATLAB scripts
USEFUL FOR

Students, educators, and developers working with MATLAB who need to implement grading systems or troubleshoot conditional logic in their scripts.

roam
Messages
1,265
Reaction score
12
http://img16.imageshack.us/img16/3078/15688239.gif

So here's my script:

Code:
x=[a, b, c, d];
w=[0.65 0.2 0.05 0.1]';
final=x*w;
disp(final);
if 65>final>=50
    disp('Grade is: C')
elseif 80>final>=65
    disp('Grade is: B')
elseif final>=80
    disp('Grade is: A')
If final>=50
    disp('Student Passes')
    else
        disp('Student Fails')
end

So for example I tried x = [70, 50, 30, 45]:

Code:
x=[70, 50, 30, 45];
w=[0.65 0.2 0.05 0.1]';
final=x*w;
disp(final);
if 65>final>=50
    disp('Grade is: C')
elseif 80>final>=65
    disp('Grade is: B')
elseif final>=80
    disp('Grade is: A')
If final>=50
    disp('Student Passes')
    else
        disp('Student Fails')
end

I got the following answer:

Code:
   61.5000

Student Fails

Well, it calculated the mark correctly, but I don't understand why it says "student fails" when it should be displaying "student passes". :confused: It also doesn't display the grade. What's the problem? Can anyone help? :rolleyes:
 
Last edited by a moderator:
Physics news on Phys.org
Probably because you didn't terminate the first 'if' loop with an 'end' statement. My MATLAB may be a little rusty, but I also don't recall if you can use the relational operators in the way you do:
Code:
if 65>final>=50

You probably want to restructure with a bunch of elseif statements that make a single comparison and cascade downwards (such that you only get one output, and not all the outputs for which the final grade is valid, e.g. a mark of 90 getting you an A, B, C and fail):
Code:
if final >= 80
      disp('Grade is: A')
elseif grade >=65
      disp('Grade is: B')
%other stuff
end

EDIT: Mathworks documentation pages for control statements, and the if statement:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/brqy1c1-1.html#brqy1c1-3
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/if.html
 
Last edited by a moderator:
Thanks a lot for your input, I tried it and it worked. :smile: :smile:
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
24K
  • · Replies 5 ·
Replies
5
Views
6K