You are viewing a single comment's thread. Return to all comments →
C++20 version with std::accumulate. Does not hard-code 5 as # scores per student.
std::accumulate
const unsigned NUM_SCORES = 5; #include <numeric> class Student { public: void input( vector<int>& scores ) { scores_ = scores; } int calculateTotalScore() { return accumulate( scores_.begin(), scores_.end(), 0 ); } private: vector<int> scores_; }; int main() { vector<int> scores( NUM_SCORES ); int q, better=0; cin >> q; int total[q]; Student s; for( int i=0; i<q; i++ ) { for( int j=0; j<NUM_SCORES; j++ ) cin >> scores[j]; s.input( scores ); total[i] = s.calculateTotalScore(); } for( int i=1; i<q; i++ ) if( total[i]>total[0] ) better++; cout << better << endl; }
Classes and Objects
You are viewing a single comment's thread. Return to all comments →
C++20 version with
std::accumulate
. Does not hard-code 5 as # scores per student.