-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoDimRaggedArrayUtility.java
175 lines (166 loc) · 4.7 KB
/
TwoDimRaggedArrayUtility.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.*;
import java.lang.*;
class TwoDimRaggedArrayUtility{
public static double[][] readFile(File file)
{
double[][] raggedArr=new double[6][];
String line;
int i=0;
try
{
BufferedReader br=new BufferedReader(new FileReader(file));
while((line=br.readLine())!=null)
{
String[] temp=line.split(" ");
int size=temp.length;
raggedArr[i]=new double[size];
for(int j=0;j<size;j++)
{
raggedArr[i][j]= Double.parseDouble(temp[j]);
}
i++;
}
br.close();
}
catch(Exception ep)
{
ep.printStackTrace();
}
return raggedArr;
}
public static void writeToFile(double[][] raggedArr,File file)
{
try
{
BufferedWriter br=new BufferedWriter(new FileWriter(file));
for(int i=0;i<raggedArr.length;i++)
{
for(int j=0;j<raggedArr[i].length;j++)
{
br.write(raggedArr[i][j]+" ");
}
br.newLine();
}
br.close();
}
catch(Exception ep)
{
ep.printStackTrace();
}
}
public static double getTotal(double[][] raggedArr)
{
double total=0;
for(int i=0;i<raggedArr.length;i++)
{
for(int j=0;j<raggedArr[i].length;j++)
{
total += raggedArr[i][j];
}
}
return total;
}
public static double getAverage(double[][] raggedArr)
{
double total=0;
int numOfElements=0;
for(int i=0;i<raggedArr.length;i++)
{
for(int j=0;j<raggedArr[i].length;j++)
{
total += raggedArr[i][j];
numOfElements += 1;
}
}
return (total/numOfElements);
}
public static double getRowTotal(double[][] raggedArr,int rowIndex)
{
double total=0;
for(int j=0;j<raggedArr[rowIndex].length;j++)
{
total += raggedArr[rowIndex][j];
}
return total;
}
public static double getColumnTotal(double[][] raggedArr,int colIndex)
{
double total=0;
for(int i=0;i<raggedArr.length;i++)
{
if(colIndex<=raggedArr[i].length-1)
total += raggedArr[i][colIndex];
}
return total;
}
public static double getHighestInRow(double[][] raggedArr,int rowIndex)
{
double highest=raggedArr[rowIndex][0];
for(int j=0;j<raggedArr[rowIndex].length;j++)
{
if(raggedArr[rowIndex][j]>highest)
highest= raggedArr[rowIndex][j];
}
return highest;
}
public static double getLowestInRow(double[][] raggedArr,int rowIndex)
{
double lowest=raggedArr[rowIndex][0];
for(int j=0;j<raggedArr[rowIndex].length;j++)
{
if(raggedArr[rowIndex][j]<lowest)
lowest= raggedArr[rowIndex][j];
}
return lowest;
}
public static double getHighestInColumn(double[][] raggedArr,int colIndex)
{
double highest=raggedArr[0][colIndex];
for(int i=0;i<raggedArr.length;i++)
{
if(colIndex<=raggedArr[i].length-1)
{
if(raggedArr[i][colIndex]>highest)
highest= raggedArr[i][colIndex];
}
}
return highest;
}
public static double getLowestInColumn(double[][] raggedArr,int colIndex)
{
double lowest=raggedArr[0][colIndex];
for(int i=0;i<raggedArr.length;i++)
{
if(colIndex<=raggedArr[i].length-1)
if(raggedArr[i][colIndex]<lowest)
lowest= raggedArr[i][colIndex];
}
return lowest;
}
public static double getHighestInArray(double[][] raggedArr)
{
double highest=raggedArr[0][0];
for(int i=0;i<raggedArr.length;i++)
{
for(int j=0;j<raggedArr[i].length;j++)
{
if(raggedArr[i][j]>highest)
highest= raggedArr[i][j];
}
}
return highest;
}
public static double getLowestInArray(double[][] raggedArr)
{
double lowest=raggedArr[0][0];
for(int i=0;i<raggedArr.length;i++)
{
for(int j=0;j<raggedArr[i].length;j++)
{
if(raggedArr[i][j]<lowest)
lowest= raggedArr[i][j];
}
}
return lowest;
}
}