Clear Filters
Clear Filters

need help with an error

3 views (last 30 days)
Anthony Campuzano
Anthony Campuzano on 2 Jul 2015
Answered: Star Strider on 2 Jul 2015
My code works properly and does exactly what I need it to which is a GPA calculation
however when I test it on coursework, it tells me that I am failing the error test to make sure that my two inputs are integers (credits between 1 and 4)(grades between 0 and 4)
I have this test in there, and it works in matlab when I check for it, but not when I run it through the test on cody coursework.
Is there something I am missing from my code or is there something wrong with the testing I am running it through?
function GPA = GPAcalc(credits,grades) %GPAcalc calculates the GPA from an array of credits and grades % %INPUT credits: an array of credit hours of classes taken % grades: an array containing the grades of each class % the arrays of both inputs must be of the same size %OUTPUT GPA: the GPA based on the grade for each class taken
% ERROR CHECKS dimC=size(credits); dimG=size(grades); L1=length(credits); L2=length(grades);
if dimC(1)~=1 dimG(1)~=1; error('Credits and Grades must be a row array') elseif L1~=L2; error('Array inputs for credits and grades must be the same size') end
for i = 1:numel(credits) if credits(i) < 1 credits(i) > 4 grades(i) < 0 grades(i) > 4 error('credits must be an integer between 1-4 and grades must be an integer between 0-4'); end end
% TO CALCULATE GPA % STEP 1. multiply the point value of the letter grade by the number of credit % hours. The result is the grade points. x = 0; for i = 1:numel(credits) x = credits(i)*grades(i) + x; end % STEP 2. Total the credit hours for the term total = 0; for i = 1:numel(credits) total = total + credits(i); end % STEP 3. divide the total quality points by the total credit hours GPA = x/total; end

Answers (1)

Star Strider
Star Strider on 2 Jul 2015
Just a guess, but you might want to include a call to isinteger for each value, and have your code do whatever your assignment tells you to do with a non-integer value.


Find more on Testing Frameworks 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!