Thursday 25 August 2016

Chapter 8 Exercise 2, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*8.2 (Sum the major diagonal in a matrix) Write a method that sums all the numbers in the major diagonal in an n * n matrix of double values using the following header: public static double sumMajorDiagonal(double[][] m) Write a test program that reads a 4-by-4 matrix and displays the sum of all its elements on the major diagonal. Here is a sample run: Enter a 4-by-4 matrix row by row: 1 2 3 4.0 5 6.5 7 8 9 10 11 12 13 14 15 16 Sum of the elements in the major diagonal is 34.5



import java.util.Scanner;
 
public class ProgramingEx8_2 {
 
 public static void main(String[] args) {
 
  double[][] matrix = new double[4][4];
 
  java.util.Scanner input = new Scanner(System.in);
  System.out.println("Enter a 4-by-4 matrix row by row: ");
  for (int row = 0; row < matrix.length; row++) {
   for (int column = 0; column < matrix[row].length; column++) {
    matrix[row][column] = input.nextDouble();
   }
  }
 
  System.out.println("Sum of the elements in the major diagonal is "
    + sumMajorDiagonal(matrix));
 
 }
 
 public static double sumMajorDiagonal(double[][] m) {
 
  double sum = 0;
  for (int row = 0; row < m.length; row++) {
   sum += m[row][row];
  }
  return sum;
 }
 
}

No comments :

Post a Comment