Question
use java please

Exercise 3 Sum the Major Diagonal of a Matrix Complete exercise 8.2 on page 308 of the textbook. Note there are some modifica
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package Ex3SumDiagonal;

import java.util.Random;

public class Ex3SumDiagonal {

    public static double sumMajorDiagonal(double[][] m) {
        double sum = 0;
        for (int i = 0; i < m.length; i++) {
            sum += m[i][i];
        }
        return sum;
    }

    public static void main(String[] args) {
        Random random = new Random();
        final int SIZE = 4;
        double[][] m = new double[SIZE][SIZE];
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                m[i][j] = 1 + random.nextDouble() * 19;
            }
        }

        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                System.out.printf("%.2f ", m[i][j]);
            }
            System.out.println();
        }

        System.out.printf("\nThe sum of the major diagonal is %.2f\n", sumMajorDiagonal(m));
    }
}

13.95 13.15 9.50 6.25 19.46 1.55 11.62 14.14 17.53 12.66 2.80 8.39 7.82 13.35 4.60 3.72 The sum of the major diagonal is 22.0

Add a comment
Know the answer?
Add Answer to:
use java please Exercise 3 Sum the Major Diagonal of a Matrix Complete exercise 8.2 on...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT