Question: LAB WORK --- JAVA Write a program that prompts the user for the size of a square array. (A square array has the same number
LAB WORK --- JAVA
Write a program that prompts the user for the size of a square array. (A square array has the same number of rows and columns). (a) (Sum elements column by column). (5 points) This requires you to create a function sumColumn. The signature of the function should be: public static int sumColumn(int m [][], int colIndex, int size)
(b) (Sum elements row by row). (5 points) This requires you to create a function sumRow. This is a second function you add to your class.
The signature of the function should be:
public static int sumRow(int m [][], int rowIndex, int size)
(c) (Sum elements by main diagonal). (5 points) This requires you to create a function sumMainDiag. This is a third function you add to your class. The main diagonal goes from top left to bottom right.
The signature of the function should be:
public static int sumMainDiag(int m [][], int size)
(d) (Sum elements by other diagonal). (5 points) This requires you to create a function sumOtherDiag. This is a fourth function you add to your class. The other diagonal goes from top right to bottom left.
The signature of the function should be: public static int sumOtherDiag(int m [][], int size)
(e) Populate the array with random numbers between 1 and 10. Have a function init, that takes two arguments the array and the dimension of the square matrix. (5 points)
The signature of the function should be: public static int[][] init(int m [][], int size)
(f) Have a function prt_aray, that takes two arguments: the array and the dimension of the square matrix. It should print the array, row by row. (5 points)
(g) Have a function init2, that takes two arguments the array and the dimension of the square matrix. It should prompt the user to enter size*size integer values and store them in the array. (5 points)
The signature of the function should be: public static void init2(int m [][], int size)
-------------------------------------------------------------------
For e.g. given a 3X3 matrix:
| 1 2 3 | | 4 5 6 | | 7 8 9 |
sum of col1 would be: 1+4+7 = 12 etc.
sum of row1 would be: 1+2+3 = 6 etc.
sum of main diagonal would be: 1+5+9 = 15
sum of other diagonal would be: 3+5+7 = 15
---------------------------------------------------------------
Here is a sample main method:
The comments are to guide you to fill in the blanks. (15 points)
public static void main(String[] args) {
int X[][], size; Scanner inp = new Scanner(System.in); System.out.println("Enter the size of the array"); size = inp.nextInt();
//Create or Instantiate the square array X, with size for columns and rows //write the command to do so. X =
//print the empty array, what are the values and why? System.out.println("Empty array"); prt_array(X,size); System.out.println();
//call the init function to initalize the array to random values. System.out.println("Intialize array to random values"); init(X,size); System.out.println();
//print the array, what are the values and why? System.out.println("Print the array"); prt_array(X,size);
//call the init2 function to initalize the array to values entered by the user. System.out.println("Intialize array to user values"); init2(X,size); System.out.println();
//print the array, what are the values and why? System.out.println("Print the array"); prt_array(X,size); System.out.println();
//create a for loop that will print out the sums of the columns (Its given to you). System.out.println(" Sum of columns"); for (int i = 0 ; i < size; i++) System.out.println("sum of column " + i + ": " + sumColumn(X,i, size) );
//create a for loop that will print out the sums of the rows. System.out.println(" Sum of rows"); ////Write the command above
//create a for loop that will print out the sum of the main diagonal. System.out.println(" Sum of main diagonal"); //Write the command above
//create a for loop that will print out the sum of the other diagonal. System.out.println(" Sum of other diagonal"); //Write the command above
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
