Classes and Objects

Sort by

recency

|

425 Discussions

|

  • + 0 comments

    This is my code if inputs were lines of string: class Student{ public: vector scores; void input(); int calculateTotalScore(); };

    void Student::input(){ string container; cin.ignore(); //delete the first "enter"

    //Parse
    size_t start = 0;
    while(true){
        string temp_container;
        size_t pos = container.find(' ', start);
        //When there's no "space"
        if (pos == string::npos){
            temp_container = container.substr(start);
            scores.push_back(stoi(temp_container));
            break;
        }
        //String parsing
        temp_container = container.substr(start, pos-start);
        start = pos + 1;
        scores.push_back(stoi(temp_container));
    }
    

    } int Student::calculateTotalScore(){ int total =0; for (int i=0; i

  • + 0 comments

    Here is my code!

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <cassert>
    using namespace std;
    
    class Student {
        public:
            int scores[5];
            int sum = 0;
            void input() {
                for (int i = 0; i < 5; i++) {
                    cin >> scores[i];
                }
            }
            int calculateTotalScore() {
                sum = 0;
                for (int i = 0; i < 5; i++) {
                    sum += scores[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;
    }
    
  • + 0 comments
    class Student {
    private:
        int scores[5];
        int totalScore = 0;
        
    public:
        void input()
        {
            for (int i = 0; i < 5; i++)
            {
                std::cin >> scores[i];
                totalScore += scores[i];
            }
        }
        
        int calculateTotalScore()
        {
            return totalScore;
        }
    };
    
  • + 0 comments
    class Student {
        private:
            int scores[5];
        
        public:
            void input() {
                for (int i = 0; i < 5; i++) {
                    cin >> scores[i];
                }
            }
            
            int calculateTotalScore() {
                int sum = 0;
                
                for (int& score : scores) {
                    sum += score;
                }
                
                return sum;
            }
    };
    
  • + 0 comments

    class Student{ private: int scores[5]; public: void input(){ for(int i=0;i<5;i++){ cin>> scores[i]; } } int calculateTotalScore(){ int result = 0; for(int i=0;i<5;i++){ result += scores[i]; } return result; } }; /