C++ This 50 record file consists of a (8-digit numeric) student id, 8 assignment
ID: 3663346 • Letter: C
Question
C++
This 50 record file consists of a (8-digit numeric) student id, 8 assignment's points, midterm points, lab exercise points, CodeLab points, and final points. All of the input data, calculated totals, percents, and grades (and plus or minus) must be stored in only one 2 dimensional array. The specifications for determination of the grades is the same as assignment 1 (or the course syllabus). Your output reports should be "very similar" to the output shown below. The point totals, percents, and grades must be follow the exact calculations. This is the same as assignment 1. You are to produce two output reports, one sorted by student id and one sorted by total points (in decending order). The two output reports must be written to separate files.
The Student Data Input File
Output Report Sorted by Student Id
Stdnt Id Ex ----- Assignments ----- Tot Mi Fin CL Pts Pct Gr
-------- -- ----------------------- --- -- --- -- --- --- --
01234567 57 16 14 20 8 10 19 11 9 99 53 21 16 246 62 D
12745220 55 20 17 16 18 19 15 20 4 114 22 64 19 274 69 D+
22645995 60 13 15 17 18 17 10 12 14 106 74 122 17 379 95 A
33339883 55 20 17 12 20 20 20 11 13 122 40 113 16 346 87 B
34029990 56 8 18 3 10 16 17 12 7 88 56 113 18 331 83 B
44576634 49 20 6 20 20 20 19 18 20 137 52 53 19 310 78 C+
46479276 39 20 7 13 16 7 15 18 10 99 26 52 18 234 59 F
47032460 60 18 18 14 11 17 18 19 6 110 61 101 20 352 88 B+
...
Output Report Sorted by Total Points (decending)
Stdnt Id Ex ----- Assignments ----- Tot Mi Fin CL Pts Pct Gr
-------- -- ----------------------- --- -- --- -- --- --- --
22645995 60 13 15 17 18 17 10 12 14 106 74 122 17 379 95 A
73528933 48 19 18 18 17 16 17 16 18 123 70 119 19 379 95 A
94958157 60 20 20 20 20 16 18 17 15 130 75 94 17 376 94 A
87305446 57 20 19 19 17 14 16 17 20 128 66 98 19 368 92 A
88258087 33 18 18 15 19 10 20 19 20 129 64 113 20 359 90 A-
90076387 57 20 16 9 9 19 17 20 17 118 67 97 18 357 89 B+
89168351 52 15 16 11 14 10 17 20 15 108 59 120 14 353 88 B+
92067579 45 19 16 15 17 19 19 8 9 114 51 122 20 352 88 B+
...
Program requirements
You must use a 2-dimensional array for all of the data.
Your solution must include at least 5 functions. And ...
At least one function must include a two dimensional array argument.
At least one function must include an argument that consists of one row of a two dimensional array.
At least one function must include an argument that consists of one element of a two dimensional array.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;
int getTotalForAssignments (int ass[]);
void getHighestPercent (int& highest, int percent);
string getLetterGradeForPercent (int percent);
string getBaseGradeForPercent (int percent);
int main()
{
ifstream file("grades.txt");
if(!file)
{
cout << "could not open file ";
exit(0);
}
ofstream reportFile("report.txt");
if(!reportFile.is_open()) {
cout << "could not open report output file";
exit(0);
}
reportFile << "Stdnt Id " << "Ex " << "------Assignments------ " << "Tot " << "Mi " << "Fin " << "CL " << "Pts " << "Pct " << "Gr " << endl;
reportFile << "-------- " << "-- " << "----------------------- " << "--- " << "-- " << "--- " << "-- " << "--- " << "--- " << "-- " << endl;
int id, exercisePoints, midterm, final, codeLab;
int ass[8];
int studentPercents[50];
int count = 0;
while(!file.eof())
{
file >> id >> exercisePoints >> ass[0] >> ass[1] >> ass[2] >> ass[3] >> ass[4] >> ass[5] >> ass[6] >> ass[7] >> midterm >> final >> codeLab;
if(file.eof()) break;
reportFile << setfill('0') << setw(8) << id << " "; //add leading 0's to ID
reportFile << exercisePoints << " ";
for(int i=0; i<8; i++)
{
reportFile << setfill(' ')<< setw(2) << ass[i] << " ";
}
reportFile << " " << right << setw(3) << getTotalForAssignments(ass) << " ";
reportFile << midterm << " " << right << setw(3) << final << " " << right << setw(2) << codeLab << " ";
float totalPoints = exercisePoints + getTotalForAssignments(ass) + midterm + final + codeLab;
reportFile << totalPoints << " ";
int percent = round(totalPoints * 100 /400);
reportFile << right << setw(3) << percent << " " << getLetterGradeForPercent(percent) << endl;
studentPercents[count] = percent;
count++;
}
reportFile.close();
ofstream summaryFile("summary.txt");
if(!summaryFile.is_open())
{
cout << "could not open summary output file";
exit(0);
}
int a, b, c, d, f;
float percentSum = 0;
int highest = 0;
for(int i=0; i<50; i++)
{
percentSum+= studentPercents[i];
getHighestPercent(highest, studentPercents[i]);
if(getBaseGradeForPercent(studentPercents[i]) == "A")
{
a++;
}
else if(getBaseGradeForPercent(studentPercents[i]) == "B")
{
b++;
}
else if(getBaseGradeForPercent(studentPercents[i]) == "C")
{
c++;
}
else if(getBaseGradeForPercent(studentPercents[i]) == "D")
{
d++;
}
else if (getBaseGradeForPercent(studentPercents[i]) == "F")
{
f++;
}
}
float average = percentSum/50;
summaryFile << "Number of A's = " << a << " Number of B's = " << b << " Number of C's = " << c << " Number of D's = " << d << " Number of F's = " << f << endl;
summaryFile << "Average percent = " << fixed << setprecision(1) << average << endl;
summaryFile << "Maximum points = " << highest << endl;
summaryFile.close();
return 0;
}
void getHighestPercent (int& highest, int percent)
{
if(percent > highest)
{
highest = percent;
}
}
int getTotalForAssignments (int ass[])
{
int sum = 0;
int lowest = ass[0];
for(int i=0; i<7; i++)
{
sum += ass[i];
if(ass[i] < lowest)
{
lowest = ass[i];
}
}
sum-= lowest;
sum+= ass[7];
return sum;
}
string getLetterGradeForPercent(int percent)
{
string grade;
grade = getBaseGradeForPercent(percent);
if(percent < 100)
{
if(percent%10 >= 0 && percent%10 < 2)
{
grade+= "-";
}
else if(percent%10 >= 8 && percent%10 <=9)
{
grade+= "+";
}
}
else
{
grade += "+";
}
return grade;
}
string getBaseGradeForPercent (int percent)
{
string grade;
if(percent < 100)
{
switch(percent/10)
{
case 9: grade = "A";
break;
case 8: grade = "B";
break;
case 7: grade = "C";
break;
case 6: grade = "D";
break;
default:grade = "F";
break;
}
}
else
{
grade = "A";
}
return grade;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.