Description \"Write a program that reads from an input file a person\'s name fol
ID: 3877761 • Letter: D
Question
Description
"Write a program that reads from an input file a person's name followed by all their exam scores. Calculate and write the average score for all students on each exam to an output file. Then, write each student's name to the output file followed by the student's score and grade on each exam."
The Lab
Lab requirements:
Use command line argument #1 (argv[1]) to open the input file name.
Use command line argument #2 (argv[2]} to open the output file name.
Read and process student scores from the input file.
The first line of the input file has the number of students and exams.
Subsequent lines contain the student name followed by scores for each exam.
All students have the same number of exam scores.
Store the student names in a dynamic string array.
Store the exam scores in a two-dimensional dynamic double array (one row for each student, one column for each exam score, ie. # students x # of exams.)
Output the average score for each exam (rounded to one decimal place) followed by evenly spaced number of student grades for A's, B's, C's, D's, and E's. (List the letter grade in parentheses after the number of grades.)
If the student's score is within + or - 5 points of the average, give a grade of C.
If the grade is more than 5 points but less than 15 points above (or below) the average, give a grade of B (D).
If the grade is 15 points or more above (or below) the average, give a grade of A (E).
Output each student's exam grade in evenly spaced columns - the student score followed by their grade in parentheses.
Here is the input file.
Here is the output file
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
double **Exams_2d = 0;
double get_average( double **Exams_2d, int no_of_student, int no_of_exams, int Exam_no);
// Subsequent lines contain the student name followed by scores for each exam.
// All students have the same number of exam scores.
// Store the student names in a dynamic string array.
// Store the exam scores in a two-dimensional dynamic double array (one row for each student, one column for each exam score, ie. # students x # of exams.)
int main(int argc, char** argv)
{
if (argc != 3){
cout << "Please enter 3 argument";
exit(1);
}
// Use command line argument #1 (argv[1]) to open the input file name.
string input_file = argv[1];
// Use command line argument #2 (argv[2]} to open the output file name.
string output_file = argv[2];
// Read and process student scores from the input file.
// The first line of the input file has the number of students and exams.
// Open a file
ifstream ifsInputFile(input_file.c_str());
ofstream ofsInputFile(output_file.c_str());
if (ifsInputFile.is_open ()){
string strLine;
// Get every line
getline (ifsInputFile, strLine);
stringstream firstline (strLine);
int no_of_student ;
firstline >> no_of_student;
int no_of_exams;
firstline >> no_of_exams;
cout << "student no is " << no_of_student <<endl;
cout << "exam no is " << no_of_exams <<endl;
// Store the student names in a dynamic string array.
string *student_names_array;
student_names_array = new string[no_of_student];
int student_index =0;
//double **Exams_2d = 0;
//memory allocated for elements of rows.
Exams_2d = new double *[no_of_student] ;
//memory allocated for elements of each column.
for( int i = 0 ; i < no_of_student ; i++ )
Exams_2d[i] = new double[no_of_exams];
//free the allocated memory
// for( int i = 0 ; i < no_of_student ; i++ )
// delete [] Exams_2d[i] ;
// delete [] Exams_2d ;
while (getline (ifsInputFile, strLine))
{
// Use std::stringstream to isolate words using operator >>
stringstream ssWordsBuf (strLine);
string strWord;
string firstname, lastname;
ssWordsBuf >> firstname >> lastname;
student_names_array[student_index] = firstname + " " +lastname;
double marks;
int i =0 ;
while (ssWordsBuf >> marks){
//cout << strWord << endl; // <--- Output!
Exams_2d[student_index][i++] = marks;
// Store the student names in a dynamic string array.
// Store the exam scores in a two-dimensional dynamic double array (one row for each student, one column for each exam score, ie. # students x # of exams.)
}
student_index++;
}
ifsInputFile.close ();
cout << "student array" <<endl;
for( int i =0 ; i < no_of_student ; i++){
cout << student_names_array[i] << endl;
}
cout << "Exam array" <<endl;
for( int i =0 ; i < no_of_student ; i++){
for( int j =0 ; j < no_of_exams ; j++){
cout << Exams_2d[i][j] << " ";
}
cout << endl;
}
if (ofsInputFile.is_open ()){
ofsInputFile << "Exam Averages:" <<endl;
for(int i =0 ; i < no_of_exams; i++){
int Exam_no = i;
double average = get_average( Exams_2d, no_of_student, no_of_exams, Exam_no);
ofsInputFile << setprecision(1) << average << endl;
cout << fixed << setprecision(1) << average << endl;
}
}
}
return 0;
}
double get_average( double **Exams_2d, int no_of_student, int no_of_exams, int Exam_no){
double average = 0;
double sum =0;
for (int i =0 ; i < no_of_student; i++){
sum += Exams_2d[i][Exam_no];
}
average = sum / no_of_student;
return average;
}
// The students Exam Grades are not added. Will add the code soon