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

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

ID: 3843526 • Letter: C

Question

Create a class called TestScores that 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.

Use appropriate error checking. Integers are expected as scores; therefore, make sure that the user enters appropriate data types. Negative scores should not be allowed.

Explanation / Answer

#include<iostream>
#include <string>
#include<fstream>
#include<stdlib.h>
using namespace std;
//Class TestScore Definition
class TestScores
{
//Declaration of data member of type pointer
int *data;
public:
//Reads data from the specified file and returns the length
int readData()
{
int no, counter = 0;
//Initializes the pointer
data = new int;
//Declares an array to store file name
char fileName[20] = "";
//Accepts the file name
cout<<" Enter File Name: ";
cin.getline(fileName, 20);
//Declares a file pointer
FILE *rFile;
//Opens the file in read mode
rFile = fopen(fileName,"r");
//If file does not exist show error message and exit
if (rFile == NULL)
{
cout << " Error!";
exit(0);
}//end of if

//Loops till end of file
while (!feof(rFile))
{
//Read data from file
fscanf(rFile, "%d", &no);
//Try block to check negative number
try
{
//If the number is negative throw an exception
if(no < 0)
throw no;
else
//Otherwise store the data
*(data + counter++) = no;
}//End of try block
//Handle the exception
catch(int x)
{
cout<<" ERROR: Number cannot be negative: "<<x;
}//End of catch block
}//end of while
counter--;
//Displays the Numbers
cout<<" Numbers are: ";
for(int x = 0; x < counter; x++)
cout<<*(data + x)<<" ";
//Returns the length
return counter;
}//End of function

//Function to calculate total and return it
int addScore(int *n)
{
//Initially tot is zero
int tot = 0;
//Calculates the total
for(int x = 0; x < *n; x++)
tot += data[x];
//Returns the total
return tot;
}//End of function

//Calculates average and returns it
float computeAverage(int *n)
{
int tot = 0;
float avg;
//Calculates total
for(int x = 0; x < *n; x++)
tot += data[x];
//Calculates average
avg = tot / *n;
//Returns average
return avg;
}//End of function

//Function to sort
void sortVector(int *n)
{
int Temp, x, y;
//Loops till end of data
for(x = 0; x < *n; x++)
{
//Loops till end of data - 1 position
for(y = 0; y < *n - x - 1; y++)
{
//If the current position of data is greater than the next data then swap
if(data[y] > data[y + 1])
{
//Swapping operation
Temp = data[y];
data[y] = data[y + 1];
data[y + 1] = Temp;
}//End of if
}//End of inner loop
}//End of outer loop
//Displays the sorted data
cout<<" After Sorting: "<<endl;
for(int x = 0; x < *n; x++)
cout<<data[x]<<" ";
}//End of function
};//End of class

//Main function
int main()
{
//Creates an object of class
TestScores t;
int len;
//Calls the methods
len = t.readData();
cout<<" Total = "<<t.addScore(&len);
cout<<" Average = "<<t.computeAverage(&len);
t.sortVector(&len);
}//End of main

Output:

Enter File Name: MyData.txt

ERROR: Number cannot be negative: -3
Numbers are:
10 56 23 55
Total = 144
Average = 36
After Sorting:
10 23 55 56

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