Passing grade#2691

Passing grade (Editorial)

For each student, we compute the average grade — the sum of their annual grades divided by the number of subjects m_i. A student can be admitted to the school if their average grade is at least the passing grade .

The value is given only in the last line of the input, so first read the descriptions of all students and store the last name, first name and average grade of each of them. Then read and print the students whose average grade is at least . Since the students are processed in the order they appear in the input, the required output order is preserved automatically.

Example

The passing grade is k = 8. Let us compute the average grade of each student:

Thus, Ivanov Ivan and Sidorov Sidor can be admitted to the school; we print them in the order they appear in the input.

Algorithm implementation

Let us declare the arrays. The first and second words of the description of the -th student (the last name and first name) are stored in and surn[i], and the student's average grade is stored in .

#define MAX 10000
char name[MAX][21], surn[MAX][21];
double res[MAX];

Read the number of students .

scanf("%d", &n);

Process the students one by one. For the -th student, read the last name, first name and the number of subjects studied .

for (i = 0; i < n; i++)
{
  scanf("%s %s %d", &name[i], & surn[i], &m);

Read the annual grades of the student and accumulate their sum in .

  res[i] = 0;
  for (j = 0; j < m; j++)
  {
    scanf("%d", &x);
    res[i] += x;
  }

Divide the sum of the grades by the number of subjects — now contains the average grade of the -th student.

  res[i] /= m;
}

Read the passing grade .

scanf("%d", &k);

Print, in the order they appear in the input, the last names and first names of the students whose average grade is at least .

for (i = 0; i < n; i++)
  if (res[i] >= k) printf("%s %s\n", name[i], surn[i]);