Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads the scores of a student from a file into an array and

ID: 3824530 • Letter: W

Question

Write a program that reads the scores of a student from a file into an array and writes the average score and the corresponding grade in the output file. You can download the input file “Scores.txt” from moodle under recitation 8. You can name you output file as “Grade.txt”. The input file contains scores of a student in 7 subjects with each score written on a newline. Your program must contain separate functions for reading and writing a file. You should declare the array of scores in your main function and pass it to these functions.

Pass the input file name and the empty scores array to the readScores() function. This function should read the scores from the input file and store the scores into your array.

           void readScores(string inputFile , float scores[])

Pass the output file name and the array of scores (which should be populated now) to the writeGrade() function. This function should calculate the average score of the student and write the average score and the corresponding grade to the output file in the following format: Average score, Grade

void writeGrade(string outputFile , float scores[])

Use the following rules to calculate the grade.

For average scores greater than and equal to 90 Grade is A  

For average scores greater than and equal to 80 and lesser than 90) Grades is B

For average scores greater than and equal to 70 and lesser than 80) Grades is C

For average scores greater than and equal to 60 and lesser than 70) Grades is D

For average scores lesser than 60 Grades is F

scores:


Explanation / Answer

// C++ code
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string> // std::string, std::to_string
#include <math.h>
#include <fstream>

using namespace std;

void readScores(string inputFile , float scores[])
{
int i = 0;
ifstream inFile (inputFile.c_str());
if (inFile.is_open())
{
  
while(true)
{
inFile >> scores[i];
i++;

if(inFile.eof())
break;
}
inFile.close();
}

else cout << "Unable to open file";
}

void writeGrade(string outputFile , float scores[])
{
ofstream outFile;
outFile.open (outputFile.c_str());

float average = 0;
for (int i = 0; i < 7; ++i)
{
average = average + scores[i];
}

average = average/7;

char letterGrade;

if(average >= 90)
letterGrade = 'A';
else if(average >= 80)
letterGrade = 'B';
else if(average >= 70)
letterGrade = 'C';
else if(average >= 60)
letterGrade = 'D';
else
letterGrade = 'F';

outFile << "Average Score: " << average << endl << "Grade: " << letterGrade << endl;
outFile.close();
}
int main()
{
string inputFile, outputFile;

cout << "Enter input file name: ";
cin >> inputFile;
cout << "Enter output file name: ";
cin >> outputFile;

float scores[7];

readScores(inputFile,scores);

writeGrade(outputFile,scores);

return 0;
}

/*
scores.txt
75
90
88
64
100
56
82

grades.txt:
Average Score: 79.2857
Grade: C


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote