//libraries
#include <fstream> //file input and output
#include <iostream> //console input and output
usingnamespace std;
//Programmer defined data types
//NONE
//Special compiler dependent definitions
//NONE
//global constants/variables
constint SIZE = 8; //size of the array
//Programmer defined functions
void introduction(string obj); //program introduction
int readScores(int* s, int count); //input scores from file
double calcAverage(int* s); //calculate average score
int countScoresGreater(int* s, double avgScore); //calculate the number of scores greater than average score
//main program
int main()
{
//Data
string objective = "input scores from file 8Scores.txt and calculate the average score and the number of scores > average score."; //program objective
int i; //loop index
int count; //check if file is empty
int scores[SIZE]; //all the scores input from the file
int nGreater = 0; //number of scores greater than the average score
double average; //average score
//user introduction
introduction(objective);
//input scores from input file
readScores(scores, count);
//if file was empty
if (count == 0) cout << "Sorry the file is empty. ";
if (count > 0) //if file was not empty
{
//calculate and output average score
average = calcAverage(scores);
cout << endl << "The average of " << SIZE << " numbers is " << average << endl;
//calculate the number of scores greater than average score
cout << "The number of scores greater than the average score is " << countScoresGreater(scores, average) << endl;
}
}//main
// introduction
void introduction(string obj)
{
//data
//obj is the program objective
//user introduction
cout << "Objective: This program will ";
cout << obj << endl;
cout << "Programmer: Teacher\n";
cout << "Editor(s) used: Notepad\n";
cout << "Compiler(s) used: TDM MinGW\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
}//introduction
//input scores from file
int readScores(int* s, int count)
{
//data
//s is an array of scores (passed from main)
int i; // loop index
int aScore = 0; //each score from input file
ifstream fin; //represent input file
string userFile; //user input file name
//
count = 0;
// get user file name
cout << "What is the input file name? ";
getline(cin, userFile);
cout <<endl;
//input scores from input file
fin.open(userFile.c_str()); //open specifed file from user
if (!fin.good()) throw"I/O error"; //if file isnt open output error to console
if (fin.good())
{
for (i = 0; i < SIZE; i++)
{
fin >> aScore;
fin.ignore(1000, 10);
s[i] = aScore;
count++;
cout << aScore << " ";
} // for
}
fin.close();
return count;
}//readScores
//calculate average score
double calcAverage(int* s)
{
//data
double result; //average score
//s array of scores from main
int i; //loop index
int scoreTotal = 0; //sum of all scores
//sum all the scores
for(i = 0; i < SIZE; i=i+1)
{
scoreTotal = scoreTotal + s[i];
}//for i
//calculate average score
result = scoreTotal / double(SIZE);
return result;
}//calcAverage
//calculate the number of scores greater than average score
int countScoresGreater(int* s, double avgScore)
{
//data
int result; //number of scores greater than the average score
//s is array of scores from main
//avgScore is the average score
int i; //loop index
//count the scores greater than the average score
for (i = 0; i < SIZE; i++)
{
if (s[i] > avgScore) result = result + 1;
}//for i
return result;
}//countScoresGreater