In: Computer Science
Write a 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:
Demonstrate each of the methods in this program. Each (except for getElementCount, are called from main.
The main program will request the number of rows and columns as input, creates the two-dimensional array, and first calls fillRandom. A sample output is:
Please enter the number of rows and columns in a two dimensional array: 4 5
78 65 72 30 95
60 71 88 41 73
32 74 47 70 27
59 91 80 81 87
Output:
Processing the int array.
Total : 1321
Average : 66.05
Total of row 0 : 340
Highest in row 0 : 95
Lowest in row 0 : 30
Total of row 1 : 333
Highest in row 1 : 88
Lowest in row 1 : 41
Total of row 2 : 250
Highest in row 2 : 74
Lowest in row 2 : 27
Total of row 3 : 398
Highest in row 3 : 91
Lowest in row 3 : 59
Code- Main.java
import java.util.*;
public class Main
{
//fill random to array
static void fillRandom(int a[][]){
int i,j;
Random r = new Random();
for(i = 0 ; i < a.length;i++){
for(j = 0 ; j < a[i].length;j++){
a[i][j] = r.nextInt(100);
}
}
}
//print the array in formatted way
static void formatPrint(int a[][]){
int i,j;
for(i = 0 ; i < a.length;i++){
for(j = 0 ; j < a[i].length;j++){
System.out.printf("%4d ",a[i][j]);
}
System.out.println();
}
}
//function to get the element count in array
static int getElementCount(int a[][]){
return a.length*a[0].length;
}
//to get total of all element in array
static int getTotal(int a[][]){
int i,j,sum=0;
for(i = 0 ; i < a.length;i++){
for(j = 0 ; j < a[i].length;j++){
sum+=a[i][j];
}
}
return sum;
}
//function to get average of element in array
static int getAverage(int a[][]){
int total = getTotal(a);
int elementCount = getElementCount(a);
return total/elementCount;
}
//to get the row total
static int rowTotal(int a[][],int row){
int i,sum=0;
for(i = 0 ; i < a[row].length;i++){
sum+=a[row][i];
}
return sum;
}
//to get the col total
static int colTotal(int a[][],int col){
int i,sum=0;
for(i = 0 ; i < a.length;i++){
sum+=a[i][col];
}
return sum;
}
//to get highest in row
static int getHighestInRow(int a[][],int row){
int i,max = 0;
for(i = 0 ; i < a[row].length;i++){
if(max<a[row][i])
max = a[row][i];
}
return max;
}
//to get lowest in row
static int getLowerInRow(int a[][],int row){
int i,min = 999;
for(i = 0 ; i < a[row].length;i++){
if(min>a[row][i])
min = a[row][i];
}
return min;
}
public static void main(String[] args) {
//Scanner to get input
Scanner sc = new Scanner(System.in);
//ask user to input rows and column of matrix
System.out.println("Please enter
the number of rows and columns in a two dimensional array:
");
int rows = sc.nextInt();
int cols = sc.nextInt();
int a[][] = new
int[rows][cols];
//function calls and print
output
fillRandom(a);
formatPrint(a);
System.out.println("Total is
"+getTotal(a));
System.out.println("Average is
"+getAverage(a));
System.out.println("Total of column
2 "+colTotal(a,2));
System.out.println("Total of row 0
"+rowTotal(a,0));
System.out.println("Highest in row
0 "+getHighestInRow(a,0));
System.out.println("Lowest in row 0
"+getLowerInRow(a,0));
System.out.println("Total of row 1
"+rowTotal(a,1));
System.out.println("Highest in row
1 "+getHighestInRow(a,1));
System.out.println("Lowest in row 1
"+getLowerInRow(a,1));
System.out.println("Total of row 2
"+rowTotal(a,2));
System.out.println("Highest in row
2 "+getHighestInRow(a,2));
System.out.println("Lowest in row 2
"+getLowerInRow(a,2));
}
}
Screenshots-
pls do give a like , thank you