In: Computer Science
Write in main
• setArray() – This method has two integer parameters, one for the rows of a two-dimensional array and the other for the columns of a two-dimensional array. The method will return a two-dimensional array of integers with rows and columns given by the parameters. In addition, the method will use the Random class, along with the nextInt() method, to give each element of the two-dimensional array a value between 1 and 20.
• display() – This is a void method that has three parameters: a twodimensional array of integers, the number of rows, and the number of columns. The method will display all the elements of the array, under the following conditions: o Each row will be displayed on a separate line. o Each value in the array should be displayed using System.out.printf with a field width of 3 characters. o There should be at least one space between each element, so the rows line up neatly.
• rowAvg() – This is a void method that has three parameters: a twodimensional array of integers, the number of rows, and the number of columns. The method will calculate and display the averages of the elements of each row in the two-dimensional array. The averages should be displayed using System.out.printf so they can be rounded to two decimal places.
• colAvg() – This is a void method that has three parameters: a twodimensional array of integers, the number of rows, and the number of columns. The method will calculate and display the averages of the elements of each column in the two-dimensional array. The averages should be displayed using System.out.printf so they can be rounded to two decimal places.
import java.util.*;
class Array2D
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows :
");
int rows = input.nextInt();
System.out.println("Enter the number of columns :
");
int cols = input.nextInt();
int[][] array = new int[rows][cols];
array = setArray(rows,cols);
System.out.println("Random array : ");
display(array,rows,cols);
System.out.println("Average of rows in the array :
");
rowAvg(array,rows,cols);
System.out.println("\nAverage of columns in the array :
");
colAvg(array,rows,cols);
}
public static int[][] setArray(int rows,int cols)
{
int[][] array = new int[rows][cols];
Random rand = new Random();
for(int i = 0;i<rows;i++)
{
for(int j = 0;j<cols;j++)
{
array[i][j] = rand.nextInt(19) + 1;
}
}
return array;
}
public static void display(int[][] array,int
rows,int cols)
{
for(int i = 0;i<rows;i++)
{
for(int j = 0;j<cols;j++)
{
System.out.printf("%3d
",array[i][j]);
}
System.out.println();
}
}
public static void rowAvg(int[][] array,int
rows,int cols)
{
double[] avgRow = new double[rows];
for(int i = 0;i<rows;i++)
{
avgRow[i] = 0;
for(int j = 0;j<cols;j++)
{
avgRow[i] = avgRow[i] + array[i][j];
}
System.out.printf("%.2f\t",avgRow[i]/cols);
}
}
public static void colAvg(int[][] array,int rows,int cols)
{
double[] avgCol = new double[cols];
for(int i = 0;i<cols;i++)
{
avgCol[i] = 0;
for(int j = 0;j<rows;j++)
{
avgCol[i] = avgCol[i] + array[j][i];
}
System.out.printf("%.2f\t",avgCol[i]/rows);
}
}
}
Output:
Enter the number of rows :3
Enter the number of columns :4
Random array :
7 10 6 14
4 12 17 7
7 9 4 19
Average of rows in the array :
9.25 10.00 9.75
Average of columns in the array :
6.00 10.33 9.00 13.33
Do ask if any doubt. Please upvote.