Classes and Objects

  • + 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;
    }