In: Computer Science
c++
Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".
(1) Read the grade sheet from attached data file,
"Assignment4.dat". Print the original grade sheet.
(3) Write a module to sort array GRADE[] and print the sorted grade
sheet.
(4) Write a module to sort array NAME[] and print the sorted grade
sheet.
(5) Write a module to sort array ID[] and print the sorted grade
sheet.
(6) Write a module to print the student's id, name, and grade for
student with highest grade and with lowest grade,
respectively.
(7) Write a module to determine mean of the grades.
(8) Write a module to list students with grade is less than
mean.
*** Coding modules for Assignment 4
*** Using pointers for allocating all the arrays
Assignment 4
ID NAME GRADE
5532 Mary 100
7856 Tom 99.56
3345 Cathy 33.78
1229 Jim 89.42
2785 David 67.52
9954 Rob 99.25
2388 Sue 87.29
6523 Michael 60.55
1287 Zach 76.12
2783 Amy 90.55
#include
#include
#include
using namespace std;
void swap(int *ids, string *names, double *grades, int i, int
j){
int temp = ids[i];
ids[i] = ids[j];
ids[j] = temp;
string ntemp = names[i];
names[i] = names[j];
names[j] = ntemp;
double gtemp = grades[i];
grades[i] = grades[j];
grades[j] = gtemp;
}
void sortByGrade(int *ids, string *names, double *grades, int
size){
for (int i = 0; i < size; ++i){
for (int j = 0; j < size - 1;
++j){
if (grades[j]
> grades[j + 1]){
swap(ids, names, grades, j, j + 1);
}
}
}
}
void sortByName(int *ids, string *names, double *grades, int
size){
for (int i = 0; i < size; ++i){
for (int j = 0; j < size - 1;
++j){
if (names[j]
> names[j + 1]){
swap(ids, names, grades, j, j + 1);
}
}
}
}
void sortById(int *ids, string *names, double *grades, int
size){
for (int i = 0; i < size; ++i){
for (int j = 0; j < size - 1;
++j){
if (ids[j] >
ids[j + 1]){
swap(ids, names, grades, j, j + 1);
}
}
}
}
void printLowestHighest(int *ids, string *names, double *grades,
int size){
int minInd = 0;
int maxInd = 0;
for (int i = 1; i < size; ++i){
if (grades[i] <
grades[minInd]){
minInd =
i;
}
if (grades[i] >
grades[maxInd]){
maxInd =
i;
}
}
string fileName = "GradeSheet.dat";
ofstream out;
out.open(fileName, ofstream::out |
ofstream::app);
if (out.is_open()){
out << "Student with minuimum
grades" << endl;
out <<
"===================================================" <<
endl;
out << ids[minInd] <<
"\t" << names[minInd] << "\t" << grades[minInd]
<< endl;
out <<
"===================================================" << endl
<< endl;
out << "Student with
maximum grades" << endl;
out <<
"===================================================" <<
endl;
out << ids[maxInd] <<
"\t" << names[maxInd] << "\t" << grades[maxInd]
<< endl;
out <<
"===================================================" << endl
<< endl;
}
else{
cout << "Can not open "
<< fileName << endl;
}
}
double getMean(double *grades, int size){
double total = 0;
for (int i = 0; i < size; ++i){
total += grades[i];
}
return total / size;
}
void printStudentsWithGradeLessThanMean(int *ids, string *names,
double *grades, int size){
double mean = getMean(grades, size);
string fileName = "GradeSheet.dat";
ofstream out;
out.open(fileName, ofstream::out |
ofstream::app);
if (out.is_open()){
out << "Students with grades
less than mean" << endl;
out <<
"===================================================" <<
endl;
for (int i = 0; i < size;
++i){
if (grades[i]
< mean)
out << ids[i] << "\t" <<
names[i] << "\t" << grades[i] << endl;
}
out <<
"===================================================" << endl
<< endl;
}
else{
cout << "Can not open "
<< fileName << endl;
}
}
void printDetailsIntoFile(int *ids, string *names, double
*grades, string message, int size){
string fileName = "GradeSheet.dat";
ofstream out;
out.open(fileName, ofstream::out |
ofstream::app);
if (out.is_open()){
out << message <<
endl;
out <<
"===================================================" <<
endl;
for (int i = 0; i < size;
++i){
out <<
ids[i] << "\t" << names[i] << "\t" <<
grades[i] << endl;
}
out <<
"===================================================" << endl
<< endl;
}
else{
cout << "Can not open "
<< fileName << endl;
}
}
int main(){
ifstream in;
string fileName = "Assignment4.dat";
in.open(fileName.c_str());
int size = 0;
string line;
int *ids;
string *names;
double *grades;
if (in.is_open()){
while (getline(in, line)){
if (line !=
"")
size++;
}
size -= 2;
in.close();
in.open(fileName.c_str());
ids = new int[size];
names = new string[size];
grades = new double[size];
getline(in, line);
getline(in, line);
for (int i = 0; i < size;
++i){
in >>
ids[i];
in >>
names[i];
in >>
grades[i];
//cout <<
ids[i] << "\t" << names[i] << "\t" <<
grades[i] << endl;
}
sortByGrade(ids, names, grades,
size);
printDetailsIntoFile(ids, names,
grades, "Sorted By Grade", size);
sortByName(ids, names, grades,
size);
printDetailsIntoFile(ids, names,
grades, "Sorted By Name", size);
sortById(ids, names, grades,
size);
printDetailsIntoFile(ids, names,
grades, "Sorted By Id", size);
printLowestHighest(ids, names,
grades, size);
printStudentsWithGradeLessThanMean(ids, names, grades, size);
}
else{
}
return 0;
}
____________________________________________________________________________________________
Assignment 4
ID NAME GRADE
5532 Mary 100
7856 Tom 99.56
3345 Cathy 33.78
1229 Jim 89.42
2785 David 67.52
9954 Rob 99.25
2388 Sue 87.29
6523 Michael 60.55
1287 Zach 76.12
2783 Amy 90.55
______________________________________________________________________________________________
Sorted By Grade
===================================================
3345 Cathy 33.78
6523 Michael 60.55
2785 David 67.52
1287 Zach 76.12
2388 Sue 87.29
1229 Jim 89.42
2783 Amy 90.55
9954 Rob 99.25
7856 Tom 99.56
5532 Mary 100
===================================================
Sorted By Name
===================================================
2783 Amy 90.55
3345 Cathy 33.78
2785 David 67.52
1229 Jim 89.42
5532 Mary 100
6523 Michael 60.55
9954 Rob 99.25
2388 Sue 87.29
7856 Tom 99.56
1287 Zach 76.12
===================================================
Sorted By Id
===================================================
1229 Jim 89.42
1287 Zach 76.12
2388 Sue 87.29
2783 Amy 90.55
2785 David 67.52
3345 Cathy 33.78
5532 Mary 100
6523 Michael 60.55
7856 Tom 99.56
9954 Rob 99.25
===================================================
Student with minuimum grades
===================================================
3345 Cathy 33.78
===================================================
Student with maximum grades
===================================================
5532 Mary 100
===================================================
Students with grades less than mean
===================================================
1287 Zach 76.12
2785 David 67.52
3345 Cathy 33.78
6523 Michael 60.55
===================================================