Recent Post

Classes and Objects.cpp-HackerRank

Problem-Kristen is a contender for valedictorian of her high school. She wants to know how many students (if any) have scored higher than her in the  exams given during this semester.

Create a class named  with the following specifications:

  • An instance variable named  to hold a student's  exam scores.
  • void input() function that reads  integers and saves them to .
  • An int calculateTotalScore() function that returns the sum of the student's scores.

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

Link to the problem:-

Result!

Comments

Popular posts from this blog

Caesar Cipher.c-HackerRank

Bon Appétit.c-HackerRank

Electronics Shop.c-HackerRank