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

Looking for a well thought out algorithm only for this program. Should be simple

ID: 3627750 • Letter: L

Question

Looking for a well thought out algorithm only for this program. Should be simple for a well experienced programmer. It should flow with this program from beginning to end.




//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

Worth a try

getData
extract x student names into array, names[x]
extract y test scores into array, tests[x][y]

calculateAverage
loop to go through all test scores for current student
-hold running total of test scores
average = total / total tests
calculate and return the average

getLetterGrade
is grade < 60?
-return F
is grade < 70?
-return D
is grade < 80?
-return C
is grade < 90?
-return B
grade must be 90 - 100
-return A

printTests
print out headers, align left for name, right for tests, middle for letter grade
loop to go through all the students
-set up align left for names
-print name
-loop to go through all the tests
--set up align right for tests
--print tests followed by average
-set up align middle for letter grade
-print letter grade from calling getLetterGrade()
print class average

START
Open data file
if error with data file exit
Open output file
call getData()
for loop that goes through all students
-call calculateAverage() and puts in last element of tests[x]
-holds running total of averages
calculates class average
call printTests()
END

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