-1

I have a class named Student, where I have the value of their grade as well as the value of the average grade.

I need to find the Student whose grade is closest to the average.

I have this:

void close (student s, int n){
    int i, sum=0, averageGrade = 1;
    for(i=0; i<n; i++)
       sum = sum + s.GetAverage();
    averageGrade = sum / n;

As an example, let's say that the average grade within all the student is 7.1. My task is to find the nearest grade of a single student that is closest to that value. Let's say John has an average grade of 7.2, Jim has 7.4 and Michael has 8.1. I need to find a way of finding the nearest value of the average grade between students and print their details. If multiple students are closest, I only need to find one student.

Tas
  • 7,023
  • 3
  • 36
  • 51
  • Maybe some numerical examples would help to make it clear what you mean – M.M Jun 13 '16 at 04:43
  • So you compute the average grade, then you need to find the student whose grade is closest to average (be it above or below)? – Tas Jun 13 '16 at 04:45
  • Thanks for responding, I will edit my post right now. – Miodrag Karalejic Jun 13 '16 at 04:45
  • `std::min_element` with a custom comparator. – LogicStuff Jun 13 '16 at 04:46
  • @LogicStuff, is there any other way of doing that? Cause, at our university, we are still at the 'basic level' and it's not expected of us to use susch commands – Miodrag Karalejic Jun 13 '16 at 04:50
  • [This](http://stackoverflow.com/questions/1042507/finding-smallest-value-in-an-array-most-efficiently). Just give it different comparison logic - `if(std::abs(averageGrade - current_student.GetAverage()) < std::abs(averageGrade - closest_student_so_far.GetAverage()) { closest_student_so_far = current_student; }`. – LogicStuff Jun 13 '16 at 04:51
  • @Tas, Exactly like that. – Miodrag Karalejic Jun 13 '16 at 04:51
  • @LogicStuff, I am not supposed to find the smallest, but the closest value to one. There can be smaller values than the average grade, and I am supposed to find the closest one. – Miodrag Karalejic Jun 13 '16 at 04:52
  • @MiodragKaralejic You're looking for the _smallest_ difference with the average. – Bart van Nierop Jun 13 '16 at 04:54
  • What happens if multiple students have a grade closest to the average? e.g. average is 7.1, one student has 7.3 and one student has 6.9 – Tas Jun 13 '16 at 04:54
  • @Tas that's not defined in the task, so basically, just the first one that's been found. – Miodrag Karalejic Jun 13 '16 at 04:57
  • I have [edited your question](http://stackoverflow.com/revisions/37781900/3) to hopefully be more clear, as well as include extra information you provided in the comments. If you feel I've changed the meaning, or you'd like to add more information, you can also [edit] your question! – Tas Jun 13 '16 at 05:00

2 Answers2

4

Start out by assuming that the first student is the one that's closest.

Then, go through all students other than the first one. If that student is closer than your current best, switch your current best to that student.

When you're finished, the student you think is the best will be the actual best one.

So:

1) Create two variables, one which tracks which student we think is the best, called "current_best" and one that track hows close that student is to the average, called "current_diff".

2) Set current_best to the first student and current_diff to the first student's difference from the average.

3) Loop over all the students but the first. For each student, check if that student is closer to the average than current_diff. If so, set current_best to that student and current_diff to that student's difference from the average.

4) The current_best indicates the student whose score is closest to the average.

Note that ties are not handled.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

Did you try this. After the average grade is calculated, run a loop finding the absolute difference between each student's individual average and the average grade. Store a 2D array with student index and absolute difference. Then sort the array with respect to absolute difference. Pick out the corresponding student Id.