Java Given a set of decimal values entered by the user in table format consistin
ID: 3788724 • Letter: J
Question
Java
Given a set of decimal values entered by the user in table format consisting of 3 rows and 4 columns, print the table, the sum of each column, the sum of the columns, the average of the columns and the value and index of the highest and lowest values in the table.
Sample Program running:
Enter 4 columns of decimal values
1.1 1.2 1.3 1.4
Enter 4 columns of decimal values
2.1 2.2 2.3 2.4
Enter 4 columns of decimal values
3.1 3.2 3.3 3.4
The Matrix:
1.1 1.2 1.3 1.4
2.1 2.2 2.3 2.4
3.1 3.2 3.3 3.4
The sum of the columns
6.3 6.6 6.9 7.2
The average of the columns
2.1 2.2 2.3 2.4
The highest value in the table 3.4 is at row 2 and col 3
The lowest value in the table 1.1 is at row 0 and col 0
Explanation / Answer
ANSWER:
import java.util.Scanner;
import java.io.*;
Public class Calculate{
Scanner sc;
Double marix= new Double[3][4];
void Create(){
for(int i=0;i<3;i++){
System.out.println("Enter 4 columns of decimal values");
for(int j=0;j<4;j++){
matrix[i][j]=sc.nextDouble();
}
}
}
Void DispalyMatrix(){
System.out.println(" The matrix:");
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
System.out.println(" "+ matrix[i][j]);
}
System.out.println();
}
}
Sum(){
int value=0;
Double sum[]= new Sum[4];
for(int j=0;j<4;j++){
for(int i=0;i<3;i++){
value=value+matrix[i][j];
}
sum[j]=value;
value=0;
}
System.out.println("The Sum of columns:");
for(int i=0;i<4;i++){
System.out.println(" " + sum[i]);
}
}
Average(){
int value=0;
Double Average[]= new Average[4];
for(int j=0;j<4;j++){
for(int i=0;i<3;i++){
value=value+matrix[i][j];
}
Average[j]=value/3;
value=0;
}
System.out.println("The Sum of columns:");
for(int i=0;i<4;i++){
System.out.println(" " + Average[i]);
}
}
minmax(){
int col,row;
Double max, min;
max=matrxi[0][0];
min=matrxi[0][0];
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
if(matrix[i][j]>max){
max=matrix[i][j]
col=j
row=i;
}
if(matrix[i][j]<min){
min=matrix[i][j]
col=j
row=i;
}
}
}
System.out.println("The highest value in the table" + max + "is at row" + i + "and col" + j);
System.out.println("The lowest value in the table" + min + "is at row" + i + "and col" + j);
}
public static void main(){
Create();
DispalyMatrix();
Sum();
Average();
minmax();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.