-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
58 lines (49 loc) · 2.06 KB
/
main.java
File metadata and controls
58 lines (49 loc) · 2.06 KB
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
package src;
import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
public class main{
public static void main(String [] args)
{
String inputFile = args[0]; String outputFile = args[1];
float[][] grid = null; float[][] classified = null;//grid for inputs.
int cols, rows, basins = 0;
try{
FileReader fIn = new FileReader(inputFile);
Scanner dataFile = new Scanner(fIn);
rows = dataFile.nextInt(); cols = dataFile.nextInt();
grid = new float[rows][cols]; classified = new float[rows][cols];//instantiate 2d array.
for (int i = 0; i <= rows; i++){
for (int j = 0; j <= cols; j++){
grid[i][j] = dataFile.nextFloat();
if (i == 0 || i == rows)
basins++;
else if (j ==0 || j == cols){
basins++;
}
}
}
for (int i = 1; i <= rows-1; i++){
for (int j = 1; j <= cols-1; j++){
if ((grid[i+1][j+1] - grid[i][j] >= 0.01) && (grid[i-1][j+1] - grid[i][j] >= 0.01) && (grid[i+1][j] - grid[i][j] >= 0.01)
&& (grid[i][j+1] - grid[i-1][j-1] >= 0.01) && (grid[i-1][j] - grid[i][j-1] >= 0.01) && (grid[i+1][j-1] - grid[i][j] >= 0.01)){
basins++;
classified[i][j] = grid[i][j];
}
else classified[i][j] = 0;
}
}
File fOut = new File(outputFile);
FileWriter output = new FileWriter(fOut);
output.write(Integer.toString(basins) + "\n");
for (int i = 1; i <= rows-1; i++){
for (int j = 1; j <= cols-1; j++){
if (classified[i][j] != 0){
output.write(Integer.toString(i) + " " + Integer.toString(j)+ "\n");
}
}
}
dataFile.close();
} catch (Exception e){ System.err.println(e);}
}
}