// A program to read n numbers, compute their maximum,  minimum, and average,  
// and to print a specified jth item.
// Version 2:  Using "safe" C++ vectors from the AP CS subset 
//             rather than arrays.

#include <iostream.h>
#include "apvector.cpp"                /* Vectors defined in local file */

int main (void)
{    int j, n;
     double max, min, sum;

     cout << "Program to process real numbers." << endl;
     cout << "Enter number of reals: ";
     cin >> n;

     apvector<double> a(n);            /* declare vector of n values,
                                       with subscripts 0, ..., n-1  */
     cout << "Enter " << n << " numbers: " ;
     for (j = 0; j < n; j++)
         cin >> a[j];           /* subscripts given in brackets [ ] */

     sum =  max = min = a[0];  /* right to left assignment operator */

     for (j = 1; j < n; j++)
       { if (a[j] > max)
            max = a[j];     
         if (a[j] < min)
            min = a[j];
         sum += a[j];
       }

     cout << "Maximum:  " << max << endl;
     cout << "Minimum:  " << min << endl;
     cout << "Average:  " << sum/n << endl << endl;

     cout << "Enter the index (1..n) of the number to be printed: ";
     cin >> j;
     cout << "The " << j << "th number is "  << a[j-1] << endl;

     return 0;
}
