Question

In: Computer Science

Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...

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:

  • fillRandom. Accepts a reference to a two-dimensional array and fills it with random integers from 0 to 99
  • formatPrint. This method should accept a two-dimensional array and print it out row by row
  • 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. Calls getTotal and getElementCount.
  • 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 sub- script 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.
  • getLowestInRow. 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 sub- script of a row in the array. The method should return the lowest value in the specified row of the array.
  • getElementCount. This method should accept a two-dimensional array and returns the total number of elements in the array.

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

Solutions

Expert Solution

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


Related Solutions

Write a program that creates a two-dimensional array initialized with test data. Use any primitive data...
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: -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...
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array. ▪ getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array. ▪ getRowTotal ....
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:
IN JAVA Array Operations Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods: getTotal: This method should accept a one-dimensional array as its argument and return the total of the values in the array. getAverage: This method should accept a one-dimensional array as its argument and return the average of the values in the array. getHighest: This method should accept a...
Q in c++ Write a program that will find the inverse of any two dimensional array...
Q in c++ Write a program that will find the inverse of any two dimensional array without the usage of the built-in function.? plz answer in while or do while loop
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT