Calculate and display the sum and average of the major and minor diagonals of a
ID: 3779279 • Letter: C
Question
Calculate and display the sum and average of the major and minor diagonals of a 2D array with any amount of rows and columns in java.
Major Diagonal: runs from upper Minor Diagonal: runs from upper right
left to lower right to lower left
123 123 12 123 123 12
345 456 34 345 456 34
789 56 789 56
Explanation / Answer
import java.util.Scanner;
public class HelloWorld{
/* method to find sum of major diagonal of a matrix */
public static int majorDiagonal(int[][] arr){
//variable to store sum
int sum_major_diagonal=0;
//loop in which calculation is done
for (int i = 0, j =0; i< arr.length && j < arr.length ; i++, j++)
sum_major_diagonal= sum_major_diagonal + arr[i][j]; //calculating sum
//return the final sum value
return sum_major_diagonal;
}
/* end of the method majorDiagonal */
/* method to find sum of minor diagonal of a matrix */
public static int minorDiagonal(int[][] arr){
//variable to store sum
int sum_minor_diagonal=0;
//loop in which calculation is done
for (int i=0,j=arr.length-1 ; i<arr.length && j>=0 ; i++, j--)
sum_minor_diagonal= sum_minor_diagonal + arr[i][j]; //calculating sum
//return the final sum value
return sum_minor_diagonal;
}
/* end of the method minorDiagonal */
/* main function */
public static void main(String []args){
/* variable to store rows of the 2D array and in matrix rows=coloumn
hence no need to declare another variable for coloumn */
int rows;
Scanner input=new Scanner(System.in);
//prompting user to enter size of the matrix
System.out.println("Enter rows of the matrix :");
rows=input.nextInt();
//declaring 2D array
int[][] arr=new int[rows][rows];
//prompting user to enter elements of the 2D array
System.out.println("Enter Elements of the matrix :");
for(int i=0;i<rows;i++){
for(int j=0;j<rows;j++)
arr[i][j]=input.nextInt();
}
/* calling majorDiagonal and minorDiagonal methods and storing returned sum in sum_major_diagonal
and sum_minor_diagonal respectively */
int sum_major_diagonal=majorDiagonal(arr);
int sum_minor_diagonal=minorDiagonal(arr);
/* calculating average by dividing sum by no.of rows in the 2D array */
int avg_major_diagonal=sum_major_diagonal/rows;
int avg_minor_diagonal=sum_minor_diagonal/rows;
/* printing sum and average */
System.out.println("Sum of the major diagonal : "+sum_major_diagonal);
System.out.println("Sum of the minor diagonal : "+sum_minor_diagonal);
System.out.println("Average of the major diagonal : "+avg_major_diagonal);
System.out.println("Average of the minor diagonal : "+avg_minor_diagonal);
}
}
/* end of the code */
/*********OUTPUT************
Enter rows of the matrix :
3
Enter Elements of the matrix :
123
123
12
345
456
34
789
0
56
Sum of the major diagonal : 635
Sum of the minor diagonal : 1257
Average of the major diagonal : 211
Average of the minor diagonal : 419
******OUTPUT***********/
/* this code has been tested on eclipse,Please ask in case of any doubt,Thanks,God bless you. */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.