I'm practicing C++ on HackerRank and came across this problem. I created the class pretty well but struggled to create n student objects I can work with correctly. I searched for similar problems (this is an example) but could not find help, then I came across this solution. It worked well, but I honestly don't understand what exactly happened in line 37 of the solution. I went to cppinsights.io
to see for myself and got the output below.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Student
{
public:
inline void input()
{
for(int i = 0; i < 5; ++i) {
std::cin.operator>>(this->scores[i]);
}
}
inline int total_score()
{
int sum = 0;
for(int i = 0; i < 5; ++i) {
sum = (sum + this->scores[i]);
}
return sum;
}
private:
int scores[5];
public:
// inline constexpr Student() noexcept = default;
};
int main()
{
int n;
std::cin.operator>>(n);
Student * s = new Student []();
for(int i = 0; i < n; ++i) {
s[i].input();
}
int kristen_score = s[0].total_score();
int count = 0;
for(int i = 1; i < n; ++i) {
if(s[i].total_score() > kristen_score) {
count++;
}
}
std::cout.operator<<(count).operator<<(std::endl);
return 0;
}
Can someone help me explain Student *s = new Student[n];
?
How would I do it if my class has a parameterized constructor taking student name or ID as argument. I'm asking this because this problem assumes Kristen's scores to be the first input all the time, but I'd like to compare any student with his peers. Thank you.