Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by Assignment1 ( 6 years ago )
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.Math;
public class Main {
/**
* Print the lowest elevation value and the number of times it is found
* @param data A two-dimensional array
* @param row The number of row
* @param col The number of column
*/
public static int[] findAndCountLowest(int [] data, int row, int col) {
int min = data[0]; // range 15000 - 99000
int count = 0;
for(int i = 0; i < row*col; i++)
{
if(min == data[i])
{
count++;
}
else{
break;
}
}
return new int[] {min, count};
}
public static int[] findPopularElevation(int [] data, int row, int col) {
// Store the max index
int maxIndex = 1, commonElevation = data[0];
int saveIndex = 0;
// Store the times occurs in the data
for(int i = 0; i < row*col; i++)
{
if(commonElevation != data[i])
{
saveIndex = i - saveIndex;
if(saveIndex > maxIndex)
{
maxIndex = saveIndex;
commonElevation = data[i];
saveIndex = i;
}
}
}
return new int[] {commonElevation,maxIndex};
}
public static void main(String[] args) {
long startTime, elapsedTime;
int row = 0, col = 0, indexRadius = 0;
int[] data = null;
try {
Scanner fileInput = new Scanner(new File("src/ELEVATIONS.TXT"));
//Read the number of Rows, Columns and Excluded Radius first
row = fileInput.nextInt();
col = fileInput.nextInt();
indexRadius = fileInput.nextInt();
//Read and store the elevations to array
data = new int[row*col];
for(int i = 0; i < row*col; i++){
data[i] = fileInput.nextInt();
}
Arrays.sort(data);
fileInput.close();
}catch(IOException ex) {
System.out.println("Error Reading file " + ex.getMessage());
}
//Time start to count
startTime = System.nanoTime();
/**
* Question 1: Print the lowest elevation value and the number of times it is found in the complete
* data set.
*/
int[] theLowest = findAndCountLowest(data,row,col);
System.out.println("The lowest peak is " + theLowest[0] + ". It occurs " + theLowest[1] + " times in the map.");
/**
* Question 4: Print the most common elevation in the data set.
*/
int[] commonElevation = findPopularElevation(data,row,col);
System.out.println("The most common height in the terrain is " + commonElevation[0] + ", it occurs " + commonElevation[1] + " times.");
//Time end and calculation
elapsedTime = System.nanoTime() - startTime;
System.out.println( "\nTotal time lost: " + elapsedTime/1000000 +" millisecond");
}
}
Revise this Paste
Parent: 110451