In: Computer Science
USE C++ PLEASE AND EXPLAIN LINES AS MUCH AS POSSIBLE...THANKS IN ADVANCE...NEED THIS ASAP
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%
CODE -
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int grades[30]; // Creating an array to store grades.
int count = 0; // Initializing number of grades to 0
ifstream infile; // File reading stream
int total_points = 0; // Creating the variable to store total points earned and initializing its value to 0
infile.open("grades.dat"); // Opening file for reading
while(infile.peek() != EOF) // Reading file till we reach the end of file.
{
infile >> grades[count]; // Reading the grade one at a time
total_points += grades[count]; // Adding the current grade to total points earned.
count++; // Increasing the count of no. of grades.
}
int max_points = count*100; // Calculating the maximum possible points
infile.close(); // Closing the file
cout << std::left << setw(23) << "Number of Grades: " << std::right << setw(5) << count << endl; // Displaying the no. of grades
cout << std::left << setw(23) << "Total Points Earned: " << std::right << setw(5) << total_points << endl; // Displaying the total points earned.
cout << std::left << setw(23) << "Max Possible Points: " << std::right << setw(5) << max_points << endl << endl; // Displaying the maximum possible points.
cout << fixed << setprecision(1); // Setting precision to 1 so that only 1 digit after decimal is printed.
for(int i=0; i<count; i++)
cout << setw(20) << grades[i] << setw(8) << ((float)grades[i]/(float)max_points) * 100 << "%" << endl; // Displaying individual grade and its percentage
cout << endl;
cout << std::left << setw(20) << "Final Grade: " << std::right << setw(8) << ((float)total_points/(float)max_points) * 100 << "%" << endl; // Displaying FInal grade
}
SCREENSHOTS -
INPUT TEXT FILE -
CODE WITH OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.