question archive Write program that creates a two-dimensional array initialized with test data

Write program that creates a two-dimensional array initialized with test data

Subject:Computer SciencePrice:4.86 Bought12

Write program that creates a two-dimensional array initialized with test data. Use any primitive data type that you wish. The program should have the following methods: getTotal. This method should accept a two-dimensional array as its argument and return the total of all the values in the array.

getAverage. This method should accept a two-dimensional array as its argument and return the average of all the values in the array.

getRowTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the total of the values in the specified row.

getColumnTotal. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a column in the array. The method should return the total of the values in the specified column.

getHighestInRow. This method should accept a two-dimensional array as its first argument and an integer as its second argument. The second argument should be the subscript of a row in the array. The method should return the highest value in the specified row of the array.

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

public class Main
{
	public static int getTotal(int[][] arr){
	    //variable declaration used to hold the sum pf all element in the 2D-array
	    int total = 0;
	    //track the row of the 2D-array
	    for(int i=0;i<arr.length;i++){
	        //track the column of 2D-array
	        for(int j = 0;j<arr[0].length;j++){
	            //calculate the sum of all the element of the 2D-array
	            total += arr[i][j];
	        }
	    }
	    
	    return total;
	}
	
	public static double getAverage(int[][] arr){
	    //calling getTotal() function and get the 2D-array total 
	    int total = getTotal(arr);
	    //calculate the number of element in the 2D-array
	    int numberOftotalELement = arr.length * arr[0].length;
	    
	    //calculate and return the average 
	    return (double)total / (double)numberOftotalELement;
	}
	
	public static int getRowTotal(int[][] arr,int row){
	    //variable declaration to hold the total of the specified row 
	    int total = 0;
	    
	    //as row is remain constant track column
	    for(int i=0;i<arr[0].length;i++){
	        total += arr[row][i];
	    }
	    
	    //return roe total 
	    return total;
	}
	
	public static int getColumnTotal(int[][] arr,int column){
	    //variable declaration to hold the total of the specified column
	    int total = 0;
	    
	    //track row as column is constant
	    for(int i=0;i<arr.length;i++)
	        total += arr[i][column];
	        
	    //return the column sum 
	    return total;
	}
	
	public static int getHighestInRow(int[][] arr,int row){
	    //max set the first element of the specified row 
	    int max = arr[row][0];
	    
	    //track the column of the specified row 
	    for(int i=0;i<arr[0].length;i++){
	        //compare variable max to the row element if max is less than element 
	        if(max < arr[row][i])
	            //set max to the next highest element in the row 
	            max = arr[row][i];
	    }
	    
	    //return the highest element of the row 
	    return max;
	}
	
	public static void main(String[] args) {
		//array declaration and initialization
		int[][] arr = {{2,55,43,89},{77,99,3,4},{21,4,55,3}};
		
		//function calling and printing the return value
		System.out.println("Total: "+getTotal(arr));
		System.out.printf("Average: %.2f\n",getAverage(arr));
		System.out.println("Total of row 2: "+getRowTotal(arr,1));
		System.out.println("Total of column 3: "+getColumnTotal(arr,2));
		System.out.println("Highest element of row 1: "+getHighestInRow(arr,0));
	}
}

Please see the attached file for the complete solution