MATLAB: Trying to create a GPA calculator

functiongpa calculatorMATLAB

I want to create a Cumulative GPA calculator using two scripts. The first script will ask for Class Name, Number, Credits, and Grade.
%Problem Statement: Gather information of a certain class
Transcript.CourseName = input('What is the Course Name?', 's' );
Transcript.CourseNumber =input('What ss the Course Number?', 's');
Transcript.CouseCredits = input('What is the Course Credits?');
Transcript.CourseGrade = input('What is your grade in that class?');
The second script will gather those informations from all the classes the user is taking, and calculate the Cumulative GPA
%Problem Statement: Calculate the cumilative GPA from the grade of the
%three courses.
%Input: i-number of classes
%Output:GPA- Culmilative GPA
Ask = input('How many classes are you taking?')
numClass = Ask
for i =1:numClass
TRANSCRIPT(i).CourseName = input('What is the Course Name?', 's' );
TRANSCRIPT(i).CourseNumber =input('What ss the Course Number?', 's');
TRANSCRIPT(i).CourseCredits = input('What is the Course Credits?');
TRANSCRIPT(i).CourseGrade = input('What is your grade in that class?');
function(GPA_Cumli) =
end
As you can see, I haven't written the function yet because I do not know how'll this look. I know I am supposed to add the CourseGrade from all my classes then divide them by i, but I am struggling with the addition part. Any help would be appreciated.

Best Answer

Hi Seong,
You can change your function to something like this:
function GPA = calculateGPA()
%Output:GPA- Culmilative GPA
Ask = input('How many classes are you taking?')
numClass = Ask;
totalEarned = 0;
totalCredits = 0;
for i =1:numClass
TRANSCRIPT(i).CourseName = input('What is the Course Name?', 's' );
TRANSCRIPT(i).CourseNumber =input('What ss the Course Number?', 's');
TRANSCRIPT(i).CourseCredits = input('What is the Course Credits?');
TRANSCRIPT(i).CourseGrade = input('What is your grade in that class?');
totalEarned = totalEarned + (TRANSCRIPT(i).CourseCredits * TRANSCRIPT(i).CourseGrade);
totalCredits = totalCredits + TRANSCRIPT(i).CourseCredits;
end
GPA = totalEarned/totalCredits;
end
Hope this helps!
Related Question