Trying to create a GPA calculator?

조회 수: 25 (최근 30일)
Seong Nam
Seong Nam 2017년 5월 7일
댓글: Liam 2023년 6월 16일
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.

채택된 답변

Rahul Goel
Rahul Goel 2017년 5월 10일
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!

추가 답변 (1개)

TARIK YAMAN
TARIK YAMAN 2019년 1월 12일
Hi, Seong. You can improve and use the script.
clc;
clear;
clear all;
fid = fopen('my_transcript.txt','wt');
a = input('enter the number of course : ');
k = 0;
for i = 1:a;
k = k + 1;
fprintf('\nfor %3d. course\n',k);
b(k) = input('\nenter the credits of this course : ');
c = input('\nenter the your grade : ','s');
switch c
case 'AA'
d(k) = 4;
case 'BA'
d(k) = 3.5;
case 'BB'
d(k) = 3;
case 'CB'
d(k) = 2.5;
case 'CC'
d(k) = 2.0;
case 'DC'
d(k) = 1.5;
case 'DD'
d(k) = 1.0;
case 'FD'
d(k) = 0.5;
case 'FF'
d(k) = 0;
case 'FG'
d(k) = 0;
otherwise disp('your input is invalid, so it must be like AA , BB , DD , FG , FF');
fprintf('\nfor %3d. course\n',k);
e = input('\nenter the your grade again : ','s');
switch e
case 'AA'
d(k) = 4;
case 'BA'
d(k) = 3.5;
case 'BB'
d(k) = 3;
case 'CB'
d(k) = 2.5;
case 'CC'
d(k) = 2.0;
case 'DC'
d(k) = 1.5;
case 'DD'
d(k) = 1.0;
case 'FD'
d(k) = 0.5;
case 'FF'
d(k) = 0;
case 'FG'
d(k) = 0;
end
end
p(k) = d(k)*b(k);
gano0 = sum(p)/sum(b);
percent0(k) = 100*(b(k));
end
percent = percent0/sum(b);
table = [1:a;b;d;percent];
fprintf(fid,'course number credit grade margin of effect on general grade\n');
fprintf(fid,' %3d %10.1d %3.1f %10.4f\n',table);
fprintf(fid,'\nsum of credits : %3d general grade : %10.3f',sum(b),gano0);
fclose(fid);
clc;
open('my_transcript.txt');
  댓글 수: 1
Liam
Liam 2023년 6월 16일
@TARIK YAMAN I tried ur code and it worked great. thanks a bunch.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by