I am suppost to have 3 functions in the program. It works fantastic, but the pro
ID: 3627752 • Letter: I
Question
I am suppost to have 3 functions in the program. It works fantastic, but the problem states that I need 3 function which are: 1. a function to read and store data into two arrays.2. a function to calculate the average test score and grade. 3. and a function to output the results. My instructor says I only have two of these. I need the function that I am missing in this program without messing up the actual output results because it is already correct. Can you please modify?
//Header files
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<math.h>
//namespace statement.
using namespace std;
// Theses are function prototypes
void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage);
char calculateGrade( double _grade);
const int numTests = 5;
int main()
{
// Declaring Variables
int count = 0;
double runningAverage =0.0;
string studentName;
// Declaring stream files
ifstream inFile;
ofstream outFile;
// opens the input file
inFile.open ("ch9_Ex13Data.txt");
// Display an error if the input file does not open.
if ( !inFile.is_open() )
{
//Terminates the program
cout << "Error in opening file.";
cout << "Program terminates." << endl;
return 1;
}
// Opens the output file
outFile.open ("Ch9_Ex13Out.txt");
//align the names to the left
cout.setf(ios::left);
cout << fixed << showpoint << setprecision (2);
cout << setw(10) << "Name" << setw(9);
cout.unsetf(ios::left);
//align the test headings to the right
cout.setf(ios::right);
cout << "Test 1" << setw(8) << "Test 2" << setw(8) << "Test 3" << setw(8) <<"Test 4" << setw(8) << "Test 5" << setw(11) << "Average" << setw(8) << "Grade" << endl;
//align the names to the left
outFile.setf(ios::left);
outFile << fixed << showpoint << setprecision (2);
outFile << setw(10) << "Name" << setw(9);
//align the test headings to the right
outFile.setf(ios::right);
outFile << "Test 1" << setw(8) << "Test 2" << setw(8) << "Test 3" << setw(8) <<"Test 4" << setw(8) << "Test 5" << setw(11) << "Average" << setw(8) << "Grade" << endl;
// Read and write a students name from the input file
inFile >> studentName;
while(inFile)
{
count++;
cout.unsetf(ios::right);
//align the names to the left
cout.setf(ios::left);
cout << setw(10) << studentName << setw(9);
outFile.unsetf(ios::right);
//align the names to the left
outFile.setf(ios::left);
outFile << setw(10) << studentName << setw(9);
calculateAverage(inFile, outFile, runningAverage);
inFile >> studentName;
}
cout << " Class average: " << (runningAverage/count) << endl << endl;;
outFile << " Class average: " << (runningAverage/count) << endl << endl;
//Close file Streams
inFile.close ();
outFile.close ();
system("pause");
return 0;
}
// average the test scores for each student
// Read and write a students test scores
void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage)
{
double testsTotal = 0;
double test = 0;
double testsAverage = 0.0;
cout.unsetf(ios::left);
cout.setf(ios::right); //align the tests to the right
cout << fixed << showpoint << setprecision (2);
_outFile.unsetf(ios::left);
_outFile.setf(ios::right); //align the tests to the right
_outFile << fixed << showpoint << setprecision (2);
for(int i=0; i < numTests; i++)
{
_inFile >> test;
testsTotal += test;
cout << test << setw(8);
_outFile << test << setw(8);
}
testsAverage = testsTotal / numTests;
_runningAverage += testsAverage;
cout << setw(11) << testsAverage;
cout << setw(6) << calculateGrade(testsAverage) << endl;
_outFile << setw(11) << testsAverage;
_outFile << setw(6) << calculateGrade(testsAverage) << endl;
}
// determines and returns the students letter grade
char calculateGrade (double _grade)
{
if (_grade < 60)
return 'F';
else if (_grade < 70)
return 'D';
else if (_grade < 80)
return 'C';
else if (_grade < 90)
return 'B';
else
return 'A';
}
Explanation / Answer
I wrote assuming that the number of tests and students were constant, if they aren't you have to use dynamic arrays and pointers so i don't know if that's too advanced for what you're working on at the moment //Header files #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<math.h> //namespace statement. using namespace std; const int numTests = 5; const int numStudents = 10; // Theses are function prototypes void getData(ifstream &inFile, string names[numStudents], double tests[numStudents][numTests + 1]); //#1 double calculateAverage(double tests[]); //#2 char getLetterGrade(double grade); void printtests(ofstream &outFile, string names[numStudents], double tests[numStudents][numTests + 1], double classAverage); //#3 int main() { // Declaring stream files ifstream inFile; inFile.open ("ch9_Ex13Data.txt"); // opens the input file ofstream outFile; outFile.open ("Ch9_Ex13Out.txt"); // Opens the output file string names[numStudents]; double tests[numStudents][numTests + 1]; //declaring arrays if(!inFile.is_open()) { //Terminates the program cout << "Error in opening file."; cout << "Program terminates." << endl; return 1; } // Display an error if the input file does not open. getData(inFile, names, tests); //gets data into arrays // Declaring Variables double runningAverage = 0.0, classAverage = 0.0; for(int i = 0; i < numStudents; i++) { tests[i][numTests] = calculateAverage(tests[i]); //storing the average in the last element of the tests array runningAverage += tests[i][numTests]; //adds up all the averages from students } classAverage = runningAverage / numStudents; //calcs class average printtests(outFile, names, tests, classAverage); //prints results system("pause"); return 0; } //reads in student test tests and name void getData(ifstream &inFile, string names[numStudents], double tests[numStudents][numTests + 1]) { for(int i = 0; i < numStudents; i++) { inFile >> names[i]; //gets name for(int j = 0; j < numTests ; j++) { inFile >> tests[i][j]; //gets test tests } } inFile.close (); //closing data file } // average the test tests for the student double calculateAverage(double tests[numTests]) { double total = 0; double average = 0.0; for(int i = 0; i < numTests; i++) { total += tests[i]; //gets test tests and adds to running total } average = total / numTests; //calcs the average return average; //returns the average } // determines and returns the students letter grade char getLetterGrade(double grade) { if (grade < 60) return 'F'; else if (grade < 70) return 'D'; else if (grade < 80) return 'C'; else if (grade < 90) return 'B'; else return 'A'; } void printtests(ofstream &outFile, string names[numStudents], double tests[numStudents][numTests + 1], double classAverage) { //align the names to the left cout.setf(ios::left); cout << fixed << showpoint << setprecision (2); cout << setw(10) << "Name" << setw(9); cout.unsetf(ios::left); //align the test headings to the right cout.setf(ios::right); cout << "Test 1" << setw(8) << "Test 2" << setw(8) << "Test 3" << setw(8) <<"Test 4" << setw(8) << "Test 5" << setw(11) << "Average" << setw(8) << "Grade" << endl; //align the names to the left outFile.setf(ios::left); outFile << fixed << showpoint << setprecision (2); outFile << setw(10) << "Name" << setw(9); //align the test headings to the right outFile.setf(ios::right); outFile << "Test 1" << setw(8) << "Test 2" << setw(8) << "Test 3" << setw(8) <<"Test 4" << setw(8) << "Test 5" << setw(11) << "Average" << setw(8) << "Grade" << endl; // Read and write a students name from the input file for(int i = 0; i < numStudents; i++) { cout.unsetf(ios::right); cout.setf(ios::left); //align the names to the left cout << setw(10) << names[i] << setw(9); cout.unsetf(ios::left); cout.setf(ios::right); //align the tests to the right cout << fixed << showpoint << setprecision (2); outFile.unsetf(ios::right); outFile.setf(ios::left); //align the names to the left outFile << setw(10) << names[i] << setw(9); outFile.unsetf(ios::left); outFile.setf(ios::right); //align the tests to the right outFile << fixed << showpoint << setprecision (2); for(int j = 0; j < numTests + 1; j++) { if(j == numTests) //if it's time to print the average of the tests, needs special formatting { cout << setw(11) << tests[i][j]; cout << setw(6) << getLetterGrade(tests[i][j]) << endl; outFile << setw(11) << tests[i][j]; outFile << setw(6) << getLetterGrade(tests[i][j]) << endl; } else //otherwise just print the test score { cout << tests[i][j] << setw(8); outFile << tests[i][j] << setw(8); } } } cout << " Class average: " << classAverage << endl << endl;; outFile << " Class average: " << classAverage << endl << endl; //class average outFile.close (); //closing output file } I wrote assuming that the number of tests and students were constant, if they aren't you have to use dynamic arrays and pointers so i don't know if that's too advanced for what you're working on at the moment //Header files #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<math.h> //namespace statement. using namespace std; const int numTests = 5; const int numStudents = 10; // Theses are function prototypes void getData(ifstream &inFile, string names[numStudents], double tests[numStudents][numTests + 1]); //#1 double calculateAverage(double tests[]); //#2 char getLetterGrade(double grade); void printtests(ofstream &outFile, string names[numStudents], double tests[numStudents][numTests + 1], double classAverage); //#3 int main() { // Declaring stream files ifstream inFile; inFile.open ("ch9_Ex13Data.txt"); // opens the input file ofstream outFile; outFile.open ("Ch9_Ex13Out.txt"); // Opens the output file string names[numStudents]; double tests[numStudents][numTests + 1]; //declaring arrays if(!inFile.is_open()) { //Terminates the program cout << "Error in opening file."; cout << "Program terminates." << endl; return 1; } // Display an error if the input file does not open. getData(inFile, names, tests); //gets data into arrays // Declaring Variables double runningAverage = 0.0, classAverage = 0.0; for(int i = 0; i < numStudents; i++) { tests[i][numTests] = calculateAverage(tests[i]); //storing the average in the last element of the tests array runningAverage += tests[i][numTests]; //adds up all the averages from students } classAverage = runningAverage / numStudents; //calcs class average printtests(outFile, names, tests, classAverage); //prints results system("pause"); return 0; } //reads in student test tests and name void getData(ifstream &inFile, string names[numStudents], double tests[numStudents][numTests + 1]) { for(int i = 0; i < numStudents; i++) { inFile >> names[i]; //gets name for(int j = 0; j < numTests ; j++) { inFile >> tests[i][j]; //gets test tests } } inFile.close (); //closing data file } // average the test tests for the student double calculateAverage(double tests[numTests]) { double total = 0; double average = 0.0; for(int i = 0; i < numTests; i++) { total += tests[i]; //gets test tests and adds to running total } average = total / numTests; //calcs the average return average; //returns the average } // determines and returns the students letter grade char getLetterGrade(double grade) { if (grade < 60) return 'F'; else if (grade < 70) return 'D'; else if (grade < 80) return 'C'; else if (grade < 90) return 'B'; else return 'A'; } void printtests(ofstream &outFile, string names[numStudents], double tests[numStudents][numTests + 1], double classAverage) { //align the names to the left cout.setf(ios::left); cout << fixed << showpoint << setprecision (2); cout << setw(10) << "Name" << setw(9); cout.unsetf(ios::left); //align the test headings to the right cout.setf(ios::right); cout << "Test 1" << setw(8) << "Test 2" << setw(8) << "Test 3" << setw(8) <<"Test 4" << setw(8) << "Test 5" << setw(11) << "Average" << setw(8) << "Grade" << endl; //align the names to the left outFile.setf(ios::left); outFile << fixed << showpoint << setprecision (2); outFile << setw(10) << "Name" << setw(9); //align the test headings to the right outFile.setf(ios::right); outFile << "Test 1" << setw(8) << "Test 2" << setw(8) << "Test 3" << setw(8) <<"Test 4" << setw(8) << "Test 5" << setw(11) << "Average" << setw(8) << "Grade" << endl; // Read and write a students name from the input file for(int i = 0; i < numStudents; i++) { cout.unsetf(ios::right); cout.setf(ios::left); //align the names to the left cout << setw(10) << names[i] << setw(9); cout.unsetf(ios::left); cout.setf(ios::right); //align the tests to the right cout << fixed << showpoint << setprecision (2); outFile.unsetf(ios::right); outFile.setf(ios::left); //align the names to the left outFile << setw(10) << names[i] << setw(9); outFile.unsetf(ios::left); outFile.setf(ios::right); //align the tests to the right outFile << fixed << showpoint << setprecision (2); for(int j = 0; j < numTests + 1; j++) { if(j == numTests) //if it's time to print the average of the tests, needs special formatting { cout << setw(11) << tests[i][j]; cout << setw(6) << getLetterGrade(tests[i][j]) << endl; outFile << setw(11) << tests[i][j]; outFile << setw(6) << getLetterGrade(tests[i][j]) << endl; } else //otherwise just print the test score { cout << tests[i][j] << setw(8); outFile << tests[i][j] << setw(8); } } } cout << " Class average: " << classAverage << endl << endl;; outFile << " Class average: " << classAverage << endl << endl; //class average outFile.close (); //closing output file }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.