Create a class called TestScoresthat stores test scores (integers) in a vector.
ID: 3847804 • 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. Thank you!
Explanation / Answer
#include <iostream>
#include<vector>
using namespace std;
class testscores
{
public:
vector<int> vec;
int i;
void addscore(vector<int> v, int size);
void sortvector();
void computeaverage();
void Printscore();
};
void testscores::addscore(vector<int> v, int size){
for (i=0;i<size;i++)
{
vec[i]=v[i]
}
}
void testscores::sortvector(){
vector<int> temp=0;
for (i=0;i<vec.size();i++)
{
if(vec[i]>vec[i+1]){temp=vec[i];
vec[i]=vec[i+1];
vec[i+1]=temp;
}
}
}
void testscores::computeaverage()
{
int sum;
for(i = 0; i < vec.size(); i++)
{ sum += vec.at(i);}
int average=sum/vec.size();
cout<<"The average is:"<<average;
}
void testscores::Printscore(){
vector<int>::iterator v = vec.begin();
while( v != vec.end()) {
cout << "value of v = " << *v << endl;
v++;
}
}
int main()
{
vector<int> v;
int size;
cout<<"Enter size:";
cin>>size;
cout<<"Enter vector";
for(int i = 0; i < 5; i++){
v.push_back(i);
}
testscores t1;
t1.addscore(v, size);
t1.computeaverage();
t1.sortvector();
t1.Printscore();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.