Problem Solving: Students Grading Calculator
Learn to write a program to display the student grade.
We'll cover the following
Students grader
Take the record of k
students (where each record consists of five courses, marked out of 100) and display their achieved total marks and grades side by side. After displaying the last (k$^{th}$ record), it should also display which students have the highest percentage and grade.
Here is the grading scheme that our program must follow:
Grading scheme
Marks | Grade |
90 - 100 | A |
80 - 89 | B |
70 - 79 | C+ |
60 - 69 | C |
50 - 59 | D |
1 - 50 | F |
other | I (invalid) |
Implementation
We will make the grader by solving the following functions as units and, after testing every unit, integrate them all into the required program.
Sample output
k = 3
Student 1(Roll# C1 C2 C3 C4 C5) : 1111 88 99 99 100 97
Compiled Result: Roll# 1111 has a percentage of 96.6% and achieved grade A
Student 2(Roll# C1 C2 C3 C4 C5) : 1112 90 78 56 89 90
Compiled Result: Roll# 1112 has a percentage of 80.6% and achieved a grade B
Student 3(Roll# C1 C2 C3 C4 C5) : 1113 88 44 55 66 77
Compiled Result: Roll# 1113 has a percentage of 80.6% and achieved a grade C
Roll# 1111 has the highest percentage of 96.6% and achieved grade A
Let’s divide this problem into two parts. The first part will follow the grading scheme and the second part will compile the result. We have the following division:
- The
marksGrader()
function takesmarks
and returns the grade according to the grading scheme. - The
kStudentsResult()
function is a core function that will perform the compilation task of thek
student records.
Let us discuss the marksGrader()
function first:
1. The marksGrader()
function
This function should take the percentage of a student and print the corresponding letter grade.
Sample input
92
Sample output
A
We can easily see from our problem that we need a function marksGrader() that takes an integer as input and returns a character (the letter grade). Hence the prototype should be:
char marksGrader(int marks);
Now, this function will take the parameter marks
and return the corresponding letter grade instead of printing it on the console. This is a significant change that we are adopting in the entire chapter of this course. We should ponder why we are doing this. The reason is, again, that this grader is now a separate module that we can call from anywhere and use its result to do several tasks.
We can implement the marksGrader()
function in three different ways:
char marksGrader (float marks){if(marks > 100)return 'I';if (marks >= 90.0 && marks <= 100.0)return 'A';else if (marks >= 80.0 && marks <= 89.0)return 'B';else if (marks >= 70.0 && marks <= 79.0)return 'C';else if (marks >= 60.0 && marks <= 69.0)return 'D';else if (marks >= 50.0 && marks <= 59.0)return 'E';else if (marks < 50.0 && marks >= 1.0)return 'F';elsereturn 'I'; //invalid input}
marksGrader()
Can you see any difference in the above three implementations in terms of efficiency?
Let’s test this code with the appropriate main()
:
#include <iostream> using namespace std; /*This function will get a percentage from user and print grade */ char marksGrader(float marks); int main() { int marks; cout<<"marks:"<<endl; cin >> marks; cout<<marksGrader(marks)<<endl; return 0; } char marksGrader (float marks) { if(marks > 100) return 'I'; else if (marks >= 90.0) return 'A'; else if (marks >= 80.0) return 'B'; else if (marks >= 70.0) return 'C'; else if (marks >= 60.0) return 'D'; else if (marks >= 50.0) return 'E'; else if (marks > 0) return 'F'; else return 'I'; //invalid input }
- In lines 13–29, we compare each percentage range with
marks
andreturn
letter grades accordingly. For example, ifmarks
are86
, it will return'B'
.
Let’s move to the next function.
2. The kStudentsResult()
function
We will make a function kStudentsResult()
, in which we will pass k
as an argument. This is the core function, which will perform the compilation task of the k
records.
As per our requirements, we will make the following memory variables.
int r_no, // for every roll numbermax_r_no, // for maintaining the maximum roll #max_percentage = -1, // for maintaining the maximum %agec1, c2, c3, c4, c5, // for storing each course markstotal; // for storing the total marks
Inside the function, we will make a loop that will take the input of k
students’ course marks
together with their roll numbers in each iteration. We will pass the marks to a percentageCourses()
function to calculate the percentage of each student. But there is a question for you: how will we keep the roll number of the student with the highest percentage?
In the previous lesson, we discussed the functionality of the maximum number. How can we use that functionality to solve this problem? We initially assign -1
to max_percentage
. In each iteration of the for
loop, we will take a student’s record and calculate the percentage. If the current student’s percentage is greater than the max_percentage
, then we will replace max_percentage
with the current student’s percentage. We will also update max_r_no
with the current student’s roll number.
Let’s see how to write what we’ve discussed above in a function:
void kStudentsResult(int k){int r_no, max_r_no, max_percentage = -1, total, count = 1,c1, c2, c3, c4, c5;float percentage;char grade;for(int i=1; i<=k; i++){cout << "Student Roll# and marks of five courses: " << endl;cin >> r_no >> c1 >> c2 >> c3 >> c4 >> c5;percentage=percentageCourses(c1,c2,c3,c4,c5);grade = marksGrader (percentage);cout << "Roll # " << r_no << "has Percentage of "<< percentage <<"% and achieved grade " << grade << endl;if (percentage > max_percentage){ // compare if current percentage is greater than previous percentagemax_percentage = percentage; //update current percentage with max_totalmax_r_no = r_no; //update current roll number with max_r_no}}grade = marksGrader(max_percentage);cout << "Roll # " << max_r_no <<"has highest Percentage of " << max_percentage<<"% and achieved grade " << grade << endl;}
Grade calculator
Notice carefully! Here we have initialized max_percentage
with -1
; but in the previous lesson (Finding Maximum Number) when we initialized max
with 0
, it did not work (do you remember why?). To fix the issue in that lesson, we assigned the first value to max
. Then, in the loop, we compared each value with max
and updated it when needed.
Why doesn’t the same logical error arise in the above case of calculating maximum percentage?
Let’s write a complete code below and see the output:
#include <iostream> using namespace std; char marksGrader (float marks) { if(marks > 100.0) return 'I'; if (marks >= 90.0) // implicitly means marks <= 100.0 return 'A'; if (marks >= 80.0) // implicitly means marks < 90.0 return 'B'; if (marks >= 70.0) // implicitly means marks < 80.0 return 'C'; if (marks >= 60.0) // implicitly means marks < 70.0 return 'D'; if (marks >= 50.0) // implicitly means marks < 60.0 return 'E'; if (marks >= 0.0) // implicitly means marks < 50 return 'F'; return 'I'; } float percentageCourses(int c1,int c2,int c3,int c4,int c5) { int total = c1 + c2 + c3 + c4 + c5; // sum of all courses float percentage = (total / 500.0) * 100.0; // calculate the percentage of 1st student return percentage; } void kStudentsResult (int k) { int r_no, max_r_no, max_percentage = -1, total, count = 1,c1, c2, c3, c4, c5; float percentage; char grade; for(int i=1; i<=k; i++) { cout << "Student Roll# and marks of five courses: " << endl; cin >> r_no >> c1 >> c2 >> c3 >> c4 >> c5; percentage=percentageCourses(c1,c2,c3,c4,c5); grade = marksGrader (percentage); cout << "Roll # " << r_no << " has Percentage of " << percentage <<"% and achieved grade " << grade << endl; if (percentage > max_percentage)// compare if current percentage is greater than previous percentage { max_percentage = percentage; //update current percentage with max_total max_r_no = r_no; //update current roll number with max_r_no } } grade = marksGrader (max_percentage); cout << "Roll # " << max_r_no << " has highest Percentage of " << max_percentage <<"% and achieved grade " << grade << endl; } int main () { int k; cout<<"K number of students:"<<endl; cin >> k; kStudentsResult (k); return 0; }