Question

In: Computer Science

java - Write a method that sums all the numbers in the major diagonal in an...

java - Write a method that sums all the numbers in the major diagonal in an n x n matrix of double  values using the following header:


public static  double sumMajorDiagonal(double [][] m)

Write a test program that first prompts the user to enter the dimension n of an n x n matrix, then asks them to enter the matrix row by row (with the elements separated by spaces). The program should then print out the sum of the major diagonal of the matrix.

SAMPLE RUN:

Enter dimension n of nxn matrix:Enter row·0:Enter row 1:Enter·row 2:Enter row 3:17.1

(can have 3 or 4 rows)

Solutions

Expert Solution

//DiagonalSum.java

import java.io.*;
import java.util.*;


class DiagonalSum
{
public static double sumMajorDiagonal(double[][] matrix)
{
  
double sum=0.0;
for(int i=0;i<matrix.length;i++)
sum=sum+matrix[i][i];
  
return sum;
  
}
public static void main(String[] args)
{
  
Scanner scan= new Scanner(System.in);

System.out.print("Enter dimension n of nxn Matrix: ");
int n=scan.nextInt();

double matrix[][]=new double[n][n];

for(int i=0;i<n;i++)
{
System.out.print("Enter row " + (i) + ": ");
for(int j=0;j<n;j++)
{
matrix[i][j]=scan.nextDouble();
}
}

System.out.println("\nInput Matrix:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(matrix[i][j]+"\t");
}
System.out.println();
}
  
System.out.println("\nSum of Major diagonal:" + sumMajorDiagonal(matrix) );

}
}

/*
output:

Enter dimension n of nxn Matrix: 3
Enter row 0: 1 2 3
Enter row 1: 4 5 6
Enter row 2: 7 8 9

Input Matrix:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0

Sum of Major diagonal:15.0

*/


Related Solutions

Use the following two loop strategies to write a function that sums all even numbers before...
Use the following two loop strategies to write a function that sums all even numbers before an odd number is observed in any given numeric vector, and test your code in R. For example, if the input vector is 2, 4, -2, 3, 4, 5, then the first odd number appears in the fourth position, which is 3. The output should be the sum of the first to the third numbers, which will be 2 + 4 − 2 =...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
Language: Java So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE...
Language: Java So I am trying to find the MAJOR and MINOR diagonal SUM and AVERAGE of a 2d matrix using only ONE class. However, the output gives me the incorrect calculations. This is my class: public static void MajorAndMinorDiagonalSumAndAvg (Scanner user, int rows, int coluumn, int [][] array) { double majorarray = 0; double majorarraycount = 0; double minorarray = 0; double minorarraycount = 0; for (int i = 0; i<array.length; i++) for(int j =0; j<array[i].length; j++) { majorarray...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should...
Java Special Methods: A) Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator. B) Write methods to implement the multiply, subtract and divide operations for integers. The results of all of these are integers. Use only the add operator.
Java only !!! Please write the following method that sorts an ArrayList of numbers: public static...
Java only !!! Please write the following method that sorts an ArrayList of numbers: public static void sort(ArrayList < Integer > list) Prompts user to enter 5 ints stores them into an array list and display them in increasing order
Java Write a program that displays all the numbers from 100 to 200 that are divisible...
Java Write a program that displays all the numbers from 100 to 200 that are divisible by 5 or 6, but not both Make sure all instructions and outputs for the user are explicit
Display the shortest words in a sentence in JAVA Write a method that displays all the...
Display the shortest words in a sentence in JAVA Write a method that displays all the shortest words in a given sentence. You are not allowed to use array In the main method, ask the user to enter a sentence and call the method above to display all the shortest words in a given sentence. You must design the algorithms for both the programmer-defined method and the main method.
Please write in java: Write a recursive method toNumber that forms the integer sum of all...
Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT