// ****************************************************************** //
//  Jeff Balsley                                                      //
//                                                                    //
//                                                                    //
// ****************************************************************** //
//  Known bugs:                                                       //
//                                                                    //
// ****************************************************************** //
//  to compile:  g++ -lm ass3.2.cpp -o ass3.2                         //
//                                                                    //

#include<iostream>
#include<cmath>
using namespace std;

void tableFill(double myArray[], int size);
double tableAverage(double myArray[], int n1, int n2);

int main()
{
    int size;           // size of array
    double temp_size;
    double average;
    int n1, n2;         // we will take the average between these
                        // two array elements.
    do {
        cout << "Enter the size of the array (integer) > ";
        cin >> temp_size;

        if ( temp_size <=0 ){
            cout << "*** that is not valid input ***\n";
        }
        if ( floor(temp_size) != ceil(temp_size) ){
            cout << "*** that is not valid input ***\n";
        }

    } while ( (temp_size <=0) || ( floor(temp_size) != ceil(temp_size) ) );

    size = (int)temp_size;

    double * myArray = new double [size];

    tableFill(myArray, size);
    cout << "This program will now take the average between any two ";
    cout << "elements of the array.\n";

    // Get element 1 from the user

    do {
        cout << "Enter the element 1 (integer) > ";
        cin >> n1;

        // make sure integer is not outside array
        if (n1 >= size-1 || n1 < 0){
            cout << "**** That integer is outside the array! ****\n";
        }
    } while ( n1 >= size-1 || n1 < 0);

    // Get element 2 from the user
    do {
        cout << "Enter the element 2 (integer) > ";
        cin >> n2;

        // make sure element is not outside the array
        if (n2 >= size){
            cout << "**** That integer is too large! ****\n";
        }

        // make sure the element is not less than n1
        if (n2 <= n1){
            cout << "**** That integer is <= element 1! ****\n";
        }
    } while (n2>= size || n2 <= n1);

    average = tableAverage(myArray, n1, n2);

    // print average
    cout << "The average between " << n1 << " and " << n2;
    cout << " is " << average << "\n";

    return 0;
}

// ******************************************************************* //
//  This function will take an previously declared array and ask the   //
//  the user for integers to fill it with.                             //
// ******************************************************************* //
void tableFill(double myArray[], int size)
{
    int i;

    for (i=0; i<size; ++i){
        cout << "Enter value for element " << i << " in the array > ";
        cin >> myArray[i];

        // Check to make sure the user entered an integer
        while (floor(myArray[i]) != ceil(myArray[i])){
            cout << "**** That is not an integer! ****\n";
            cout << "Enter value for element " << i << " in the array > ";
            cin >> myArray[i];
        }
    }
    return;
}

// ******************************************************************* //
//  This fuction will return the average of an array that is passed    //
//  to it.                                                             //
// ******************************************************************* //
double tableAverage(double myArray[], int n1, int n2)
{
    int i;
    double total = 0;  // sum of all the array elements
    double average;    // array average

    //  Sum up all the elements of the array
    for (i=n1; i<= n2; ++i){
        total = total + myArray[i];
    }
    average = total / (n2-n1+1);

    return(average);
}
