Computer >> Computer tutorials >  >> Programming >> C++

Maximum students to pass after giving bonus to everybody and not exceeding 100 marks in C++


In this tutorial, we will be discussing a program to find maximum students to pass after giving bonus to everybody and not exceeding 100 marks.

For this we will be provided with an array containing marks of N students. Our task is to get more student pass the exam (50 marks required) by giving each student the same amount of bonus marks without any student exceeding 100 marks.

Example

#include<iostream>
#include<algorithm>
using namespace std;
int check(int n, int marks[]) {
   int* x = std::max_element(marks,marks+5);
   int bonus = 100-(int)(*x);
   int c = 0;
   for(int i=0;
   i<n;i++) {
      if(marks[i] + bonus >= 50) c += 1;
   }
   return c;
}
int main() {
   int n = 5;
   int marks[] = {0, 21, 83, 45, 64};
   cout<<check(n, marks)<<endl;
   return 0;
}

Output

3