- Prepare
- C++
- Classes
- Classes and Objects
- Discussions
Classes and Objects
Classes and Objects
+ 0 comments C++20 version with
std::accumulate
. Does not hard-code 5 as # scores per student.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; }
+ 0 comments include
- using namespace std;
- class student {
- private:
- int a,b,c,d,e;
- int total;
- public:
- void get_input(){
- cin >> a >> b >> c >> d >> e;
- }
- int calculateTotalScore(){
- total = a + b + c + d + e;
- return total;
- }
- };
- int main() {
- int i,n,count;
- student s;
- cin >> n;
- int a[n];
- for (i = 0 ; i < n ; i++){
- s.get_input();
- a[i] = s.calculateTotalScore();
- }
- for ( i = 1 ; i < n ; i++){
- if(a[0] < a[i])
- count++;
- }
- cout << count << endl;
- }
+ 0 comments include
include
include
include
include
using namespace std; class student{ private: int marks,b,gre=0; public: void sum(int n) {b=n; int ar[b]; for(int i=0;i>marks; ar[i]+=marks; } } for(int i=0;i } cout<>n; obj.sum(n); return 0; }
+ 0 comments include
include
include
include
include
using namespace std; class student{ private: int marks,b,gre=0; public: void sum(int n) {b=n; int ar[b]; for(int i=0;i>marks; ar[i]+=marks; } } for(int i=0;i } cout<>n; obj.sum(n); return 0; }
+ 0 comments include
include
include
include
include
using namespace std;
class Student{ public: int a,b,c,d,e; void input(); int calculateTotalScore();
};void Student :: input(){ cin>>a>>b>>c>>d>>e; }
int Student :: calculateTotalScore(){ int total = a+b+c+d+e; return total; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT / int n; cin>>n; Student S = new Student[n]; S[0].input(); int Kristen = S[0].calculateTotalScore(); int count = 0; for(int i = 1; i < n; i++){ S[i].input(); if(Kristen < S[i].calculateTotalScore()){ count++; } } cout<
Sort 365 Discussions, By:
Please Login in order to post a comment