C++ The name of the file must be college.cpp CPP State College has a student fil
ID: 3737765 • Letter: C
Question
C++
The name of the file must be college.cpp
CPP State College has a student file with the following data: (students.dat)
- Social Security No.
- Student Name
- Class Code
- School Code
- GPA
- Credits Earned
Class Codes:
1 = Freshman
2 = Sophomore
3 = Junior
4 = Senior
School Codes:
1 = Business
2 = Liberal Arts
3 = Engineering
Write a program that will read student data from file and print summary data as follows:
a. The percentage of students with a GPA of:
1. less than 2.0
2. between 2.0 and 3.0 (inclusive)
3. greater than 3.0
b. The percentage of students with GPAs greater than 3.0 who are:
1. Business majors
2. Liberal Arts majors
3. Engineering majors
c. The percentage of students who have earned more than 100 credits and have GPAs less than 2.00
d. The percentage of students with GPAs greater than 3.0 who are:
1. Freshmen
2. Sophomores
3. Juniors
4. Seniors
Coding requirements/options:
1. Read from the file into an array of structs
2. Use enumerators in your code, example:
enum {FRESHMAN = 1, SOPHOMORE = 2, JUNIOR = 3, SENIOR = 4};
3. Reading from file options:
a. Define a maximum number of lines and define an array of structs accordingly
(inefficient).
b. Count number of lines in file then dynamically allocate memory for the array of
structs. Check how these functions are used to count number of lines in file:
fin.unsetf(ios_base::skipws);
// count the newlines with an algorithm specialized for counting:
unsigned line_count = count(istream_iterator(fin),
istream_iterator(), ' ');
//position streaming pointer back to the beginning of file
fin.clear();
fin.seekg(0, ios::beg);
fin>>skipws; //white space skipping flag -->default
c. Read from the file into a vector (most efficient solution). You need to read from the file to a temporary struct variable first, then push_back this temporary variable into the vector.
Sample Output:
______________________________________________________________________________________
A. The percentage of students with:
1. GPA less than 2.0: 0%
2. GPA between 2.0 and 3.0: 43.75%
3. GPA greater than 3.0: 56.25%
B. The percentage of students with GPAs greater than 3.0 who are:
1. Business majors: 43.75%
2. Liberal Arts majors: 6.25%
3. Engineering majors: 6.25%
C. The percentage of students who have earned more than 100 credits and have GPAs less than 2.00: 0%
D. The percentage of students with GPAs greater than 3.0 who are:
_____________________________________________________________________________
Makefile:
- Should compile both programs, make sure you test your makefile before submission.
- Check how to compile separate programs; one way for doing it is as follows:
________________________________________________________________________
g++ college.cpp -o college -std=c++11
Part2:
g++ library.cpp -o library -std=c++11
clean:
rm -rf *.o
________________________________________________________________________
students.dat:
125997600 HENDERSON 1 1 3.79 011
234789543 MARTINSON 1 1 2.50 011
276888003 JACKSON 2 2 3.94 020
332557267 BRUHANSKY 2 1 3.14 020
235654654 THOMPSON 2 3 2.79 021
336221180 SMITH 2 1 3.04 019
332557845 MIKA 3 3 2.17 024
377000854 ABRAMS 3 1 3.79 027
399000002 NAIRN 3 3 3.75 029
211311411 BUTTRAM 4 1 3.97 035
122886567 DARK 4 1 2.97 034
224257889 SMITH 4 1 3.97 038
125654334 HAYES 4 1 4.00 040
345345669 FISHER 4 2 2.00 036
432234543 JONES 4 3 2.91 033
488345612 PILLION 4 1 2.55 034
Explanation / Answer
There was nothing mentioned about library.cpp hence not including in make file. Be careful with the indentation in makefile while copying.
college.cpp
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <cassert>
#include <cmath>
#include <cstring>
#include <fstream>
#include <sstream>
using namespace std;
enum {FRESHMAN = 1, SOPHOMORE = 2, JUNIOR = 3, SENIOR = 4};
enum {BUSINESS = 1, LIBERALARTS = 2, ENGINEERING = 3};
typedef struct _studentRecord {
long int studentSSN;
string studentName;
int classCode;
int schoolCode;
double GPA;
double creditsEarned;
}StudentRecord;
int main()
{
vector<StudentRecord> records;
ifstream inFile;
inFile.open("students.dat");
string line;
//Accepting line from file
while(inFile.good() && getline(inFile,line)) {
istringstream iss(line);
StudentRecord rec;
//Accepting space separated input from file.
iss >> rec.studentSSN >> rec.studentName >>
rec.classCode >>rec.schoolCode >> rec.GPA >>rec.creditsEarned;
records.push_back(rec);
}
// PART A
//finding out the gpa category here. with index 0: <2.0 1: 2.0>= & <=3.0 2: >3.0
double GPAcategory[3] = {0};
for(int i = 0; i<(int)records.size(); i++) {
if(records[i].GPA < 2.0)
GPAcategory[0]++;
else if(records[i].GPA <= 3.0)
GPAcategory[1]++;
else
GPAcategory[2]++;
}
cout<<"A. The percentage of students with:"<<endl;
//dividing the total number by records.size() that is total number of records
cout<<"1. GPA less than 2.0: "<<(GPAcategory[0]/(double)records.size())*100<<"% ";
cout<<"2. GPA between 2.0 and 3.0: "<<(GPAcategory[1]/(double)records.size())*100<<"% ";
cout<<"3. GPA greater than 3.0: "<<(GPAcategory[2]/(double)records.size())*100<<"% ";
//PARTB
// schoolcode categories with index : 0: BUSINESS 1: LIBERALARTS 2:ENGINEERING
double schoolCodeCat[3] = {0};
for(int i = 0; i<(int)records.size(); i++) {
if(records[i].GPA > 3.0) {
if(records[i].schoolCode == BUSINESS)
schoolCodeCat[0]++;
if(records[i].schoolCode == LIBERALARTS)
schoolCodeCat[1]++;
if(records[i].schoolCode == ENGINEERING)
schoolCodeCat[2]++;
}
}
cout<<"B. The percentage of students with GPAs greater than 3.0 who are: ";
cout<<"1. Business majors: "<<(schoolCodeCat[0]/(double)records.size())*100<<"% ";
cout<<"2. Liberal Arts majors: "<<(schoolCodeCat[1]/(double)records.size())*100<<"% ";
cout<<"3. Engineering majors: "<<(schoolCodeCat[2]/(double)records.size())*100<<"% ";
//PART C
double moreThan100lessThan2 = 0;
for(int i = 0; i<(int)records.size(); i++) {
// checking if credits Earned are more than 100 and GPA less than 2.0
if(records[i].creditsEarned > 100 && records[i].GPA < 2.0)
moreThan100lessThan2++;
}
cout<<"C. The percentage of students who have earned more than 100 credits and have GPAs less than 2.00 "<<
( moreThan100lessThan2 / (double)records.size() )*100 << endl;;
//PART D
// classCode categories with indexing 0:FRESHMAN 1:SOPHOMORE 2:JUNIOR 3:SENIOR
double classCodeCat[4] = {0};
for(int i = 0; i<(int)records.size(); i++) {
if(records[i].GPA > 3.0) {
if(records[i].classCode == FRESHMAN)
classCodeCat[0]++;
else if(records[i].classCode == SOPHOMORE)
classCodeCat[1]++;
else if(records[i].classCode == JUNIOR)
classCodeCat[2]++;
else if(records[i].classCode == SENIOR)
classCodeCat[3]++;
}
}
cout<<"D. The percentage of students with GPAs greater than 3.0 who are: ";
cout<<"1. Freshmen: "<<(classCodeCat[0]/(double)records.size())*100<<"% ";
cout<<"2. Sophomores: "<<(classCodeCat[1]/(double)records.size())*100<<"% ";
cout<<"3. Juniors: "<<(classCodeCat[2]/(double)records.size())*100<<"% ";
cout<<"4. Seniors: "<<(classCodeCat[3]/(double)records.size())*100<<"% ";
return 0;
}
Makefile
all: Part1
Part1:
g++ college.cpp -o college -std=c++11
Part2:
g++ library.cpp -o library -std=c++11
clean:
rm -rf *.o
Sample Output:
A. The percentage of students with:
1. GPA less than 2.0: 0%
2. GPA between 2.0 and 3.0: 43.75%
3. GPA greater than 3.0: 56.25%
B. The percentage of students with GPAs greater than 3.0 who are:
1. Business majors: 43.75%
2. Liberal Arts majors: 6.25%
3. Engineering majors: 6.25%
C. The percentage of students who have earned more than 100 credits and have GPAs less than 2.00 0
D. The percentage of students with GPAs greater than 3.0 who are:
1. Freshmen: 6.25%
2. Sophomores: 18.75%
3. Juniors: 12.5%
4. Seniors: 18.75%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.