Write a C++ program that reads student data from a file, computers average test
ID: 3720361 • Letter: W
Question
Write a C++ program that reads student data from a file, computers average test and grades for the class and outputs the data in a file. The data that will be stored in the output file are the student names, average test scores and grades only for the students that have a grade of C or better.
Output should be: Name of student 77.3 C
77.3 is the average test score and C is the grade
Grades are assigned as following: >=90 A, >=80 B, >=70 C, >=60 D, <60 F
Requirement:
Must have 3 functions -> To read students data from file, to assign grade to each student, to output names, average scores and grades of the students with a grade of C or better to a file
Use input data file students.txt, you may use an array of structs to store the data
Output the data in file name output.txt, each line should contain the first and last name, average test score and grade for only students with C or better.
*Did not attach file, I just need to know how the code would look like
Explanation / Answer
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
int n;
int Calc_Avg(vector<int> marks){
int sz = marks.size();
int sum = 0;
for(int i = 0; i<sz; i++)
sum = sum + marks[i];
return sum/sz;
}
char Calc_Grade(int avg){
if(avg >= 90)
return 'A';
if(avg >= 80)
return 'B';
if(avg >= 70)
return 'C';
if(avg >= 60)
return 'D';
return 'F';
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
cout<<"Enter total number of students"<<endl;
cin>>n;
for(int i = 1; i <= n; i++){
string fname, lname;
int score;
cin>>fname>>lname;
vector < int > marks;
for(int j = 1; j <= 5; j++){
cin>>score;
marks.push_back(score);
}
int average = Calc_Avg(marks);
char grade = Calc_Grade(average);
cout<<fname<<" "<<lname<<" "<<average<<" ";
if(grade <= 'C')
cout<<grade;
cout<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.