Question

In: Computer Science

Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".

 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

Solutions

Expert Solution

#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
===================================================


Related Solutions

Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which...
Write a C++ program in a file called pop.cpp that opens a file called pop.dat which has the following data (you’ll have to create pop.dat) AX013 1.0 BX123456 1234.56 ABNB9876152345 99999.99 The data are account numbers and balances. Account numbers are, at most 14 characters. Balances are, at most, 8 characters (no more than 99999.99). Using a loop until the end of file, read an account number and balance then write these to standard output in the following format shown...
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must...
Write a C++ program that creates a file called Readings.txt. Inside the file, your program must create a list. The list is composed of integer double pairs. There is one pair per line. The integers are in sequence (0, 1, 2, 3, ...) beginning with zero and ending with some random value between 512 and 1024. The doubles should be random values between 50.000 and 90.000. The doubles only have 3 decimal places. The file should look like this (of...
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
A. Write a program called OnesZerosA.java to do all of the following. Please note, for this...
A. Write a program called OnesZerosA.java to do all of the following. Please note, for this problem you are not allowed to use ArrayList. Read a file containing ones and zeros (such as the example below, also available at http://www2.hawaii.edu/~esb/2020fall.ics111/oneszeros.txt) into a two-dimensional int array. Your program must create the array with the same number of rows as the number of lines in the file, and the same number of columns as the number of ones and zeros in each...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT