program in java which does the following Randomly Generate a 2D array. This opti
ID: 3779896 • Letter: P
Question
program in java which does the following
Randomly Generate a 2D array. This option will ask the user for the number of rows and columns in the array, as well as the range of numbers to generate, (i.e. The user can choose the min and max of the random number formula.) The random numbers should be integers.
Display the array in table format. (Print the 2D array)
Calculate and display the sum and average of the entire array.
Calculate and display the sum and average of each row.
Calculate and display the sum and average of each column.
Calculate and display the sum and average of the major and minor diagonals *see below.
Display the row and col with the highest average.
Display the row and col with the lowest average.
Be sure to use appropriate methods.
Major Diagonal: runs from upper Minor Diagonal: runs from upper right
left to lower right to lower left
1 2 3 1 2 3 1 2 1 2 3 1 2 3 1 2
3 4 5 4 5 6 3 4 3 4 5 4 5 6 3 4
7 8 9 5 6
Explanation / Answer
Note: This program works for Square matrix. Major and Minor diagonals are the features of square matrix.
import java.util.Scanner;
import java.util.Random;
class randomArray
{
public static void main(String args[])
{
int m,i,j,minm,maxm,arr_sum=0,major_sum=0,col_sum=0,row_sum=0,minor_sum=0;
float arr_avg=0,major_avg=0,row_avg=0,col_avg=0,minor_avg=0;
float low_col_avg=0,low_r_avg=0,high_r_avg=0,high_c_avg=0;
Scanner in=new Scanner(System.in);
System.out.println("Enter the size of the matrix");
m=in.nextInt();
System.out.println("Enter the minimum value");
minm=in.nextInt();
System.out.println("Enter the maximum value");
maxm=in.nextInt();
int[][] a=new int[m][m];
Random rn=new Random();
System.out.println("MATRIX A:");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=rn.nextInt(maxm-minm-1)+minm;
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
arr_sum+=a[i][j];
if(i==j)
major_sum+=a[i][j];
if(j==(m-i-1))
minor_sum+=a[i][j];
}
}
arr_avg=arr_sum/(m*m);
major_avg=major_sum/(m);
minor_avg=minor_sum/(m);
System.out.println("The sum of the entire array : "+arr_sum);
System.out.println("The average of the entire array : "+arr_avg);
System.out.println("The sum of the major diagonal : "+major_sum);
System.out.println("The average of the major diagonal : "+major_avg);
System.out.println("The sum of the minor diagonal : "+minor_sum);
System.out.println("The average of the minor diagonal : "+minor_avg+" ");
for(i=0;i<m;i++)
{
row_sum=0;
row_avg=0;
for(j=0;j<m;j++)
row_sum+=a[i][j];
row_avg=row_sum/(m);
if(i==0)
low_r_avg=row_avg;
if(low_r_avg>row_avg)
low_r_avg=row_avg;
if(high_r_avg<row_avg)
high_r_avg=row_avg;
System.out.println("The sum of row "+(i+1)+": "+row_sum);
System.out.println("The average of row "+(i+1)+": "+row_avg);
}
System.out.println("The lowest average of row : "+low_r_avg);
System.out.println("The highest average of row : "+high_r_avg+" ");
for(i=0;i<m;i++)
{
col_sum=0;
col_avg=0;
for(j=0;j<m;j++)
col_sum+=a[j][i];
col_avg=col_sum/(m);
if(i==0)
low_col_avg=col_avg;
if(low_col_avg>col_avg)
low_col_avg=col_avg;
if(high_c_avg<col_avg)
high_c_avg=col_avg;
System.out.println("The sum of column "+(i+1)+": "+col_sum);
System.out.println("The average of column "+(i+1)+": "+col_avg);
}
System.out.println("The lowest average of column : "+low_col_avg);
System.out.println("The highest average of column : "+high_c_avg);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.