I was sick the day my professor talked about files, and now this is due. I had a
ID: 3859599 • Letter: I
Question
I was sick the day my professor talked about files, and now this is due. I had a rough idea on how to get numbers from a file but wanted to post it here just in case. I am going to try it on my own but wanted to post this here just in case I can't figure it out. Any help would be much appreciated.
"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 "B" is (less than the A grade above) average - 10% and above is a "C" (less than B above) less than average -10% is "F".
Sorry if it's worded weird this is how he posted it on the blackboard website. I posted this question yesterday but forgot to mention that I use c++. The question was answered, but I think it was in java. I thought I could use it as an example but its way to different.
Explanation / Answer
Letter Grades:
A for (marks >= (Average + 10) )
B for (marks >= Average and marks < (Average + 10) )
C for (marks >= (Average - 10) and marks < Average)
F for (marks < (Average - 10) )
C++ code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string line;
vector< vector <double> > table;
ifstream myfile ("input.txt");
if (myfile.is_open())
{
// getline (myfile,line);
std::vector<float> marks;
int summ = 0;
int n = 0;
while ( getline (myfile,line) )
{
n = n+1;
int m = atof(line.c_str() ) ;
marks.push_back(m);
summ = summ + m;
}
float Average = summ/n;
for (int i = 0; i < marks.size(); ++i)
{
if( marks[i] >= (Average + 10) )
{
cout << marks[i] << " = A Grade" << endl;
}
else if(marks[i] >= Average and marks[i] < (Average + 10))
{
cout << marks[i] << " = B Grade" << endl;
}
else if(marks[i] >= (Average - 10) and marks[i] < Average)
{
cout << marks[i] << " = C Grade" << endl;
}
else
{
cout << marks[i] << " = F Grade" << endl;
}
}
myfile.close();
}
else
{
cout << "Unable to open file" << endl;
exit(1);
}
}
Sample input.txt
60
70
60
70
65
65
60
70
Sample Output:
60 = C Grade
70 = B Grade
60 = C Grade
70 = B Grade
65 = B Grade
65 = B Grade
60 = C Grade
70 = B Grade
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.