So I have this c++ assignment and I can\'t seem to implement the correct code. H
ID: 640186 • Letter: S
Question
So I have this c++ assignment and I can't seem to implement the correct code. Here is the assignment along with what I have and the genneral format:
Two input files contain unsorted lists of student names and associated raw scores. Write a program that will read this information into two parallel arrays. Implement separate methods that:
(1) compute the average and store the result in private data member Avg
(2) compute the standard deviation and store the result in private data member StDev
(3) compute the median (middle score by position) and store the result in private data member Median
(4) sort the names/scores in descending order (highest score in the first array location) using a variation of the Bubble Sort algorithm
Explanation / Answer
Please share your Even.dat and Odd.dat file to validate and test the code. I have created my own and it looks good. You can do the test of your own if any issues let me know.
#include<iostream>
#include<string>
#include<fstream>
#include<cmath>
#include<sstream>
using namespace std;
class StudentStat
{
private:
int Size;
string Names[20];
int Scores[20];
double Avg, StDev, Median;
void Sortem();
void Calc_Avg();
void Calc_StDev();
void Calc_Median();
StudentStat(); // hide the default constructor from user
public:
StudentStat(char *); // Argument is input filename
string getName(int); // return the name at position on the list
int getScore(int); // return score at position on the list
double getStatistic(string); // retrieve "Avg", "StD", "Med", "High", "Low", "Range"
};
StudentStat::StudentStat(char * fname)
{
Avg = StDev = Median = -1.0; // Initialize values before reading
Size = 0;
// TODO: READ DATA FROM INPUT FILE
//Read student file
ifstream inputFile(fname);
string line;
while (!inputFile.eof())
{
getline(inputFile, line);
istringstream ss(line);
ss >> Names[Size] >> Scores[20];
Size++;
if(Size==20) {
cout<<"Can't store any more student scores. Max limit of 20 lines reached.";
break;
}
}
// Sort the names/scores in descending order
Sortem();
}
string StudentStat::getName(int index) // return the name at position i on the list, make sure index is valid, return "" if index is not valid
{
// TODO: ADD LOGIC HERE
if(index>=0 and index<Size)
return Names[index];
else
return "";
}
int StudentStat::getScore(int index) // return the score at position i on the list, make sure index is valid, return -1 if index is not valid
{
// TODO: ADD LOGIC HERE
if(index>=0 and index<Size)
return Scores[index];
else
return -1;
}
double StudentStat::getStatistic(string stat)
{
if(Size == 0) // should be values in list to perform calculations
return -1;
if(stat == "Avg") // retrieve Avg
{
if(Avg == -1) Calc_Avg();
return Avg;
}
if(stat == "StD") // retrieve Standard Deviation
{
if(StDev == -1) Calc_StDev();
return StDev;
}
if(stat == "Med") // retrieve Median
{
if(Median == -1) Calc_Median();
return Median;
}
// High,low and range logic
int high=-99999,low=99999;
if(stat == "High") // retrieve High
{
for(int i=0;i<Size;i++)
if (Scores[i]>high) high=Scores[i];
return high;
}
if(stat == "Low") // retrieve Low
{
for(int i=0;i<Size;i++)
if (Scores[i]<low) low=Scores[i];
return low;
}
if(stat == "Range") // retrieve Range
{
high=getStatistic("High");
low=getStatistic("Low");
return high-low;
}
// TODO: ADD REMAINING LOGIC
return -1;
}
void StudentStat::Sortem()
{
// TODO: ADD VARIATION OF BUBBLE SORT
// Bubble sort algo.
int base, comp;
for(base = 0; base < Size-1; base++)
for(comp = base + 1; comp < Size; comp++)
if(*(Scores+base) > *(Scores+comp))
{
int temp = *(Scores+base);
*(Scores+base) = *(Scores+comp);
*(Scores+comp) = temp;
string namet=Names[base];
Names[base]=Names[comp];
Names[comp]=namet;
}
}
void StudentStat::Calc_Median()
{
// TODO: ADD LOGIC HERE
int middle=Size/2;
if (Size%2==0)
Median=(Scores[middle-1]+Scores[middle])/2;
else
Median=Scores[middle];
}
void StudentStat::Calc_Avg()
{
// TODO: ADD LOGIC HERE
for(int i=0;i<Size;i++){
Avg+=Scores[i];
}
Avg/=Size;
}
void StudentStat::Calc_StDev()
{
// TODO: ADD LOGIC HERE
if (Avg==-1) Calc_Avg();
for(int i=0;i<Size;i++)
StDev+=pow((Scores[i]-Avg),2);
StDev=sqrt(StDev/Size);
}
//This helps with testing, do not modify.
bool stringCheckTest(string testName, string whatItShouldBe, string whatItIs) {
if (whatItShouldBe == whatItIs) {
cout << "Passed " << testName << " Output was: " << whatItIs << endl;
return true;
}
else {
if (whatItShouldBe == "") {
cout << "**Failed test " << testName << " ** " << endl << " Output was "<< whatItIs << endl << " Output should have been blank. " << endl;
} else {
cout << "**Failed test " << testName << " ** " << endl << " Output was "<< whatItIs << endl << " Output should have been " << whatItShouldBe << endl;
}
return false;
}
}
//This helps with testing, do not modify.
bool numericalCheckTest(string testName, double whatItShouldBe, double whatItIs) {
// round to 2 decimal places for comparison test
double whatItIsRounded = static_cast<int>(whatItIs*100 + 0.5)/ 100.0;
if (whatItShouldBe == whatItIsRounded) {
cout << "Passed " << testName << " Output was: " << whatItIs << endl;
return true;
}
else {
cout << "**Failed test " << testName << " ** " << endl << " Output was "<< whatItIs << endl << " Output should have been " << whatItShouldBe << endl;
return false;
}
}
int main()
{
StudentStat StatEven("Even.dat");
// Each test is worth one point
// Test 1 - Person with 2nd highest score
stringCheckTest("Test 1", "Rebecca", StatEven.getName(1));
// Test 2 - Person with 8th highest score
stringCheckTest("Test 2", "Roger", StatEven.getName(7));
// Test 3 - Person with 12th highest score
stringCheckTest("Test 3", "Alan", StatEven.getName(11));
// Test 4 - Person with 15th highest score
stringCheckTest("Test 4", "David", StatEven.getName(14));
// Test 5 - Check Median
numericalCheckTest("Test 5", 72, StatEven.getStatistic("Med"));
// Test 6 - Check Standard Deviation
numericalCheckTest("Test 6", 26.11, StatEven.getStatistic("StD"));
// Test 7 - Check Average
numericalCheckTest("Test 7", 60.67, StatEven.getStatistic("Avg"));
// Test 8 - Check High Score
numericalCheckTest("Test 8", 99, StatEven.getStatistic("High"));
// Test 9 - Check Low Score
numericalCheckTest("Test 9", 7, StatEven.getStatistic("Low"));
// Test 10 - Check Range
numericalCheckTest("Test 10", 92, StatEven.getStatistic("Range"));
StudentStat StatOdd("Odd.dat");
// Each test is worth one point
// Test 11 - Person with 2nd highest score
stringCheckTest("Test 11", "Rebecca", StatOdd.getName(1));
// Test 12 - Person with 8th highest score
stringCheckTest("Test 12", "Scott", StatOdd.getName(7));
// Test 13 - Person with 12th highest score
stringCheckTest("Test 13", "Heber", StatOdd.getName(11));
// Test 14 - Person with 15th highest score
stringCheckTest("Test 14", "Pearl", StatOdd.getName(14));
// Test 15 - Check Median
numericalCheckTest("Test 15", 71, StatOdd.getStatistic("Med"));
// Test 16 - Check Standard Deviation
numericalCheckTest("Test 16", 26.42, StatOdd.getStatistic("StD"));
// Test 17 - Check Average
numericalCheckTest("Test 17", 59.47, StatOdd.getStatistic("Avg"));
// Test 18 - Check High Score
numericalCheckTest("Test 18", 99, StatOdd.getStatistic("High"));
// Test 19 - Check Low Score
numericalCheckTest("Test 19", 7, StatOdd.getStatistic("Low"));
// Test 20 - Check Range
numericalCheckTest("Test 20", 92, StatOdd.getStatistic("Range"));
return 0;
}
Even.dat:
Abc 100
Rebecca 99
Roger 7
Alan 92
David 72
Test2 23
Test21 22
Test22 25
Test23 29
Test24 50
Test25 32
Test26 45
Test27 56
Test28 67
Test29 13
Test20 20
Test21 24
Even 19
Odd.dat:
Abc 100
Rebecca 99
Roger 7
Alan 92
David 72
Test2 23
Test21 22
Test22 25
Test23 29
Test24 50
Test25 32
Test26 45
Test27 56
Test28 67
Test29 13
Test20 20
Test21 24
Even 19
Odd 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.