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

Create a class called TestScoresthat stores test scores (integers) in a vector.

ID: 3848248 • Letter: C

Question

Create a class called TestScoresthat stores test scores (integers) in a vector.

The class should have a function that adds a score to the vector (call it addScore), another that sorts the vector in ascending order (call it sortVector), a third member function that computes the average of the scores in the vector (call it computeAverage), and a fourth member function that prints the scores to the screen.

This is a lab that reinforces pointers. To receive credit for your lab, in any function that requires arguments, those arguments should be declared as pointers in your function headers. At least one of your functions should require an argument.

Write a program that uses the class to perform the following features: [Note: The program should have a menu, and these are the items your menu should include.]

Read scores from a file.

[Note that when the user selects this option, the program should prompt the user to enter a file name to open. It should then open the file and read it into the vector by calling the function that adds a score to the vector. Hint: Make sure that the file exists before trying to read from it.]

Add a score from the keyboard.

[Here, just prompt the user for a score and call a member function to add the score to the vector.]

Sort the scores. [Write your own algorithm for this. Do not use any built-in functions to sort the vector.]

Compute the average score. [Again, write your own algorithm for this.]

Print the scores to the screen.

Exit the program.

If the user chooses to read the scores from a file, you may assume that the file will be formatted such that every line contains a score. For example, an input file might contain:

95

77

88

99

Read scores into your program until the end of file is reached. A file may contain 1 score or a million scores. The program should be robust enough to handle as many scores as are in the file. Negative scores should not be allowed.

Please use C++ to solve this problem.And please use pointer and vector in the code. Thank you

Explanation / Answer

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<fstream>

using namespace std;

class TestScores {
public:
vector<double> scores;
TestScores();
void addScore(vector<double> &v);
void sortVector(vector<double> &v);
double computeAverage(vector<double> &v);
void print(vector<double> &v);
};

TestScores::TestScores()
{
scores.clear();
}

void TestScores::addScore(vector<double> &vec)
{
fstream fi;
fi.open("inp.txt", std::fstream::in);
double score;
int x = vec.size()+1;
while(x--)
fi >> score;
vec.push_back(score);
fi.close();
return;
}

void TestScores::sortVector(vector<double>&vec)
{
sort(vec.begin(), vec.end());
return;
}

double TestScores::computeAverage(vector<double>&vec)
{
double sum = 0.0;
for(int i = 0; i < vec.size(); ++i)
sum += (double)vec[i];
sum /= (double)(1.0 * vec.size());
return sum;
}
void TestScores::print(vector<double> &v)
{
for(int i = 0; i < v.size(); ++i)
cout << v[i] <<" ";
cout <<endl;
}
int main()
{
TestScores a;
int choice;
cout <<"Press 1 for new entry. Press 2 to sort the vector. Press 3 to display average. Press anything else to exit. ";
while(1) {
cout << "Enter Choice ";
cin >> choice;

if(choice == 1) {
a.addScore(a.scores);
a.print(a.scores);
}
else if(choice == 2) {
a.sortVector(a.scores);
a.print(a.scores);
}
else if(choice == 3) {
double val = a.computeAverage(a.scores);
cout <<"Average score is "<<val<<endl;
} else
break;
}
return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote