Classes and Objects

Sort by

recency

|

415 Discussions

|

  • + 0 comments

    **For Those Wants To Execute This Code Without Using Vector **

    #include <iostream>
    using namespace std;
    
    // Define the Student class
    class Student {
    private:
        int scores[5]; // Array to hold scores (fixed size for 5 subjects)
    
    public:
        // Function to read scores from input
        void input() {
            for (int i = 0; i < 5; i++) {
                cin >> scores[i];
            }
        }
    
        // Function to calculate the total score
        int calculateTotalScore() {
            int totalScore = 0;
            for (int i = 0; i < 5; i++) {
                totalScore += scores[i];
            }
            return totalScore;
        }
    };
    
    int main() {
        int n; // Number of students
        cin >> n;
    
        Student* students = new Student[n]; // Dynamically allocate array of students
    
        // Input scores for each student
        for (int i = 0; i < n; i++) {
            students[i].input();
        }
    
        // Calculate Kristen's total score (first student)
        int kristenScore = students[0].calculateTotalScore();
    
        // Count how many students scored higher than Kristen
        int count = 0;
        for (int i = 1; i < n; i++) {
            if (students[i].calculateTotalScore() > kristenScore) {
                count++;
            }
        }
    
        // Output the result
        cout << count << endl;
    
        // Clean up dynamic memory
        delete[] students;
    		
    		
    		
    
        return 0;
    }
    
  • + 0 comments
    class Student
    {
    
    private: 
        vector<int> scores; 
    
    public: 
        void input()
        {
            int a, b, c, d, e; 
            cin >> a >> b >> c >> d >> e; 
            scores = { a, b, c, d ,e }; 
        };
        int calculateTotalScore()
        {
            return accumulate(scores.begin(), scores.end(), 0);
        }; 
    
    };
    
  • + 0 comments
    1. include

      using namespace std; class Student{ private: int q1,q2,q3,q4,q5; public: void input(){ cin >> q1 >> q2 >> q3 >> q4 >> q5; } int calculateTotalScore(){ int score; score += q1+q2+q3+q4+q5; return score; }}; int main() { int n; // number of students cin >> n; Student *s = new Student[n]; for(int i = 0; i < n; i++){ s[i].input(); } int kristen_score = s[0].calculateTotalScore(); int count = 0; for(int i = 1; i < n; i++){ int total = s[i].calculateTotalScore(); if(total > kristen_score){ count++; } } cout << count; return 0; }
  • + 0 comments

    Mine wors in my compiler, but when I put the exact same code in the Hackerank compiler, it gives me the wrong answer. Very bizarre:

    class Student
    {
        std::vector<int> scores{};
    
    public:
        void input()
        {
            // If std::cin buffer has '\n', then clear the buffer
            if (std::cin.peek() == '\n') {
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }
    
            std::string str;
    
            //read input and store it in a string "str", note that this includes the newline character '\n';
            std::getline(std::cin, str); //a way to remove the newline from the string
    
            std::stringstream iss( str );
    
            //convert the numbers on the string to an integers and store it as elements of vector str_int
            int score;
            while (iss >> score)
            {
                scores.emplace_back(score);
            }
        }
    
        int calculateTotalScore()
        {
            int total{0};
            for (const auto &num: scores)
            {
                total += num;
            }
            return total;
        }
    };
    
  • + 0 comments

    No locked code int C++20

    That i lost time implementing the parsing and the reading. Fix that shit