Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ifndef Student_h #define Student_h #include <string> using namespace std; const

ID: 3759288 • Letter: I

Question

ifndef Student_h

#define Student_h

#include <string>

using namespace std;

const int MAX_NUM_GRADES = 20;

class Student {

private:

/**

   * Instance variables that represent a student

   * Marked as private so that they are not able to be changed outside of this

   * class

   */

  

// The full name of this student

string name;

  

// An array of grades recorded for this student

int grades[MAX_NUM_GRADES] = { };

  

// The number of valid grades for this student, used to know the upper bound

// of the grades[] array

int numGrades = 0;

  

public:

/**

   * Requires: name of the student

   * Modifies: name

   * Effects : Student constructor for when only a student name is given.

   * Note that numGrades is 0 and grades[] is all 0's, given by

   * their declarations.

   */

Student(string name);

  

/**

   * Requires: nothing

   * Modifies: name

   * Effects : Default Student constructor. name is set to be an empty string.

   * Note that numGrades is 0 and grades[] is all 0's, given by

   * their declarations.

   */

Student();

  

/**

   * Requires: nothing

   * Modifies: nothing

   * Effects : Returns the name of this Student instance.

   */

string getName();

  

/**

   * Requires: nothing

   * Modifies: nothing

   * Effects : Returns the number of grades contained by this Student instance.

   */

int getNumGrades();

  

/**

   * Requires: An integer (score) representing the grade to add, 0 to 100 inclusive

   * Modifies: grades[] and numGrades

   * Effects : Adds the passed score to the list of grades for this Student

   * instance and increments the number of grades that this Student

   * instance contains.

   */

void addGrade(int score);

  

/**

   * Requires: nothing

   * Modifies: nothing

   * Effects : Returns the lowest grade in the list of grades for this

   * Student instance. Returns -1 if there are no grades.

   */

int getLowestGrade();

  

/**

   * Requires: nothing

   * Modifies: nothing

   * Effects : Returns the highest grade in the list of grades for this

   * Student instance. Returns -1 if there are no grades.

   */

int getHighestGrade();

  

/**

   * Requires: nothing

   * Modifies: nothing

   * Effects : Calculates and returns the average grade given all of this

   * Student instance's grades. Returns 0 if there are no grades.

   */

double getAverageGrade();

  

/**

   * Requires: nothing

   * Modifies: nothing

   * Effects : Returns a string, suitable for printing, that contains this

   * Student instance's name, average grade, and the number of

   * grades stored.

   */

string printableSummary();

};

#endif /* Student_h */

#include "Student.h"

#include <string>

using namespace std;

Student::Student(string studentName) {

name = studentName;

}

  

Student::Student() {

name = "";

}

  

string Student::getName() {

// YOUR CODE HERE

return ""; // <--- delete this

}

int Student::getNumGrades() {

// YOUR CODE HERE

return 0; // <--- delete this

}

void Student::addGrade(int score) {

// YOUR CODE HERE - 2 lines

}

int Student::getLowestGrade() {

// YOUR CODE HERE - approximately 7 lines

return 0; // <--- delete this

}

int Student::getHighestGrade() {

// YOUR CODE HERE - very similar to getLowestGrade()

return 0; // <--- delete this

}

double Student::getAverageGrade() {

int total = 0;

  

for (int grade = 0; grade < numGrades; ++grade) {

total += grades[grade];

}

  

// Handle special case of no grades

if (numGrades == 0) {

return 0;

}

  

return ((double) total) / numGrades;

}

string Student::printableSummary() {

return getName() + " " + to_string((int)getAverageGrade()) + " average, given " + to_string(numGrades) + " grades";

}

Explanation / Answer

// Student.h

ifndef Student_h
#define Student_h

#include <string>

using namespace std;

const int MAX_NUM_GRADES = 20;

class Student {
private:
/**
* Instance variables that represent a student
* Marked as private so that they are not able to be changed outside of this
* class
*/
  
// The full name of this student
string name;
  
// An array of grades recorded for this student
int grades[MAX_NUM_GRADES] = { };
  
// The number of valid grades for this student, used to know the upper bound
// of the grades[] array
int numGrades = 0;
  
public:
/**
* Requires: name of the student
* Modifies: name
* Effects : Student constructor for when only a student name is given.
* Note that numGrades is 0 and grades[] is all 0's, given by
* their declarations.
*/
Student(string name);
  
/**
* Requires: nothing
* Modifies: name
* Effects : Default Student constructor. name is set to be an empty string.
* Note that numGrades is 0 and grades[] is all 0's, given by
* their declarations.
*/
Student();
  
/**
* Requires: nothing
* Modifies: nothing
* Effects : Returns the name of this Student instance.
*/
string getName();
  
/**
* Requires: nothing
* Modifies: nothing
* Effects : Returns the number of grades contained by this Student instance.
*/
int getNumGrades();
  
/**
* Requires: An integer (score) representing the grade to add, 0 to 100 inclusive
* Modifies: grades[] and numGrades
* Effects : Adds the passed score to the list of grades for this Student
* instance and increments the number of grades that this Student
* instance contains.
*/
void addGrade(int score);
  
/**
* Requires: nothing
* Modifies: nothing
* Effects : Returns the lowest grade in the list of grades for this
* Student instance. Returns -1 if there are no grades.
*/
int getLowestGrade();
  
/**
* Requires: nothing
* Modifies: nothing
* Effects : Returns the highest grade in the list of grades for this
* Student instance. Returns -1 if there are no grades.
*/
int getHighestGrade();
  
/**
* Requires: nothing
* Modifies: nothing
* Effects : Calculates and returns the average grade given all of this
* Student instance's grades. Returns 0 if there are no grades.
*/
double getAverageGrade();
  
/**
* Requires: nothing
* Modifies: nothing
* Effects : Returns a string, suitable for printing, that contains this
* Student instance's name, average grade, and the number of
* grades stored.
*/
string printableSummary();
};

#endif /* Student_h */

// Student.cpp

#include "Student.h"
#include <string>

using namespace std;

Student::Student(string studentName) {
name = studentName;
}
  
Student::Student() {
name = "";
}
  
string Student::getName() {
// YOUR CODE HERE
return name;
}

int Student::getNumGrades() {
// YOUR CODE HERE
return numGrades;
}

void Student::addGrade(int score) {
// YOUR CODE HERE - 2 lines
grades[numGrades] = score;
numGrades++;
}

int Student::getLowestGrade() {
// YOUR CODE HERE - approximately 7 lines
if(numGrades == 0) return -1;
int lowestGrade = grade[0];
for(int i = 1; i < numGrades; i++){
if(grade[i] < lowestGrade){
lowestGrade = grade[i];
}
}
return lowestGrade;
}

int Student::getHighestGrade() {
// YOUR CODE HERE - very similar to getLowestGrade()
if(numGrades == 0) return -1;
int highestGrade = grade[0];
for(int i = 1; i < numGrades; i++){
if(grade[i] > highestGrade){
highestGrade = grade[i];
}
}
return highestGrade;
}

double Student::getAverageGrade() {
int total = 0;
  
for (int grade = 0; grade < numGrades; ++grade) {
total += grades[grade];
}
  
// Handle special case of no grades
if (numGrades == 0) {
return 0;
}
  
return ((double) total) / numGrades;
}

string Student::printableSummary() {
return getName() + " " + to_string((int)getAverageGrade()) + " average, given " + to_string(numGrades) + " grades";
}