Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

program language- JAVA This is a homework assaignment chapter 7 problem 18 Write

ID: 3625066 • Letter: P

Question

program language- JAVA

This is a homework assaignment chapter 7 problem 18 Write a program that creates a 2D array with initialized testdata. Use any data type you wish. The program shouldhave the following functions:
getTotal - this function should accepty a 2Darray as its arguement and return the total of all the values ofthe array
getAverage - same as getTotal but return theaverage of all the values in the array
getRowTotal - this function should accept a 2Darray as its first arguement and an integer as its secondargument. The second argument should be the subscript of arow in the array. The function should return the total of thevalues in the specified row.
getColumnTotal - This function should accept 2Darray as its first argument and an integer as its secondargument. The second argument should be the subscript of acolumn in the array. The function should return the total ofthe values in the specified column.
getHighestInRow - This function should accept a2D array as its first argument and in integer as its secondargument. The second argument should be the subscript of arow in the array. The function should return the highestvalue in the specified row of the array.
getLowestInRow - same as get highest in row butreturn lowest value in the specified row of the array.

Explanation / Answer

please rate - thanks

import java.util.*;
    public class matrix718
   {
      public static void main(String[] args)
      {Scanner in = new Scanner(System.in);
        int n,i,j;
        System.out.print("Enter size of matrix: ");
        n=in.nextInt();
        while(n<2)
           {System.out.println("size must be at least 2");
            System.out.print("Enter size of matrix: ");
           n=in.nextInt();
            }       
        int [][] a = new int [n][n];
      Random r=new Random();
        for(i=0;i<n;i++)
           for(j=0;j<n;j++)
               {
                a[i][j]=r.nextInt(500);
                }
        System.out.println("The Matrix:");
        for(i=0;i<n;i++)
           {for(j=0;j<n;j++)
               System.out.print(a[i][j]+" ");
            System.out.println(" ");
            }
        System.out.println("The total of all elements is "+getTotal(a));
        System.out.println("The average of all elements is "+getAverage(a));
        System.out.println("The total of all elements in row 3 is "+getRowTotal(a,3));
        System.out.println("The total of all elements in column 3 is "+getColumnTotal(a,3));
        System.out.println("The highest element in row 3 is "+getHighestInRow(a,3));
        System.out.println("The lowest element in column 3 is "+getLowestInRow(a,3));
    }
public static int getTotal(int a[][])
{int i,j,tot=0;
for(i=0;i<a.length;i++)
    for(j=0;j<a[0].length;j++)
          tot+=a[i][j];
return tot;
}
public static double getAverage(int a[][])
{return (double)getTotal(a)/(a.length*a[0].length);
}
public static int getRowTotal(int a[][],int n)
{int j,tot=0;
    for(j=0;j<a[0].length;j++)
          tot+=a[n][j];
return tot;
}
public static int getHighestInRow(int a[][],int n)
{int j,max=a[n][0];
    for(j=0;j<a[0].length;j++)
          if(a[n][j]>max)
                 max=a[n][j];
return max;
}
public static int getLowestInRow(int a[][],int n)
{int j,low=a[n][0];
    for(j=0;j<a[0].length;j++)
          if(a[n][j]<low)
                 low=a[n][j];
return low;
}

public static int getColumnTotal(int a[][],int n)
{int j,tot=0;
    for(j=0;j<a.length;j++)
          tot+=a[j][n];
return tot;
}

}