/*********************************************************************
 * Array2D - Provides a set of functions for getting data about an array.
 * 
 * @author arch and Nancy Harris
 * @version Jan 28, 2014
 * 
 ********************************************************************/

public class Array2D
{
    /********************************************************************
     * ERROR Return value if an error occurs.
     *******************************************************************/
    public static final double ERROR = Double.NEGATIVE_INFINITY;

    /*****************************************************************************************
     * Averages all the values in the array.
     * 
     * @param array
     *            The array to process
     * @return The average of all the array elements or ERROR if the parameter is null or the
     *         array is empty.
     ****************************************************************************************/
    public static double getAverage(double[][] array)
    {
       
		double sum = 0;
		double count = 0;
		double average = ERROR;
		
		if (array != null)
		{
			sum = getTotal(array);
			
			for (int ii = 0; ii < array.length; ii++)
			{
				if (array[ii] != null)
				{
					count = count + array[ii].length;
				}
			}
			if (count > 0)
				average = sum / count;
	   }		
	
		
		return average;
	}
 
    /*****************************************************************************************
     * getColumnTotal sums all of the values in one column of this array.
     * 
     * @param col
     *            The number (0 based) of the col to sum
     * @param array
     *            The array to process
     * 
     * @return The sum of the array elements in that column or ERROR if the parameter is null
     *         or the array col does not exist.
     ****************************************************************************************/
    public static double getColumnTotal(int col, double[][] array)
    {
        double sum = ERROR;
		
		if (array != null)
		{
			if (isCol(col, array))
			{
            sum = 0;
				for (int row = 0; row < array.length; row++)
				{
					if (array[row] != null && array[row].length > col)
						sum = sum + array[row][col]; 
				}
			}
		}
		
		return sum;
    }

    /*****************************************************************************************
     * getHighestInRow finds the largest value one row of this array.
     * 
     * @param row
     *            The number (0 based) of the row to find the largest
     * @param array
     *            The array to process
     * 
     * @return The highest of the row array elements or ERROR if the parameter is null or the
     *         array row does not exist.
     ****************************************************************************************/
    public static double getHighestInRow(int row, double[][] array)
    {
        double highest = ERROR;
		
		if (array != null)
		{
			if (isRow(row, array) && array[row] != null)
			{
				if (array[row].length > 0)
				{
            	highest = array[row][0];  
					for (int col = 0; col < array[row].length; col++)
						if (array[row][col] > highest)
							highest = array[row][col];
				}
			}
		}
		
		return highest;
    }

    /*****************************************************************************************
     * getLowestInRow finds the lowest value one row of this array.
     * 
     * @param row
     *            The number (0 based) of the row to find the lowest
     * @param array
     *            The array to process
     * 
     * @return The lowest of the row array elements or ERROR if the parameter is null or the
     *         array row does not exist.
     ****************************************************************************************/
    public static double getLowestInRow(int row, double[][] array)
    {
        double lowest = ERROR;
			
			if (array != null)
			{
				if (isRow(row, array) && array[row] != null)
				{
					if (array[row].length > 0)
					{
						lowest = array[row][0];  
						for (int col = 0; col < array[row].length; col++)
							if (array[row][col] < lowest)
								lowest = array[row][col];
					}
				}
		    }
          
          return lowest;
      }

    /*****************************************************************************************
     * getRowTotal sums all of the values in one row of this array.
     * 
     * @param row
     *            The number (0 based) of the row to sum
     * @param array
     *            The array to process
     * 
     * @return The sum of the row array elements or ERROR if the parameter is null or the array
     *         row does not exist.
     ****************************************************************************************/
    public static double getRowTotal(int row, double[][] array)
    {
        double sum = ERROR;
		if (array != null)
		{	
			if(isRow(row, array))
			{
            sum = 0;
				for (int col = 0; col < array[row].length; col++)
					sum = sum + array[row][col];
			}
		}		
		return sum;
    }

    /*****************************************************************************************
     * getTotal sums all of the values of this array.
     * 
     * @param array
     *            The array to process
     * @return The sum of the array elements or ERROR if the parameter is null.
     ****************************************************************************************/
    public static double getTotal(double[][] array)
    {
        double total = ERROR;
		
		if (array != null)
		{
			for (int row = 0; row < array.length; row ++)
			{
				if (array[row] != null)
				{
               total = 0;
					for (int col = 0; col < array[row].length; col++)
					{
						total = total + array [row][col];
					}
				}
			}
		}
				
		return total;
    }

    /*****************************************************************************************
     * @param col
     *            The column to check
     * @param array
     *            The array to process
     * @return true if the row exists in this array, false otherwise
     ****************************************************************************************/
    public static boolean isCol(int col, double[][] array)
    {
        boolean column = false;
    	
	    if (array != null)
	    {
	    	for (int row = 0; row < array.length && !column; row++)
	    		if (array[row].length > col)
	    			column = true;
	    }
	    	    
	    return column;
    }

    /****************************************************************************************
     * @param row
     *            The row to check
     * @param array
     *            The array to process
     * @return true if the row exists in this array, false otherwise
     ****************************************************************************************/
    public static boolean isRow(int row, double[][] array)
    {
        boolean isRow = false;
    	
	    if (array != null)
	    {
	    	if (array.length > row)
	    		isRow = true;
	    }
	    	    
	    return isRow;
    }
}