Question
//******************************************************* //This program computes the average of a group of scores. //The scores are read from a file named scores.txt, formatted and written to an
//******************************************************* //This program computes the average of a group of scores. //The scores are read from a file named scores.txt, formatted and written to an output file //along with the average //Last Changed: June 2014 //***************************************************************** #include#include #include #include using namespace std; int calculateStudentAverage(ifstream& inp, double& courseAvg); //calculate the average of a group of scores and number of entries void formatData(istream& inp, ostream& oup, int precision, double avg); // Read numeric values from the original stream and write those numbers to the // updated stream with the precision defined by precision. Return the count of // numbers read and the average value of the numbers void setOutputFormat(ostream out, int decimal_places); // Set the number of decimal places in an output stream void waitForUser(); // Hold the program on the screen until the user hits any character int main( ) { ifstream inF; ofstream outF; double avgValue; int valueCount = 0, blankCount = 0; cout << "CSC-160 Support Program" << endl; setOutputFormat(cout, 3); inF.open("scores.txt"); if (!inF.fail()) { outF.open("scores.out"); if (!outF.fail()) { valueCount = calculateStudentAverage(inF, avgValue); formatData(inF, outF, 3, avgValue); cout << " Formatting Completed: " << " Input File had " << valueCount << " values, with an average value of " << avgValue; } else cout << endl << endl << "Unable to open output file"; } else cout << endl << endl << "Unable to open input file"; inF.close(); outF.close(); waitForUser(); return 0; } void setOutputFormat(ostream out, int decimal_places) { out.setf(ios::fixed); out.setf(ios::showpoint); out.precision(decimal_places); } void formatData(istream& in, ostream& out, int decimalPlaces, double avg) { double value = 0; setOutputFormat(out, decimalPlaces); in >> value; while (!in.eof()) { out << value << endl; in >> value; } out << "Average is: " << avg < > score; totalScore = totalScore + score; numberOfStudents++; inp >> score; }while (!inp.eof()); if (totalScore > 0.0 && !(numberOfStudents == 0)) courseAvg = totalScore / numberOfStudents; else courseAvg = 0; return numberOfStudents; }//end calculate Average void waitForUser() { cout << endl << endl; system("PAUSE"); }
Answer the following:
(a)Which program-defined functions is mixing double and int in its calculations and should be using "type casting"?
(b)Which lines of code declare local variables that are object references?
(c)As written, why won't the function setOutputFormat() compile?
(d)Could we change the call to the formatData() function at line 44 to direct the output to the terminal instead of an open file (ie. use cout as the second argument)? Why or why not?
(e)In calculateStudentAverage() we set the variable totalScore to an initial value (line 92) but do not initialize the variable score (line 94)? Is this an error? Why or why not.
(f)Which function parameter is also being used as an argument?
(g)The calculateStudentAverage() function has one or more logical errors: find one of those errors and describe it.
(h)Could we change the call to the formatData() function at line 44 to direct the output to the terminal instead of an open file (ie. use cout as the second argument)? Why or why not?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started