Write a program that reads student scores from a file. You do not know how many
ID: 3866565 • Letter: W
Question
Write a program that reads student scores from a file.
You do not know how many students in the class (therefore in the file)
The program finds the average for the class.
then it assigns the letter grade as follows:
average + 10% and above is A
average and above is B (less than the A grade above)
average - 10% and above is a C (less than B above)
less than average -10% is F
I am using visual studio, c++, and will like to see a reference a working code while doing my own, just as reference, any help would be awsome
Explanation / Answer
NOTE: I have completed the code for your assignment. Please check and let me know if you face any issues. I will revert back within 24 hours.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double score, sum_scores = 0, avg;
int no_of_scores = 0;
char grade;
// opening file scores.txt containing scores in read mode
ifstream infile ("scores.txt");
// reading each score from file
while(infile >> score){
// counting number of scores and summing them
no_of_scores++;
sum_scores += score;
}
avg = sum_scores/no_of_scores;
cout << " Average of the class is: " << avg <<endl;
cout << " ===================";
cout << " Grading the student";
cout << " ===================";
cout << " Score Grade" << endl;
// resettting the file pointer to beginning again
infile.clear();
infile.seekg(0, ios::beg);
while(infile >> score){
if(score >= (avg + 10/100))
grade = 'A';
else if(score >= avg)
grade = 'B';
else if(score >= (avg - 10/100))
grade = 'C';
else if(score < (avg - 10/100))
grade = 'F';
cout << score << " " << grade << endl;
}
infile.close();
return 0;
}
Output screenshot:
https://pasteboard.co/GD3H7sE.png
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.