Assignment: The Oregon Department of Education has commissioned a study to deter
ID: 3834102 • Letter: A
Question
Assignment:
The Oregon Department of Education has commissioned a study to determine if Males or Females score
better on a particular test. They also want to study if Community College or University students score
better. Researchers have created a data file (scores.txt), which contains the scores attained by the
student sample. The first column is the student’s name, the second column is the student’s gender (M
for male, and F for female), the third column indicates Community College (CC) or 4 year (UN), and the
last column specifies the score. A specimen of the file scores.txt is attached as part of this assignment.
You should download this file to a convenient place on your hard drive.
Design Considerations:
1. Your assignment is to prompt the user to enter the filename with the path on disk.
If the file does not exist in the specified location, your program should exit with a
suitable error message.
2. No global variables. All variables must be inside main().
3. The first thing your program should do is output to the screen a copy of the data
read in from the disk file. This is known as “echoing” the input data.
4. Your program should then calculate and display the average score for males,
females, community college students, and university students. Display your
results to two places of decimals, and write your program to automatically list
your four calculated averages one to a line, along with a suitable label for each
result.
5. Finally, your program should calculate and display to two places of decimals the
overall average score for all of the students surveyed. (Note: Mathematically, this
is NOT the average of the four averages you calculated previously).
Explanation / Answer
I have made couple of assumptions regarding missing information in the given question.
File: Scores.cpp
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
using namespace std;
// Store name, gender, institute type and score details for a Student
class Student
{
private:
string name;
string gender;
string instituteType;
double score;
public:
Student(string aName, string aGender, string anInstituteType, double aScore)
: name(aName), gender(aGender), instituteType(anInstituteType), score(aScore) { }
string getName() { return name; }
string getGender() { return gender; }
string getInstituteType() { return instituteType; }
double getScore() { return score; }
};
int main()
{
// Prompt user to enter the input file path
string filePath;
cout << "Enter data file path: ";
cin >> filePath;
ifstream inFile(filePath.c_str());
if (inFile.good() == false)
{
cout << "Invalid data file path." << endl;
return -1;
}
// Read information from input file and prepare a vector array of student details
string line;
string name;
string gender;
string instituteType;
double score;
vector<Student *> students;
while (getline(inFile, line))
{
istringstream inSS(line);
inSS >> name >> gender >> instituteType >> score;
students.push_back(new Student(name, gender, instituteType, score));
}
// Compute various averages required
int nMaleStudents = 0;
int nFemaleStudents = 0;
int nCCStudents = 0;
int nUNStudents = 0;
double avgMales = 0;
double avgFemales = 0;
double avgCC = 0;
double avgUN = 0;
double avgAll = 0;
for (int i = 0; i < students.size(); i++)
{
cout << students[i]->getName() << " " << students[i]->getGender() << " "
<< students[i]->getInstituteType() << " " << students[i]->getScore() << endl;
if (students[i]->getGender() == "M")
{
avgMales += students[i]->getScore();
nMaleStudents++;
}
else if (students[i]->getGender() == "F")
{
avgFemales += students[i]->getScore();
nFemaleStudents++;
}
if (students[i]->getInstituteType() == "CC")
{
avgCC += students[i]->getScore();
nCCStudents++;
}
else if (students[i]->getInstituteType() == "UN")
{
avgUN += students[i]->getScore();
nUNStudents++;
}
avgAll += students[i]->getScore();
}
if (nMaleStudents > 0)
{
avgMales /= nMaleStudents;
}
if (nFemaleStudents > 0)
{
avgFemales /= nFemaleStudents;
}
if (nCCStudents > 0)
{
avgCC /= nCCStudents;
}
if (nUNStudents > 0)
{
avgUN /= nUNStudents;
}
if (students.size() > 0)
{
avgAll /= students.size();
}
// Diplay averages with two decimals precision
cout << fixed << setprecision(2);
cout << "Average score for male students : " << avgMales << endl;
cout << "Average score for female students : " << avgFemales << endl;
cout << "Average score for community college students: " << avgCC << endl;
cout << "Average score for university students : " << avgUN << endl;
cout << "Average score for all students : " << avgAll << endl;
return 0;
}
Compilation:
$ g++ Scores.cpp
Sample Execution Output #1:
$ ./a.out
Enter data file path: wrong.txt
Invalid data file path.
Sample Input File: scores.txt
John,A M CC 10.1
John,B M UN 12.2
Emma,C F UN 14.3
Emma,D F CC 16.5
John,E M CC 18.6
John,F M UN 20.7
Emma,G F UN 11.8
Emma,H F CC 13.9
John,I M CC 15.1
John,J M UN 17.2
Emma,K F UN 19.4
Emma,L F CC 10.5
John,M M CC 12.6
John,N M UN 11.7
Emma,O F UN 10.8
Emma,P F CC 20.9
John,Q M CC 18.1
John,R M UN 16.2
Emma,S F UN 14.3
Emma,T F CC 12.4
John,U M CC 10.5
John,V M UN 19.6
Emma,W F UN 17.7
Emma,X F CC 15.8
John,Y M CC 13.9
John,Z M UN 11.1
Sample Execution Output #2:
$ ./a.out
Enter data file path: scores.txt
John,A M CC 10.1
John,B M UN 12.2
Emma,C F UN 14.3
Emma,D F CC 16.5
John,E M CC 18.6
John,F M UN 20.7
Emma,G F UN 11.8
Emma,H F CC 13.9
John,I M CC 15.1
John,J M UN 17.2
Emma,K F UN 19.4
Emma,L F CC 10.5
John,M M CC 12.6
John,N M UN 11.7
Emma,O F UN 10.8
Emma,P F CC 20.9
John,Q M CC 18.1
John,R M UN 16.2
Emma,S F UN 14.3
Emma,T F CC 12.4
John,U M CC 10.5
John,V M UN 19.6
Emma,W F UN 17.7
Emma,X F CC 15.8
John,Y M CC 13.9
John,Z M UN 11.1
Average score for male students : 14.83
Average score for female students : 14.86
Average score for community college students: 14.53
Average score for university students : 15.15
Average score for all students : 14.84
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.