Write a C++ program for Student Grading Using File I/O and Arrays The purpose of
ID: 3876353 • Letter: W
Question
Write a C++ program for Student Grading Using File I/O and Arrays
The purpose of this assignment is to review the use of one-dimenisonal arrays, reading and writing text files, writing functions, and program planning and development.
For this assignment, you will be reading one input file and writing out to an output file.
Read in the student data file, partially shown below. Each line of this file consists of a (8-digit numeric) student id, lab exercise points (50), 10 assignment's points (20 each), midterm points (60), final points (100), and CodeLab points (10). For coding, read the entire file assuming you do not know how many lines are in the file (no hard-coding of loop count). You will write the input student data and the results of the processing into a student report file HW1-output-XXXXX.txt that looks "just like" the output shown below. In addition to the input student data, the report should contain the "total" of the assignment grades, the total and percent of all points achieved, and the letter grade. You may assume that the input data file does not contain any errant data.
The Student Data Input File
...
Points
Lab Exercises
50 points
10 HW
200 points
Midterm
70 points
Final
100 points
Extra Credit: CodeLab
10 points
Total used for percentage calculation : 420 (not including extra credit)
Grading Scale:
A 90% and up of total score
B 78% through 89% of total score
C 62% through 77% of total score
D 46% through 61% of total score
F 45% or less of total score
The Student Report Output File
At the end of the previous report, add the code to write a summary report file that contains the average percent of total points for all students. Also, display the number of A's, B's, C's, D's and F's for the students. Your summary output file should look something like this:
Average total point percent of all students: ??
Number of A's = ??
Number of B's = ??
Number of C's = ??
Number of D's = ??
Number of F's = ??
Maximum points = ??? <- This is the highest number of points achieved by any student
Program requirements
You must use at least 1 one-dimension array. Do not use 2 dimensional arrays - this will be covered in the another assignment.
Your solution must include at least 3 functions, counting the main() function. One function must include an array argument with a size argument. Place the function definitions below main() and function prototypes above main().
All files must be checked for a successful open.
Make sure you write the student id with a leading 0, if appropriate (i.e. the 7th id).
The percent of total points must be rounded to the nearest integer. For example, 94.1% becomes 94%, 85.8% becomes 86%, 75.5% is 76% (see cmath library)
The letter grade is determined by the "integer" percent.
Add headings to your output report file. They should be aligned and correctly identify the column data.
Do not use any global variable.
Submit in Canvas your source code. Your code will be checked for the output file.
Lab Exercises
50 points
10 HW
200 points
Midterm
70 points
Final
100 points
Extra Credit: CodeLab
10 points
Assignments Tot Mi Fin CL Pts Pct Gr 99587442 40 11 12 18 10 15 11 19 14 15 19 144 57 93 4 338 80 B 69376571 48 14 19 19 20 19 19 16 18 20 8 172 40685 333 80 C 83406759 46 16 18 4 14 16 19 20 12 14 7 140 57 71 10 324 75 C 68507434 41 20 17 20 18 11 18 12 19 6 17 158 47808 334 80 B 68315319 50 6 14 19 19 19 18 9 1617 20 157 53 89 350 85 B 85868414 48 20 19 14 14 13 15 19 20 20 13 167 59 9 6 374 90 A 01234567 48 16 20 14 18 14 18 16 15 15 16 162 57 85 8 360 85 B 9434976048 2017 1212181717 208 514 33 68 8303 70CExplanation / Answer
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <sstream>
#include <fstream>
using namespace std;
class Student
{
public:
unsigned long int studentID;
int labExcePoint;
int assignPoints1;
int assignPoints2;
int assignPoints3;
int assignPoints4;
int assignPoints5;
int assignPoints6;
int assignPoints7;
int assignPoints8;
int assignPoints9;
int assignPoints10;
int total;
int midtermPoints;
int finalPoints;
int codeLabPoints;
int pts;
float pct;
char gr;
public:
Student();
~Student(){}
void printStudentInfo();
};
//implementations
Student::Student()
{
studentID = -9999;
labExcePoint = 0;
assignPoints1 = 0;
assignPoints2 = 0;
assignPoints3 = 0;
assignPoints4 = 0;
assignPoints5 = 0;
assignPoints6 = 0;
assignPoints7 = 0;
assignPoints8 = 0;
assignPoints9 = 0;
assignPoints10 = 0;
total = 0;
midtermPoints = 0;
finalPoints = 0;
codeLabPoints = 0;
pts = 0;
pct = 0.0;
}
//printing
void Student:: printStudentInfo()
{
/* cout << studentID << " " << labExcePoint << " " ;
cout << " " assignmentPoints1 << " " assignmentPoints2 << " " assignmentPoints3 ;
cout << " " assignmentPoints4 << " "< assignmentPoint5 << " " assignmentPoints6 ;
cout << " " assignmentPoints7 << " " assignmentPoint8 << " " assignmentPoints9 ;
cout << " " assignmentPoints10 << " " total << " " midtermPoints << " " finalPoints << " " codeLabPoints;
cout << " " pts << " " pct << " " gr << endl;
*/
}
int main()
{
std::ifstream file("NewFile.txt");
std::string line;
std::vector<Student> tokens;
while(std::getline(file, line)) { // ' ' is the default delimiter
std::istringstream iss(line);
std::string token;
Student *f1 = new Student;
string studentID1;
string labExcePoint1;
string assignmentPoints1;
string assignmentPoints2;
string assignmentPoints3;
string assignmentPoints4;
string assignmentPoints5;
string assignmentPoints6;
string assignmentPoints7;
string assignmentPoints8;
string assignmentPoints9;
string assignmentPoints10;
string total1;
string midtermPoints1;
string finalPoints1;
string codeLabPoints1;
string pts1;
string pct1;
string gr1;
// cout << line << endl;
std::string::size_type sz;
getline (iss, studentID1, ' '); //read from in, store iso and table
getline (iss, labExcePoint1, ' '); //getline will only use string
getline (iss, assignmentPoints1, ' ');
getline (iss, assignmentPoints2, ' ');
getline (iss, assignmentPoints3, ' ');
getline (iss, assignmentPoints4, ' ');
getline (iss, assignmentPoints5, ' ');
getline (iss, assignmentPoints6, ' ');
getline (iss, assignmentPoints7, ' ');
getline (iss, assignmentPoints8, ' ');
getline (iss, assignmentPoints9, ' ');
getline (iss, assignmentPoints10, ' ');
// getline (iss, total1, ' ');
getline (iss, midtermPoints1, ' ');
getline (iss, finalPoints1, ' ');
getline (iss, codeLabPoints1, ' ');
// getline (iss, pts1, ' ');
// getline (iss, pct1, ' ');
// getline (iss, gr1, ' ');
// cout << studentID1 << labExcePoint1 << endl;
// cout << assignmentPoints1 << assignmentPoints2 << assignmentPoints3 << assignmentPoints4 << assignmentPoints5 << endl;
// cout << assignmentPoints6 << assignmentPoints7 << assignmentPoints8 << assignmentPoints9 << assignmentPoints10 << endl;
// cout << midtermPoints1 << finalPoints1 << codeLabPoints1 << pts1 << pct1 << gr1 << endl ;
f1->studentID = std::atol(studentID1.c_str());
f1->labExcePoint = std::atol(labExcePoint1.c_str());
f1->assignPoints1 = std::atol(assignmentPoints1.c_str());
f1->assignPoints2 = std::atol(assignmentPoints2.c_str());
f1->assignPoints3 = std::atol(assignmentPoints3.c_str());
f1->assignPoints4 = std::atol(assignmentPoints4.c_str());
f1->assignPoints5 = std::atol(assignmentPoints5.c_str());
f1->assignPoints6 = std::atol(assignmentPoints6.c_str());
f1->assignPoints7 = std::atol(assignmentPoints7.c_str());
f1->assignPoints8 = std::atol(assignmentPoints8.c_str());
f1->assignPoints9 = std::atol(assignmentPoints9.c_str());
f1->assignPoints10 = std::atol(assignmentPoints10.c_str());
f1->total = ( f1->assignPoints1 + f1->assignPoints2 + f1->assignPoints3 + f1->assignPoints4 + f1->assignPoints5 + f1->assignPoints6 + f1->assignPoints7 + f1->assignPoints8 + f1->assignPoints9 + f1->assignPoints10 );
f1->midtermPoints = std::atol(midtermPoints1.c_str());
f1->finalPoints = std::atol(finalPoints1.c_str());
f1->codeLabPoints = std::atol(codeLabPoints1.c_str());
f1->pts = f1->labExcePoint + f1->total + f1->midtermPoints + f1->finalPoints + f1->codeLabPoints;
f1->pct = (f1->pts * 100 ) / 420 ;
if(f1->pct >= 90){
f1->gr = 'A';
}else if(f1->pct >= 78 && f1->pct <= 89){
f1->gr = 'B';
}else if(f1->pct >= 62 && f1->pct <= 77){
f1->gr = 'C';
}else if(f1->pct >= 46 && f1->pct <= 61){
f1->gr = 'D';
}else{
f1->gr = 'F';
}
tokens.push_back(*f1);
}
ofstream outputFile;
outputFile.open("HW1-output-XXXXX.txt");
vector<Student>::iterator it1;
cout << "Studt ID" << " " << "Ex" << " " << "-------- Assignment ---------" << " " << "Tot" << " " << "Mi" << " " << "Fin" << " " << "CL" << " " << "Pts" << " " << "pct" << " " << "GR" << endl;
cout << "--------" << " " << "--" << " " << "-------- Assignment ---------" << " " << "---" << " " << "--" << " " << "---" << " " << "--" << "--" << "---" << "--" << "---" << "--" << "--" << endl;
for ( it1 = tokens.begin(); it1 != tokens.end(); ++it1 ) {
cout << it1->studentID << " " << it1->labExcePoint << " " ;
cout << " " << it1->assignPoints1 << " " << it1->assignPoints2 << " " << it1->assignPoints3 ;
cout << " " << it1->assignPoints4 << " " << it1->assignPoints5 << " " << it1->assignPoints6 ;
cout << " " << it1->assignPoints7 << " " << it1->assignPoints8 << " " << it1->assignPoints9 ;
cout << " " << it1->assignPoints10 << " " << it1->total << " " << it1->midtermPoints << " " << it1->finalPoints << " " << it1->codeLabPoints;
cout << " "<< it1->pts << " " << it1->pct << " " << it1->gr << endl;
outputFile << "Studt ID" << " " << "Ex" << " " << "-------- Assignment ---------" << " " << "Tot" << " " << "Mi" << " " << "Fin" << " " << "CL" << " " << "Pts" << " " << "pct" << " " << "GR" << endl;
outputFile << "--------" << " " << "--" << " " << "-------- Assignment ---------" << " " << "---" << " " << "--" << " " << "---" << " " << "--" << "--" << "---" << "--" << "---" << "--" << "--" << endl;
outputFile << it1->studentID << " " << it1->labExcePoint << " " ;
outputFile << " " << it1->assignPoints1 << " " << it1->assignPoints2 << " " << it1->assignPoints3 ;
outputFile << " " << it1->assignPoints4 << " " << it1->assignPoints5 << " " << it1->assignPoints6 ;
outputFile << " " << it1->assignPoints7 << " " << it1->assignPoints8 << " " << it1->assignPoints9 ;
outputFile << " " << it1->assignPoints10 << " " << it1->total << " " << it1->midtermPoints << " " << it1->finalPoints << " " << it1->codeLabPoints;
outputFile << " "<< it1->pts << " " << it1->pct << " " << it1->gr << endl;
}
outputFile.close();
return 0;
}
---- End----------------------
Output
-------------
Studt ID Ex -------- Assignment --------- Tot Mi Fin CL Pts pct GR
-------- -- -------- Assignment --------- --- -- --- ----------------
50 40 10 11 12 13 14 15 16 17 18 19 145 21 22 23 251 59 D
Description
-------------------
1. Create NewFile.txt file in the same directory where this program to be executed or placed.
2. make the below entries. You can some more entries but i havetaken only row for example
50 40 10 11 12 13 14 15 16 17 18 19 21 22 23
3. i have taken studnet class which represent whole data that file contains
4. program performs opertion like calculating total , percentage and grande and store them into stundent class object.
5. finally program iterate vector element ( studnet object ) one by one and write the content to file.
Please let me know if you need more assistance on same.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.