Need help finding and printing min and max in a 2D array
Hey, guys I am fairly new to java so help would more than appreciated. I have initialized and filled a 2D array and also two 1D arrays. I have them running for the time being as I can print them to my output. My next problem here is finding and printing the maximum value in the 2D array. The 2D array is named precipitationAmount. This array consists of 5 rows and 12 columns. ( 5 years and 12 months for each year). This is my expected output: The maximum rainfall of 6.44 occurred in May of 1995 I know this is not right, but i think i am somewhat close? maybe not. Help would be greatly appreciated. Sorry if this is unclear or hard to deal with as I am new to Java and this website. thanks.
PS. I am using the program called eclipse to compile and run my code.
Not sure if there is a cap for characters in posting, I'll try posting the whole thing. There is also a read in IO file that fills the array.
Code java:
public class Precipitation
{
private final static int MONTHS = 12;
private final static int YEARS = 5;
private final static int STARTYEAR = 1995;
private final String[] monthLabel = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double [] [] precipitationAmount;
double [] yearTotal;
double [] monthlyAverage;
//DECLARE ARRAYS HERE FOR TASK #1
public Precipitation()
{
precipitationAmount = new double [5] [12];
yearTotal = new double [12];
monthlyAverage = new double [12];
//CREATE THE ARRAYS OF THE CORRECT SIZE HERE FOR TASK #1
//(USE THE CLASS CONSTANTS)
}
public void readFile(Scanner infile) throws IOException
{
for(int row = 0; row< precipitationAmount.length; row++)
{
for(int col = 0 ; col<this.precipitationAmount[row].length;col++)
{
double value = infile.nextDouble();
System.out.print("[ row:"+row+"col:"+col+" value: "+value+" ] ");
precipitationAmount [row] [col] = value;
}
System.out.println();
}
//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
//STORE IT INTO THE 2-D ARRAY FOR TASK #2
infile.close();
calculateMonthlyAverage();
calculateYearTotal();
}
private void calculateMonthlyAverage()
{
double month = 0;
for(int col = 0; col < MONTHS; col++)
{
for(int row = 0 ; row < YEARS; row++)
{
month = month + precipitationAmount[row][col];
}
monthlyAverage[col] = month /5;
month = 0;
System.out.println(monthlyAverage[col]);
}
}
//CREATE A LOOPING STRUCTURE TO CALCULATE THE MONTHLY AVERAGE
//FOR EACH COLUMN AND STORE IT INTO THE MONTHLY AVERAGE ARRAY
//FOR TASK #2
private void calculateYearTotal()
{
double year = 0;
for(int row = 0; row < YEARS; row++)
{
for(int col = 0 ; col < MONTHS; col++)
{
year = year + precipitationAmount[row][col];
}
yearTotal[row] = year;
year = 0;
System.out.println(yearTotal[row]);
}
}
//CREATE A LOOPING STRUCTURE TO CALCULATE THE YEAR TOTAL
//FOR EACH ROW AND STORE IT INTO THE YEAR TOTAL ARRAY
//FOR TASK #2
public String findMax()
{
String month = "";
double max = 0;
double value = 0;
for(int row = 0; row < precipitationAmount.length; row++)
{
for(int col = 0; col < precipitationAmount[row].length; col++);
{
if(max < value)
{
max = value;
monthLabel[row]=month;
System.out.println("The maximum rainfall of"+max+ "occurred in"+month+"of"+STARTYEAR);
}
}
}
//TASK #3A
//SEARCH THE ARRAY FOR THE LARGEST AMOUNT OF PRECIPITAION
//SAVE THE AMOUNT, MONTH, AND YEAR.
return "The maximum rainfall";
//RETURN A STRING CONTAINING INFORMATION ABOUT THE MAXIMUM PRECIPITATION
}
Re: Need help finding and printing min and max in a 2D array
Can you explain what the posted code does? I don't see where it is looking at any of the elements in any array.
Do you know how an assignment statement works?
x = y;
The above takes the value in y and stores it in x.
The value of the expression/variable on the rightside of the = goes into the variable on the left side.
Some of the assignment statements in the code (those with the = operator) look backwards.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Finding and printing maximum value in 2D array..right section? :\
Hey, guys I am fairly new to java so help would more than appreciated. I have initialized and filled a 2D array and also two 1D arrays. I have them running for the time being as I can print them to my output. My next problem here is finding and printing the maximum value in the 2D array. The 2D array is named precipitationAmount. This array consists of 5 rows and 12 columns. ( 5 years and 12 months for each year). This is my expected output: The maximum rainfall of 6.44 occurred in May of 1995 I know this is not right, but i think i am somewhat close? maybe not. Help would be greatly appreciated. Sorry if this is unclear or hard to deal with as I am new to Java and this website. thanks.
PS. I am using the program called eclipse to compile and run my code.
Not sure if there is a cap for characters in posting, I'll try posting the whole thing. There is also a read in IO file that fills the array.
Code java:
public class Precipitation
{
private final static int MONTHS = 12;
private final static int YEARS = 5;
private final static int STARTYEAR = 1995;
private final String[] monthLabel = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double [] [] precipitationAmount;
double [] yearTotal;
double [] monthlyAverage;
//DECLARE ARRAYS HERE FOR TASK #1
public Precipitation()
{
precipitationAmount = new double [5] [12];
yearTotal = new double [12];
monthlyAverage = new double [12];
//CREATE THE ARRAYS OF THE CORRECT SIZE HERE FOR TASK #1
//(USE THE CLASS CONSTANTS)
}
public void readFile(Scanner infile) throws IOException
{
for(int row = 0; row< precipitationAmount.length; row++)
{
for(int col = 0 ; col<this.precipitationAmount[row].length;col++)
{
double value = infile.nextDouble();
System.out.print("[ row:"+row+"col:"+col+" value: "+value+" ] ");
precipitationAmount [row] [col] = value;
}
System.out.println();
}
//CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND
//STORE IT INTO THE 2-D ARRAY FOR TASK #2
infile.close();
calculateMonthlyAverage();
calculateYearTotal();
}
private void calculateMonthlyAverage()
{
double month = 0;
for(int col = 0; col < MONTHS; col++)
{
for(int row = 0 ; row < YEARS; row++)
{
month = month + precipitationAmount[row][col];
}
monthlyAverage[col] = month /5;
month = 0;
System.out.println(monthlyAverage[col]);
}
}
//CREATE A LOOPING STRUCTURE TO CALCULATE THE MONTHLY AVERAGE
//FOR EACH COLUMN AND STORE IT INTO THE MONTHLY AVERAGE ARRAY
//FOR TASK #2
private void calculateYearTotal()
{
double year = 0;
for(int row = 0; row < YEARS; row++)
{
for(int col = 0 ; col < MONTHS; col++)
{
year = year + precipitationAmount[row][col];
}
yearTotal[row] = year;
year = 0;
System.out.println(yearTotal[row]);
}
}
//CREATE A LOOPING STRUCTURE TO CALCULATE THE YEAR TOTAL
//FOR EACH ROW AND STORE IT INTO THE YEAR TOTAL ARRAY
//FOR TASK #2
public String findMax()
{
String month = "";
double max = 0;
double value = 0;
for(int row = 0; row < precipitationAmount.length; row++)
{
for(int col = 0; col < precipitationAmount[row].length; col++);
{
if(max < value)
{
max = value;
monthLabel[row]=month;
System.out.println("The maximum rainfall of"+max+ "occurred in"+month+"of"+STARTYEAR);
}
}
}
//TASK #3A
//SEARCH THE ARRAY FOR THE LARGEST AMOUNT OF PRECIPITAION
//SAVE THE AMOUNT, MONTH, AND YEAR.
return "The maximum rainfall";
//RETURN A STRING CONTAINING INFORMATION ABOUT THE MAXIMUM PRECIPITATION
}
public String findMin()
{
//TASK #3B
//SEARCH THE ARRAY FOR THE SMALLEST AMOUNT OF PRECIPITAION
//SAVE THE AMOUNT, MONTH, AND YEAR.
return "The minimum rainfall";
//RETURN A STRING CONTAINING INFORMATION ABOUT THE MINIMUM PRECIPITATION
}
public void printTable(PrintWriter outfile)
{
//System.out.print("Month")
//OUTPUT THE TABLE USING NICELY FORMATTED NUMBERS #.##
//AND NEAT COLUMNS FOR TASK #4
outfile.close();
}
public static void main(String[] args) throws IOException
{
Precipitation wetStuff = new Precipitation();
File file = new File("precip.dat");
Scanner infile = new Scanner(file);
PrintWriter outfile = new PrintWriter(new FileWriter("precip.out"));
wetStuff.readFile(infile);
wetStuff.printTable(outfile);
}
}
Sorry for that. stupid of me not to include that.
Re: Need help finding and printing min and max in a 2D array
The posted code does not compile.
When it compiles it will need a main() method to be able to execute it.