Question

Write a Java program that gets a 4x4 array of numbers and that calculates the sum...

Write a Java program that gets a 4x4 array of numbers and that calculates the sum and average of numbers on the main diagonal, ie the diagonal that goes from the left corner down to the right corner.

Examples of resulting programs are:

Enter a 4x4 matrix row by row:

1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7

The sum of numbers in the diagonal is: 16

The average of numbers in the diagonal is: 4
Write a program that:

- Prints appropriate prompt to user.
- Loading a 4x4 matrix.
- Calculates sum and average for the main diagonal
- Prints the result with an appropriate user prompt.
Hint:
Store the matrix in a 4x4 double array.

Homework Answers

Answer #1

Ans

code:-

import java.util.*;

class MyClass {

public static void main(String[ ] args) {

//create object for taking input

Scanner sc = new Scanner(System.in);

//creating double array of 4 by 4

double a[][]=new double[4][4];

double sum=0.0;//initialise sum

//prompt

System.out.println("Enter matrix elements of size 4x4");

//loading matrix a

for(int i=0;i<4;i++){

for(int j=0;j<4;j++){

a[i][j]=sc.nextDouble();

if(i==j){//sum the elements on diagonal

sum=sum+a[i][j];

}

}

}//display the sum

System.out.println("The sum of numbers in the diagonal is: "+sum);

//display the average

System.out.println("The average of numbers in the diagonal is: "+(sum/4.0));

  

}

}

code with output:-

.

...

..

If any doubt ask in the comments.

Know the answer?
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
Similar Questions
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds)
Step 1: Write a Java program that finds the sum of 10 numbers entered by the...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the user. Step 2: Modify the previous program so that it asks the user how many numbers they have, inputs that many numbers, and finally outputs the average of their numbers. The messages to the user should be clear throughout.
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
In Java, Write a small program that gets some numbers as command line arguments and finds...
In Java, Write a small program that gets some numbers as command line arguments and finds the minimum of those numbers. You can assume that the user will provide some command line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 8.25.
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Define a recursive rangeSum(from, to) method that calculates and prints the sum of numbers between the...
Define a recursive rangeSum(from, to) method that calculates and prints the sum of numbers between the two values inclusive. IN JAVA. OUTPUT rangeSum(1,5) -> 15 rangeSum(1,5) -> 1+2+3+4+5=15 rangeSum(-3,4) -> 4 rangeSum(-3,4) -> -3+-2+-1+0+1+2+3+4=4 rangeSum(-5, 7) -> 13 rangeSum(-5, 7) -> -5+-4+-3+-2+-1+0+1+2+3=-9 please write main method as well as output for three above methods.
Python code Write a complete program with for loop which calculates the sum of the numbers...
Python code Write a complete program with for loop which calculates the sum of the numbers from 1 to 100
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Write a program in MASM (32 bit) that calculates the sum of all odd numbers in...
Write a program in MASM (32 bit) that calculates the sum of all odd numbers in the Fibonacci sequence between 0 and 1,000,000. ***Please Complete in MASM 32 bit!!***
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT