In: Computer Science
In Visual Studios 2017, using c++, Create a class (Scores) that stores test scores (integers) in an array (assume a maximum of 100 scores). The class should have a constructor that allows the client to specify the initial value of the scores (the same initial value applies to all of them) and another default constructor that initializes them to 0. The class should have functions as follows:
1. A member function that adds a score to the array. The client must not pass the array index to the object. This member function returnsan error indication if the value is invalid – i.e. negative test scores are not permitted.
2. A member function to sort the array in ascending order.
3. A member function to compute the average of the scores in the array, and
4. A member function to display the scores.
Note that you will need to be careful about sorting the array and computing the average of the scores. Because your constructor will initialize all members of the array to be a value, you do not want to include these values in your average calculation nor in your sort function. Write a program (client) that uses the class by creating a Scores object and prompting the user for a file name. Appropriate error checking is required to ensure that the file exists and can be opened successfully. The client should read the file contents and store them in the object. The file will be formatted so that the first line contains the number of scores, and each successive line contains a score. A typical input file might contain:
4
90
85
99
94
The client should read and store the contents of the file, sort and display the scores, and compute and display the average score. All output should be labeled appropriately, and validity checking should be done on input and storage of the data. If an error is encountered in the input, the program should output an error message indicating which item was invalid, and the entire program should then terminate.
The executing program should look something like this:
In the screen above, notice that the average is displayed with decimal points. Whenever you are calculating an average, nevertruncate the decimal portion until you are told to do so. In this lab, you are not to truncate the average.
If a negative score is read in, the program might look something like this:
If a non-numeric score is read in, the program might look something like this:
In the screen above, notice that the program pauses long enough to read the error message that is displayed.
Use good coding style (modular, separate .h and .cpp class files, no globals, meaningful comments, etc.) throughout your program.
If you have any doubts, please give me comment...
Scores.h
#pragma once
#include<iostream>
using namespace std;
class Scores {
private:
int scoresList[100];
int size;
public:
Scores();
Scores(int list);
bool addScore(int val);
void sortScore();
double averageScores();
void displayScores();
};
Scores.cpp
#include "Scores.h"
Scores::Scores(int val) {
size = 0;
for (int i = 0; i < 100; i++) {
scoresList[i] = val;
}
}
Scores::Scores() {
size = 0;
for (int i = 0; i < 100; i++) {
scoresList[i] = 0;
}
}
bool Scores::addScore(int val) {
if (val < 0) {
return false;
} else {
scoresList[size] = val;
size++;
return true;
}
}
void Scores::sortScore() {
// This Logic will Sort the Array of elements in Ascending order
int temp;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (scoresList[i] > scoresList[j]) {
temp = scoresList[i];
scoresList[i] = scoresList[j];
scoresList[j] = temp;
}
}
}
}
double Scores::averageScores() {
double total = 0;
for (int i = 0; i < size; i++) {
total = total + scoresList[i];
}
return ((double)total) / size;
}
void Scores::displayScores() {
for (int i = 0; i < size; i++) {
cout << scoresList[i] << endl;
}
}
scoresDriver.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Scores.h"
using namespace std;
int main() {
// Define local variables
int score, num;
string inputFile;
// uses inData for input file stream
ifstream inData;
cout << "Enter the input file :";
// getline is used for if user enters a space for a file name
getline(cin, inputFile);
inData.open(inputFile.c_str());
// checks to see if the file is valid
if (inData.fail()) {
cout << "File Not Found";
return 1;
}
inData >> num;
if (num < 0) {
cout << "Invalid Score Entered: " << num<<endl;
return 0;
}
Scores scoreslist;
for (int i = 0; i < num; i++) {
inData >> score;
bool value = scoreslist.addScore(score);
if (!value)
cout << "Invalid score entered :" << num << endl;
}
cout<<setprecision(2)<<fixed;
scoreslist.sortScore();
cout << "Displaying Scores After Sorting :" << endl;
scoreslist.displayScores();
cout << "Average Score :" << scoreslist.averageScores() << endl;
// system("pause");
return 0;
}