USE C++ TO WRITE THIS CODE, MUST USE AN ARRAY
An unknown number of grades as integers, but no more than 30, are to be read from the file grades.dat. You may assume each grade will be a valid value between 0 and 100 (no validation necessary) and all "grades" are out of a maximum 100 points. Output the Number of Grades read in from the file. Calculate and output the Total Points Earned. Calculate and output the Total Possible Points. (Remember each grade is out of a possible 100 points max.) Following these, output each individual grade and its percentage of the Total Points Earned. Format these so that they are lined up in two columns. The first column should be 20 characters wide and contain the grade; the second column should be 8 characters wide containing the percentage. A percent sign should be output after the second field. The percentages should be rounded to the nearest tenth. Finally, output the Final Grade as a percentage, such that it lines up with second column previously. See the sample input and output that follows:
Sample Input: assuming grades.dat contained the following
100 95 87 90 76
Sample Output:
Number of Grades: 5 Total Points Earned: 448 Max Possible Points: 500 100 20.0% 95 19.0% 87 17.4% 90 18.0% 76 15.2% Final Grade: 89.6%
Here is code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
const int ISIZE = 30;
int grades[ISIZE];
int num, count = 0, total = 0;
ifstream dataFile;
dataFile.open(".\\files\\grades.dat");
if (!dataFile)
cout << "Error opening data file\n";
else
{ // Input daily grade
count = 0;
while (dataFile >> num && count < ISIZE)
{
grades[count] = num;
total += num;
count++;
}
cout << "Number of Grades: " << count <<
endl;
cout << "Total Points Earned: " << total <<
endl;
cout << "Max Possible Points: " << count * 100 <<
endl << endl;
// <<
for (int i = 0; i < count; i++)
{
cout << setprecision(1);
cout << fixed;
cout << std::setw(20) << grades[i] <<
std::setw(8) << (grades[i] / float(count * 100)) * 100
<< "%" << endl;
}
cout << "\n\nFinal Grade:" << (total / float(count *
100)) * 100 << "%" << endl;
dataFile.close();
cout << endl;
}
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.