/** Array2D - Provides tools for manipulating a 2D array
   
	@author - CS239 Teams
	@version - V1 - 01/15/2008
 */
public class Array2D
{
	/** getTotal sums all of the values of the array.
	 *	Team PocketProtector
	 * @param matrix The two dimensional array
	 * @return The sum of the array elements
	 */
	public static double getTotal(double [][] matrix)
	{
		double total;
		total = 0;
		
		for (int row = 0; row < matrix.length; row++)
		{
				for (int col = 0; col < matrix[row].length; col++)
				{
					total = total + matrix[row][col];
				
				}
		
		
		}
		return total;
	}
	
	/** getaverage averages all of the values of the array.
	 *
	 *Wu Tang Clan
	 * @param matrix The two dimensional array
	 * @return The average of the array elements
	 */

	public static double getAverage(double [][] matrix)
	{
		int numberOfNum;
		double total;
		double average;
		
		total = getTotal(matrix);
		numberOfNum = (matrix.length) * ( matrix[0].length);
		average = total / numberOfNum;
		return average;
	}
	
	/** getRowTotal sums all of the values in one row of the array.
	 *  
	 *  Team C.A.B.S.
	 * @param matrix The two dimensional array
	 * @param row The number (0 based) of the row to sum
	 *
	 * @return The sum of the row array elements.  If the row does not exist
	 *         in matrix....YOU FILL IN THE REST.
	 */

	public static double getRowTotal(double [][] matrix, int row)
	{
		double answer = 0;
		
		if ( matrix.length > row )
		{
		    for ( int i =0; i < matrix[row].length; i++ )
			     answer += matrix[row][i];
			 
		}
		else
		{
		    System.out.println("Row index not acceptable");
			 answer = Double.MIN_VALUE;
		}
		return answer;
	}
	
	/** getColumnTotal sums all of the values in one column of the array.
	 *
	 * @param matrix The two dimensional array
	 * @param col The number (0 based) of the col to sum
	 *
	 * @return The sum of the array elementsin that column.  
	 *				If the row does not exist in matrix, return MIN_VALUE;
	 */
	public static double getColumnTotal (double [][] matrix, int col)
	{
		double total = 0;
		
		if (col >= matrix[0].length)
		{
			System.out.println("Column index not acceptable");
			total = Double.MIN_VALUE;
		}
		else
		{
			for ( int row = 0; row < matrix.length; row++)
			{
				total += matrix[row][col];
			}
		}
			
		return total;
	}

	/** getHighestInRow finds the largest value one row of the array.
	 *
	 * Teim - There is an I in teim.
	 *
	 * @param matrix The two dimensional array
	 * @param row The number (0 based) of the row to find the largest
	 *
	 * @return The highest of the row array elements.  If the row does not exist
	 *         in matrix return -1.
	 */
	public static double getHighestInRow(double [][] matrix, int row)
	{
		double rowHigh;
		
		if( row < matrix.length )
		{
			rowHigh = matrix[row][0];
			for( int col = 0; col < matrix[row].length; col++)
			{
				if( matrix[row][col] > rowHigh )
					rowHigh = matrix[row][col];
			}
		}
		else
		{
			System.out.println("Row " + row + " does not exist.");
			rowHigh = Double.MIN_VALUE;
		}
				
		return rowHigh;
	}
	
	/** getLowestInRow finds the lowest value one row of the array.
	 *
	 * Team NARKOLEPSY
	 *
	 * @param matrix The two dimensional array
	 * @param row The number (0 based) of the row to find the lowest
	 *
	 * @return The lowest of the row array elements.  If the row does not exist
	 *         in matrix, return the minimum allowed value.
	 */
	public static double getLowestInRow(double [][] matrix, int row)
	{
		double rowMin = Double.MAX_VALUE;
		
		// there is a better way to do this.  You do not need exception 
		// handling
		try
		{
			for( int element = 0; element < matrix[row].length; element++ )
			{
				if( matrix[row][element] < rowMin )
					rowMin = matrix[row][element];
			}
		}
		catch ( IndexOutOfBoundsException ioobe )
		{
			System.out.println( "Row " + row + " does not exist." );
			rowMin = Double.MIN_VALUE;
		}
		
		return rowMin;
	}
}