In an input file each line has a students ID (4 digits), 4 test scores, and a fi
ID: 3625912 • Letter: I
Question
In an input file each line has a students ID (4 digits), 4 test scores, and a final exam score. How do you write a program, in C++ using arrays and/or functions, that takes this input file, and calculates a grade. The grade is equal to 50% test average plus 50% final exam score. After it does this, the program creates an output file and on each line of the output file is the students ID, 4 test scores, final exam score, grade (3 decimal places), and a letter grade based on a 10 point grading scale.Explanation / Answer
please rate - thanks
any problems, message me and I'll make the changes
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
char calculateGrade(double );
int main(){
int id,exam[4],final;
int i,sum;
char filename[80];
double grade;
ifstream in;
ofstream out;
cout<<"What is the name of your input data file? ";
cin>>filename;
in.open(filename); //open file
if(in.fail()) //is it ok?
{ cout<<"input file did not open please check it ";
system("pause");
return 1;
}
cout<<"What is the name of your output data file? ";
cin>>filename;
out.open(filename);
out<<"ID Exam1 Exam2 Exam3 Exam4 Final Average Grade ";
in>>id ;
while(in)
{sum=0;
for(i=0;i<4;i++)
{in>>exam[i];
sum+=exam[i];
}
in>>final;
grade=sum/4.*.5+.5*final;
out<<id;
for(i=0;i<4;i++)
out<<" "<<exam[i];
out<<" "<<final<<" "<<setprecision(3)<<fixed<<grade<<" "
<<calculateGrade(grade)<<endl;
in>>id;
}
out.close();
in.close();
return 0;
}
char calculateGrade(double average)
{if(average >=90)
return 'A';
else
if(average >=80)
return 'B';
else
if(average >=70)
return 'C';
else
if(average >=60)
return 'D';
else
return 'F';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.