Modify the program as follows: a) Change the array for test scores (score[]) to
ID: 3847486 • Letter: M
Question
Modify the program as follows:
a) Change the array for test scores (score[]) to dynamic array. Add a private member variable called number_of_scores to store how many tests a student took.
b) Modify the mutator function (setVars) to set the value for number_of_scores and test scores to integrate the dynamic array definition. Prompt the user for the value of number_of_scores.
c) Write a destructor function to clear the dynamic array for test scores.
d) Write a copy constructor function that creates a duplicate copy of a Student object.
e) In your main function, create a dynamic array of Student objects. Prompt the user for the size of the dynamic array.
f) Demonstrate the accessor and mutator functions for the Student object.
Code so far:
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student();
void setVars(long int new_id, string new_name, int new_score[], double new_average);
void getVars();
private:
long int id;
string name;
int score[4];
double average;
};
Student::Student()
{
}
void Student::setVars(long int new_id, string new_name, int new_score[], double new_average)
{
double total = 0.0;
cout << "Enter ID: ";
cin >> new_id;
cin.ignore();
cout << "Enter Name: ";
getline(cin, new_name);
cout << "Enter 5 test scores." << endl;;
for (int i = 0; i < 5; i++)
{
cout << "Test " << i+1 << ": ";
cin >> new_score[i];
score[i] = new_score[i];
total += new_score[i];
}
new_average = total/5;
id = new_id;
name = new_name;
average = new_average;
}
void Student::getVars()
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
for (int i = 0; i < 5; i++)
{
cout << "Test " << i+1 << ": " << score[i] << endl;
}
cout << "Average: " << average << endl;
}
int main()
{
long int new_id;
string new_name;
int new_score[4];
double new_average = 0;
cout << endl;
Student student[4];
for (int i = 0; i < 5; i++)
{
cout << "Student " << i+1 << endl;
student[i].setVars(new_id, new_name, new_score, new_average);
cout << endl;
cout << "Student " << i+1 << endl;
student[i].getVars();
cout << endl;
}
return 0;
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student();
void setVars(long int new_id, string new_name, int new_score[], double new_average);
void getVars();
private:
long int id;
string name;
int *score;
int number_of_scores;
double average;
};
Student::Student()
{
}
Student::Student(Student other) {
id = other.id;
name = other.name;
number_of_scores = other.number_of_scores;
average = other.average;
score = new int[number_of_score];
for (int i = 0; i < number_of_score; i++)
score[i] = other.score[i];
}
Student::~Student() {
if (score != NULL)
delete[] score;
}
void Student::setVars(long int new_id, string new_name, int *new_score, double new_average)
{
double total = 0.0;
cout << "Enter ID: ";
cin >> new_id;
cin.ignore();
cout << "Enter Name: ";
getline(cin, new_name);
cout << "Enter number of test scores to consider:";
cin >> number_of_scores;
scores = new int[number_of_scores];
new_score = new int[number_of_scores];
cout << "Enter "<<number_of_scores<<" test scores." << endl;
for (int i = 0; i < number_of_scores; i++)
{
cout << "Test " << i+1 << ": ";
cin >> new_score[i];
score[i] = new_score[i];
total += new_score[i];
}
new_average = total/number_of_scores;
id = new_id;
name = new_name;
average = new_average;
}
void Student::getVars()
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
for (int i = 0; i < number_of_scores; i++)
{
cout << "Test " << i+1 << ": " << score[i] << endl;
}
cout << "Average: " << average << endl;
}
int main()
{
long int new_id;
string new_name;
int *new_score;
double new_average = 0;
int number_of_students;
cout << endl;
cout << "Enter number of students: ";
cin >> number_of_students;
Student *student;
student = new Student[number_of_students];
for (int i = 0; i < number_of_students; i++)
{
cout << "Student " << i+1 << endl;
student[i].setVars(new_id, new_name, new_score, new_average);
cout << endl;
cout << "Student " << i+1 << endl;
student[i].getVars();
cout << endl;
}
return 0;
}
I have edited the program to meet all your requirments. Parts in bold are are what I have edited.
As a guide I want to point out a logical error with the code:
Let me know if you need any help. Shall try my best to help you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.