Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help finding and printing min and max in a 2D array

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.


     
    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
     
     
    	}
    Last edited by new2.java; February 6th, 2013 at 04:35 PM. Reason: request on reply - I am clueless.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.


    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.
    Last edited by new2.java; February 6th, 2013 at 05:16 PM. Reason: 0>me

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help, Building List & finding min and max
    By Margaret_Girl87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 30th, 2011, 07:15 PM
  2. Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)
    By TheWhopper858 in forum Collections and Generics
    Replies: 1
    Last Post: November 6th, 2011, 08:50 PM
  3. Replies: 5
    Last Post: May 24th, 2011, 10:39 AM
  4. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 08:57 PM
  5. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM

Tags for this Thread