HackerRank: Grading Students

HackerLand University has the following grading policy:

  • Every student receives a grade in the inclusive range from to 0 to 100.
  • Any less than 40 is a failing grade.
  • Sam is a professor at the university and likes to round each student’s grade according to these rules:
  • If the difference between the grade and the next multiple of 5 is less than 3, round up to the next multiple of 5.
  • If the value of is less than 38, no rounding occurs as the result will still be a failing grade.
int* gradingStudents(int grades_count, int* grades, int* result_count) {
    int * arr = (int*)malloc(sizeof(int)*grades_count);
    for(int i=0; i<grades_count; i++){
        if(grades[i]<38) {
           arr[i] = grades[i]; 
           }
        else{
        if((grades[i]+2)%5==0 ||(grades[i]+1)%5==0 ){ 
        if((grades[i]+2)%5==0) arr[i] = grades[i] + 2;
        if((grades[i]+1)%5==0) arr[i] = grades[i] + 1;
        }
        else arr[i] = grades[i];
        }
}
*result_count = grades_count;
return arr;
}

Leave a Comment