This repository has been archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoreStats.java
54 lines (51 loc) · 1.52 KB
/
MoreStats.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class MoreStats {
public static void main(String[]args){
final int N = Integer.parseInt(args[0]);
int[][] square = new int[N][N];
int k=1, sum=0, min=Integer.parseInt(args[1]), max=min;
double average=0;
int colsum=0, rowsum=0, diagsum=0;
if(square.length>0){
System.out.println("Please input "+(N*N)+" values,");
System.out.println("to create a "+N+" by "+N+" square:");
for(int i=0; i<square.length; i++){
for(int j=0; j<square.length; j++){
square[i][j]=Integer.parseInt(args[k]);
sum = sum+square[i][j];
if(square[i][j]<min)
min=square[i][j];
else if(square[i][j]>max)
max=square[i][j];
System.out.print(square[i][j]);
k++;
}
System.out.println();
}
average=sum/(N*N);
System.out.printf("Sum: %2d Average: %2.1f Min: %2d Max: %2d\n",sum, average, min, max);
//Calculating the sum of every column and row
for(int j=0; j<square.length; j++){
for(int i=0; i<square.length; i++){
colsum=colsum+square[i][j];
}
System.out.println("Sum of column-"+j+": "+colsum);
colsum=0;
}
for(int i=0; i<square.length; i++){
for(int j=0; j<square.length; j++){
rowsum=rowsum+square[i][j];
}
System.out.println("Sum of row-"+i+": "+rowsum);
rowsum=0;
}
//Calculating the sum of diagonals
for(int i=0; i<square.length; i++){
for(int j=0; j<square.length; j++){
if(i==j || (i+j)==(N-1))
diagsum=diagsum+square[i][j];
}
}
System.out.println("Sum of diagonals: "+diagsum);
}
}
}