SOURCE FILE: 07_averager.cpp



/*
     This program processes input files that look like this:
    
61 66 100 82 77 92 61 63 68 78 96 80 88 99 83

7

6    121 28 4 125 146 56

8    120 44 114 148 25 78 85 136

5    19 95 19 78 138

4    116 18 24 84

3    105 23 134

5    104 90 94 117 73

5    65 144 76 91 41
    
    The program assumes that the name of the input file is: data

    The program reads the first (non-blank) line of numbers, copying
    them into an array called: target

    The number of integers on that first line is the value of
    maxTarget, which is a global const int of the program.
 
    The program then reads the single integer on the next non-blank
    line, and copies it into a variable called: numLines

    The value of numLines is the number of remaining non-blank lines
    in the input file.

    The program reads each of these remaining lines.  
    Each of them has a format like this:

    5    19 95 19 78 138

    In the example, the 5 means that this line has 5 values to be
    averaged.

    The program computes the appropriate average for each line
    and writes a report comparing the averages with the corresponding
    target value.

    For example, if the data is as above, the program reads this line
    into the array called target:

         61 66 100 82 77 92 61 63 68 78 96 80 88 99 83

    Then the program reads the 7, which tells it that there are 7
    lines to process.
   
    The program then processes each of the seven lines.

    As an example of what happens when a line is processed, consider
    the processing of this line:

     8    120 44 114 148 25 78 85 136

     When the program reads the 8, that tells it there are 8 more
     numbers on the line.  The program reads the 8 numbers.  As it
     reads them, it computes their sum.  Then it divides the sum by
     8.0, which gives the average.  The program writes to the screen
     the line number, the average, the corresponding target, and says
     whether the average exceeds the corresponding target.  Since this
     line:

     8    120 44 114 148 25 78 85 136

     is the second line to be processed, the average is compared to
     the second target, which is 66 in the example data above.

     Here is what the report for the sample data looks like:

Line Number    Average    Target    Exceeds?

     1          80.00       61        YES
     2          93.75       66        YES
     3          69.80      100         NO
     4          60.50       82         NO
     5          87.33       77        YES
     6          95.60       92        YES
     7          83.40       61        YES

*/

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std ;

const int maxTargets = 15 ;

/* ************************** */
void MakeHeadings () ;
   /*
        This function just makes column headings for a report, 
        like this:

Line Number    Average    Target    Exceeds?

   */

/* ************************** */
double AverageLine (ifstream & fin) ;
   /*

       This function reads the information on the next line of the
       data file, computes the indicated average, and returns it.  For
       example, if the next line is:

5    104 90 94 117 73

      then this function sums these numbers: 104 90 94 117 73,
      and returns the value of the sum divided by 5.0.
   
      In order to work correctly, the function must be called
      when the next thing to be read from fin is the beginning
      of a new line.  
   */

/* ************************** */
void ReportLine(int linePosition, double ave, int targ) ;
   /*
       This function writes one line of a report, like these lines:

     3          77.25       99         NO

     6         109.83       99        YES

       The first number is the value of linePosition, the second 
       is the value of ave, and the third is the value of targ.
       The final string is YES if ave is more than targ, else it
       is NO.  The output is spaced so it lines up under the 
       headings like this:

Line Number    Average    Target    Exceeds?

     1         128.67       97        YES
     2         100.75      110         NO
     3          77.25       99         NO
     4         119.50       89        YES
   */

/* ************************** */
int main (void)
{
   int numLines, target[maxTargets] ;
   double average ;

   ifstream f_in("data") ;

   for (int targNum=0; targNum<maxTargets; targNum++)
        f_in >> target[targNum] ;

   f_in >> numLines ;

   MakeHeadings () ;

   for (int lineNum=1; lineNum <= numLines; lineNum++)
   {
      average = AverageLine(f_in) ;
      ReportLine(lineNum, average, target[lineNum-1]) ;
   }
   
   cout << endl << endl ;
   
   return 0;
}

/* ************************** */
/* ************************** */
void MakeHeadings ()
{
   cout << endl ;
   cout << "Line Number    Average    Target    Exceeds?" ;
   cout << endl << endl ;
}

/* ************************** */
/* ************************** */
double AverageLine (ifstream & fin)
{
  int numValues, nextNum, sum = 0 ;

  fin >> numValues ;

  for (int colNum =1; colNum <= numValues; colNum++)
  {
     fin >> nextNum ;
     sum = sum + nextNum ;
  }
  return (sum / static_cast<double>(numValues) ) ;
}

/* ************************** */
/* ************************** */
void ReportLine(int linePos, double ave, int targ) 
{
  cout.setf(ios::fixed) ;
  cout.setf(ios::showpoint) ;
  cout.precision(2) ;

  cout << setw(6) << linePos ;
  cout << setw(15) << ave ;
  cout << setw(9) << targ ;
  if (ave <= targ) 
     cout << setw(11) << "NO" ;
     else cout << setw(11) << "YES" ;
  cout << endl ;
}