Skip to content

Instantly share code, notes, and snippets.

@adophilus
Created July 30, 2024 00:58
Show Gist options
  • Save adophilus/9830439ed95ab9368f9979da91179578 to your computer and use it in GitHub Desktop.
Save adophilus/9830439ed95ab9368f9979da91179578 to your computer and use it in GitHub Desktop.
total_units = 0;
total_points = 0;
total_years = input('How many years do you want to calculate for?:');
% for loop to calculate the gpa of all the years
for year = 1:total_years
fprintf('Calculating for year %d\n', year);
number_of_courses = input('Enter number of courses offered: ');
session_total_units = 0;
session_total_points = 0;
for i = 1:number_of_courses
course = input('Enter course code: ', 's');
unit = input('Enter unit for course: ');
grade = input('Enter grade for course (A-F): ', 's');
switch grade
case 'A'
point = 5;
case 'B'
point = 4;
case 'C'
point = 3;
case 'D'
point = 2;
case 'E'
point = 1;
case 'F'
point = 0;
end
session_total_units = session_total_units + unit;
session_total_points = session_total_points + (point * unit);
end
% calculate the GPA for the session
session_gpa = session_total_points / session_total_units;
fprintf('GPA for year %d is %.2f\n', year, session_gpa);
total_units = total_units + session_total_units;
total_points = total_points + session_total_points;
end
% Calculate the CGPA
cgpa = total_points / total_units;
% Display the CGPA
fprintf('Your CGPA is %.2f\n', cgpa);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment