Write a program that calculates the semester grades of students and finds the nearest neighbor of the first student based on their quiz scores.
Input description:
The program first reads an integer: the number of students (maximum 10).
Then, it reads 6 quiz scores (integers between 0 and 10) for each student.
Task1: Semester grades
First drop the highest and lowest quiz scores for each student, then average the remaining four scores to obtain the semester grade.
Task 2: Nearest neighbor of the first student
To find which other student is most similar to the first student, compute the Euclidean distance between the first student’s quiz scores and every other student’s quiz scores:
The nearest neighbor is the student with the smallest Euclidean distance.
If two students have the same distance, choose the one with the smaller student number.
Program Input
Enter the number of students: 3
Enter the scores for each student:
4 1 10 6 1 10
9 2 6 3 8 9
5 0 3 1 7 3
Program Output
Table of Semester Grades:
Stud1 Stud2 Stud3
Grades: 5.2 6.5 3.0
The nearest neighbor of Stud1 is Stud2.
Explanation
Task 1:
Stud1: 4, 1, 10, 6, 1, 10
After drop the highest and lowest quiz scores, we have: Stud1: 4, 1, 10, 6
Hence, the semester grade of Stud1 is (4+1+10+6) / 4 = 5.2
Repeat the above process for each student.
Task 2:
distance(Stud1, Stud2) = sqrt((4-9)^2 + (1-2)^2 + (10-6)^2 + (6-3)^2 + (1-8)^2 + (10-9)^2) = sqrt(101) = 10.05
distance(Stud1, Stud3) = sqrt((4-5)^2 + (1-0)^2 + (10-3)^2 + (6-1)^2 + (1-7)^2 + (10-3)^2) = sqrt(161) = 12.69
Since distance(Stud1, Stud2) < distance(Stud1, Stud3), Stud2 is the nearest neighbor of Stud1.
Remarks
More input-output samples.
Your solution should be based on the code template .
Your program must conform the following requirements:
The outputs of your program should behave as in the above example.
You only need to add your code to the function definitions of computeSemesterGrades() and getNearestNeighborOfStud1()
Do not modify readStudentGrades() and displaySemesterGrades()
Do not modify main() unless you know what you are doing.
Do not modify any cin or cout statements, and do not add any cout statements. Otherwise, your program might not be evaluated properly.
Hint:
You can use sort(array, array+6) to sort an array with size 6.