We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C++
- Classes
- Classes and Objects
- Discussions
Classes and Objects
Classes and Objects
+ 1 comment #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; class Grades{ public: void input(int score1, int score2, int score3, int score4, int score5){ this->score1 = score1; this->score2 = score2; this->score3 = score3; this->score4 = score4; this->score5 = score5; setSum(score1,score2, score3, score4,score5); } void setSum(int score1, int score2, int score3, int score4, int score5){ this->sum = this->score1 + this->score2 + this->score3 + this->score4 + this->score5; } int get_calculateSum(){ return this->sum; } private: int score1, score2,score3,score4, score5, sum; }; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int s1,s2,s3,s4,s5, n; int count = 0; cin >> n; cin >> s1 >> s2 >> s3 >> s4 >> s5; Grades Kristen; Kristen.input(s1,s2,s3,s4,s5); int Ksum = Kristen.get_calculateSum(); //cout << Ksum << endl; //should be 135 for(int i = 0; i< n-1; i++){ Grades others; cin >> s1 >> s2 >> s3 >> s4 >> s5; others.input(s1,s2,s3,s4,s5); int Osum = others.get_calculateSum(); //first one should be 140 //second should be 120 //cout << Osum << endl; if( Osum> Ksum){ count ++; } } cout << count << endl; return 0; } /* inputs: 3 30 40 45 10 10 40 40 40 10 10 50 20 30 10 10 */
+ 0 comments Easy C++ code || Class and Objects || With Proper guidlines
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; class Student { private: int score1, score2, score3, score4, score5; public: void input(int score1, int score2, int score3, int score4, int score5) { this->score1 = score1; this->score2 = score2; this->score3 = score3; this->score4 = score4; this->score5 = score5; } int calculateTotalScore() { return this->score1+this->score2+this->score3+this->score4+this->score5; } }; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; cin >> n; int s1, s2, s3, s4, s5; cin >> s1 >> s2 >> s3 >> s4 >> s5; Student Kristen; Kristen.input(s1, s2, s3, s4, s5); int KristenScore = Kristen.calculateTotalScore(); int cnt = 0; n--; while (n-- > 0) { Student others; cin >> s1 >> s2 >> s3 >> s4 >> s5; others.input(s1, s2, s3, s4, s5); if(others.calculateTotalScore() > KristenScore) cnt++; } cout << cnt; return 0; }
+ 0 comments `#include
using namespace std;
class Student { private:
int scores[5]; public: void input(); int calculateTotalScore(); };void Student::input() { for(int i = 0; i < 5; i++) { cin >> scores[i]; } }
int Student::calculateTotalScore() { int sum = 0; for(int i = 0; i < 5; i++) { sum += scores[i]; } return sum; }
int main() { int n; cin >> n; Student a[n]; for(int i = 0; i < n; i++) { a[i].input(); } int cnt = 0; for(int j = 1; j < n; j++) { if(a[j].calculateTotalScore() > a[0].calculateTotalScore()) cnt++; } cout << cnt; return 0; } `
+ 0 comments class student{ public: int arr[5]; int TotalScore(student a){ int total=0; for(int i=0;i<5;i++)total+=arr[i]; return total; } }; int main() { int n; cin>>n; student Kristen; for(int i=0;i<5;i++){ int temp; cin>>temp; Kristen.arr[i]=temp; } int KristenMarks=Kristen.TotalScore(Kristen); n--; int cnt=0; for(int i=1; i<=n;i++){ student s; for(int i=0;i<5;i++){ int temp; cin>>temp; s.arr[i]=temp; } int curr_marks=s.TotalScore(s); if(curr_marks>KristenMarks)cnt++; } cout<<cnt; return 0; }
+ 0 comments #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <cassert> using namespace std; class Student{ private: int grades[5]; public: void input(){ for (int i = 0; i < 5; i++){ cin >> grades[i]; } } int calculateTotalScore(){ int sum = 0; for (int i = 0; i < 5; i++){ sum += grades[i]; } return sum; } }; int main() { int n; // number of students cin >> n; Student *s = new Student[n]; // an array of n students for(int i = 0; i < n; i++){ s[i].input(); } // calculate kristen's score int kristen_score = s[0].calculateTotalScore(); // determine how many students scored higher than kristen int count = 0; for(int i = 1; i < n; i++){ int total = s[i].calculateTotalScore(); if(total > kristen_score){ count++; } } // print result cout << count; return 0; }
Load more conversations
Sort 390 Discussions, By:
Please Login in order to post a comment