MATLAB: I have a homework assignment to create a final course grade calculator

homework help calculator errorMATLAB

This is my first semester using matlab and I need some help with this.
My class had an assignment to create a code that estimates your final course grade. I wanted to modify it so it had some more practical use.
This is based on the score and weight of quizzes, homework and exams. Then it takes your final exam weight and it displays your final grade based on how you do on your final from 0-100%
Say a class is only based on homework and exams, if I put zero for the quiz input, it breaks the code. How do I get around this?
Also, is there a better way to display the results? Still new to using matlab.
Here is what I have so far:
clear,clc;
AskQuiz= input('How many quizzes do you have? ');
QuizWeight=input('What is your quiz weight as a decimal? ');
AskExam= input('How many exams do you have? ');
ExamWeight=input('What is your exam weight? ');
AskHomework= input('How many homework assignments do you have? ');
HomeworkWeight=input('What is your homework weight? ');
numQuiz = AskQuiz;
numExam= AskExam;
numHomework=AskHomework;
for i=1:numQuiz
quiz(i)=input('What is your quiz score? ');
totalquiz=sum(quiz(i));
QuizScore=totalquiz*QuizWeight;
end
for j=1:numExam
exam(j)=input('What is your exam score? ');
totalexam=sum(exam(j));
ExamScore=totalexam*ExamWeight;
end
for k=1:numHomework
homework(k)=input('What is your homework score? ');
totalhomework=sum(homework(k));
HomeworkScore=totalhomework*HomeworkWeight;
end
TotalGrade=QuizScore+ExamScore+HomeworkScore;
FinalWeight=input('What is your final weight? ');
m=[0:10:100];
FinalScore=m*FinalWeight;
FinalGrade=FinalScore+FinalWeight;
table=[m' FinalGrade'];
fprintf('Final Exam Projected Grade Percent\n')
disp(table)

Best Answer

Right now, the only place you assign a value to the QuizScore variable is inside the for loop that runs from 1 to the number of quizzes. Therefore if the number of quizzes is 0, that variable never gets created and MATLAB can't add that variable to the other scores.
I'd define an initial value for QuizScore prior to the for loop so that even if the number of quizzes is 0 that variable exists (with that initial value) when MATLAB tries to compute the total score.
Related Question