Implement the functions to work with a student. Only submit student.h and studen
ID: 3710144 • Letter: I
Question
Implement the functions to work with a student. Only submit student.h and student.cpp.
Function Descriptionvoid Initialize(Student&);This function takes a student as parameter and initializes the values of the grades array to zero
void WriteStudent(const Student&, ostream&);Sends the formatted contents of a student to the provided ostream
void ReadStudent(Student&, istream&);Reads the id, major and three grades from the provided istream
int GetGrade(const Student&, size_t);The second parameter specifies which grade of the student to return, need to validate that the second parameter value is between 0 and 2.
int SetGrade(Student&, size_t, int);Sets the grade of the student. The second parameter specifies what grade and the third parameter specifies the value.
int GetAverage(const Student&);Calculates the average of the three grades of the student parameter, returns this average.
Explanation / Answer
student.h
#pragma once
#include <iostream>
// Using the namespace std
using namespace std;
// Student structure
struct Student{
string id;
string name;
int grade[3]; // Array of grades
};
// Functions implemented in student.cpp
void Initialize(Student&);
void WriteStudent(const Student&, ostream&);
void ReadStudent(Student&, istream&);
int GetGrade(const Student&, size_t);
int SetGrade(Student&, size_t, int);
int GetAverage(const Student&);
student.cpp
#include "student.h"
// This function takes a student as parameter and initializes the values of the grades array to zero
void Initialize(Student& s) {
// Iterates over the 3 grades, and sets them 0
for (int i = 0; i < 3; i++) s.grade[i] = 0;
}
// Sends the formatted contents of a student to the provided ostream
void WriteStudent(const Student& s, ostream& os) {
// Send the string "Id: ", the student id and a new line character to ostream
os<<"Id: "<<s.id<<endl;
// Send the string "Name: ", the student name and a new line character to ostream
os<<"Name: "<<s.name<<endl;
// Iterates over the 3 grades and
// Send the string "Grade: ", the integer i, the student's ith grade and a new line character to ostream
for (int i = 0; i < 3; i++) os<<"Grade "<<i<<": "<<s.grade[i]<<endl;
}
// Reads the id, major and three grades from the provided istream
void ReadStudent(Student& s, istream& is) {
// Prints "Enter id: " on the screen and reads the student id from istream
cout<<"Enter id: ";
is>>s.id;
// Prints "Enter name: " on the screen and reads the student name from istream
cout<<"Enter name: ";
is>>s.name;
// Iterates over the 3 grades and
// Prints "Grade ", the integer i, and ": " on the string, and
// Reads the student's ith grade from istream
for (int i = 0; i < 3; i++) {
cout<<"Grade "<<i<<": ";
is>>s.grade[i];
}
}
// The second parameter specifies which grade of the student to return
int GetGrade(const Student& s, size_t n) {
// If the second paramet is not between 0 and 2, Print "Invalid parameter" and return -1
if (n < 0 || n > 2) {
cout<<"Invalid parameter! ";
return -1;
}
// Return the nth grade
return s.grade[n];
}
// Sets the grade of the student.
// The second parameter specifies what grade and the third parameter specifies the value.
int SetGrade(Student& s, size_t n, int g) {
// If the second paramet is not between 0 and 2, Print "Invalid parameter" and return -1
if (n < 0 || n > 2) {
cout<<"Invalid parameter! ";
return -1;
}
// Set the nth grade as g, and return g
s.grade[n] = g;
return g;
}
// Calculates the average of the three grades of the student parameter, returns this average.
int GetAverage(const Student& s) {
// Initialize the average to 0
int avg = 0;
// Iterate over the 3 grades, and store the sum to avg
for (int i = 0; i < 3; i++) avg += s.grade[i];
// Find the mean by dividing them sum (in avg) by 3, and returning avg
avg /= 3;
return avg;
}