R Program to Assign Grades Based on a Student’s Score Using If-Else
Last Updated :
27 Sep, 2023
Giving pupils grades based on their performance is a frequent practise in educational environments. This program’s goal is to automatically grade students in accordance with their test results. The programme will assess a student’s performance and compute the corresponding grade using if-else statements in R. This automation not only speeds up the grading process but also guarantees fairness and consistency.
The If-Else statement is used when we need to perform a particular task based on a condition. The If block executes if the condition is true else the Else block is executed.
Grade Assignment Based on Percentage for Student’s Marks
Given an integer array marks, which comprises of marks scored by a student (out of 100) in different subjects, the task is to assign a grade to the student. The grade is found out by taking the percentage of the marks scored by the student. The percentage is calculated as:
(Total Marks Obtained by the student ✖ 100 ) / (Maximum marks of all the subjects)
The grade is assigned using the following rules
Approach:
Input: marks = { 25, 65, 46, 98, 78, 65 }
Output: C
Explanation: Total marks obtained by the student = 25 + 64+ 46+ 98 + 78+ 65 = 376
Max_marks = 5 ✖ 100 = 500
Percentage = ( 376 ✖ 100) / 500
= 37600 / 500 = 73. 2 (So the grade will be C, according to the above table)
Input: marks = { 95, 88, 98, 93, 92, 96 }
Output: A
Explanation: Total marks obtained by the student = 95 + 88+ 98 +93 +92 +96 = 562
Max_marks = 6 ✖ 100 = 600
Percentage = ( 562 ✖ 100) / 600
= 56200 / 600 = 93. 6 (So the grade will be A, according to the above table)
Below is the approach to solve the problem:
- Initialize a variable to sum all the marks scored by the student, total to 0.
- Initialize a variable to store the grade of the student, grade to ‘F’.
- First, we iterate through the marks array and find the total marks scored by the student.
- Then, we apply the formula described above to calculate the percentage.
- We then make a nested if else construct to assign proper grade to the student.
Below is the implementation of the above approach in R language:
R
marks <- c (95, 88, 98, 93, 92, 96)
max_marks <- length (marks) * 100
total <- 0
grade<- 'F'
total <- sum (marks)
percentage <- (total/ max_marks) *100
if (percentage >= 90) {
grade <- 'A'
} else {
if (percentage >= 80 && percentage <= 89) {
grade <- 'B'
} else {
if (percentage >= 60 && percentage <= 79) {
grade <- 'C'
} else {
if (percentage >= 33 && percentage <= 59) {
grade <- 'D'
} else {
grade <- 'F'
}
}
}
}
print (grade)
|
Output:
[1] "A"
Time complexity: O(N), Where N is the number of elements in the array.
Auxiliary Space : O(1), constant space is used.
Here in the above code we have find out the sum of the array elements using sum() function. and then find the percentage of the marks obtained by the students by using the formula mentioned in the article, Based on their percentage, we have given Grades(A, B, C, D, E) to the students.
Grade Classification on student’s score using If-Else
Suppose There is a class- test of Programming Language, In which students got different marks Out of 50. If the students will get some Grade Based on the below table:
Excellent
|
Very Good
|
Good
|
Bad
|
Write a program to assign grades based on student’s score using If -Else Satatement in R:
Below is the implementation using If-Else Statement:
R
assignGrade <- function (marks) {
if (marks > 40) {
return ( "Excellent" )
} else if (marks >= 30 && marks <= 40) {
return ( "Very Good" )
} else if (marks >= 20 && marks < 30) {
return ( "Good" )
} else {
return ( "Bad" )
}
}
student1_marks <- 45
student2_marks <- 35
student3_marks <- 25
grade1 <- assignGrade (student1_marks)
grade2 <- assignGrade (student2_marks)
grade3 <- assignGrade (student3_marks)
cat ( "Student 1 got:" , student1_marks, "marks. Grade:" , grade1, "\n" )
cat ( "Student 2 got:" , student2_marks, "marks. Grade:" , grade2, "\n" )
cat ( "Student 3 got:" , student3_marks, "marks. Grade:" , grade3, "\n" )
|
Output:
Student 1 got: 45 marks. Grade: Excellent
Student 2 got: 35 marks. Grade: Very Good
Student 3 got: 25 marks. Grade: Good
In the above program, we have declared a function that will print the Grade of the Students according to their Marks using If-else Statement.
Conclusion
In conclusion, if-else programming expressions are a useful tool for automating the grading process based on student achievement. We guarantee fairness and uniformity in the grading process by analysing their results and implementing particular guidelines. These programmes streamline the grading process and save time for teachers, whether they use a predefined grading scale or percentage calculations to determine grades. This strategy improves the precision and objectivity of grade assignment in educational settings while also being effective.
Share your thoughts in the comments
Please Login to comment...