Results 1 to 19 of 19
  1. #1
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default finding average in 2d arrays

    im trying to find the average of the high temprature and return it..

    Java Code:
    import java.util.*;
    
    public class Chap9Ex12p589
    {
    	static Scanner console  = new Scanner(System.in);
    	
    	public static void main(String[] args)
    	{
    		int[][] temperature = {{50,16},{47,-2},{65,35},{68,41},{73,50},
    										{81,66},{93,73},{101,82},{90,75},
    										{75,60},{67,53},{56,38}};
    										
    		printTemp(temperature);						
    									
    		avgHighTemp(temperature);							
    	}//end main
    	
    	//method
    	public static void printTemp(int[][] temp)
    	{
    		for (int high = 0; high < temp.length; high++)
    		{
    			for (int low = 0; low < temp[high].length; low++)
    				System.out.printf("%7d", temp[high][low]);
    				
    			System.out.println();
    		}
    	}
    	
    	public static int avgHighTemp(int[][] temp)
    	{
    		int sum =0;
    	 	double avg= 0;
    		
    		for (int high = 0; high < temp[0].length; high++)
    		{
    			sum = 0;
    			for (int low = 0; low < temp.length; low++)
    				sum += temp[high][low];
    
    		}
    
                              avg = sum / temp.length
    		System.out.println("The average of the high temperature is " + String.format("%.2f", avg));
    
    		return avg;
    	}
    	
    }//end class

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Great stuff, and your question/problem is?

  3. #3
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    in this method im trying to find the average of the high temperature. i compile it and getting an error

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at Chap9Ex12p589.avgHighTemp(Chap9Ex12p589.java:43)
    at Chap9Ex12p589.main(Chap9Ex12p589.java:18)

    Java Code:
    	public static int avgHighTemp(int[][] temp)
    	{
    		int sum =0;
    	 	double avg= 0;
    		
    		for (int high = 0; high < temp[0].length; high++)
    		{
    			sum = 0;
    			for (int low = 0; low < temp.length; low++)
    				sum += temp[high][low]; [U]//this might be where my problem is[/U]
    
    		}
    
                              avg = sum / temp.length
    		System.out.println("The average of the high temperature is " +  avg);
    
    		return avg;
    	}

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    That certainly is a problem, not your only one though. How many columns does your 2d array have? Why are you getting a number 2 with the ArrayOutOfBounds exception?. Try to answer these questions.

    Java Code:
    int[][] temperature = {{50,16},{47,-2},{65,35},{68,41},{73,50},
    {81,66},{93,73},{101,82},{90,75},{75,60},{67,53},{56,38}};
    Java Code:
    for (int high = 0; high < temp[0].length; high++)
    		{
    			sum = 0;
    			for (int low = 0;[COLOR="DarkOrange"] low < temp.length[/COLOR]; low++)
    				sum += temp[high][low]; //this might be where my problem is
    
    		}
    [/CODE]

  5. #5
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    there is one inside the main and the avghightemp method.

    i have 12 rows and 2 columns. first column is the high temp and the second is low temp...

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    What value is temp.length?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by the beginner View Post
    i have 12 rows and 2 columns.
    But you are iterating over the array columns first and then rows. Perhaps if you renamed the high and low variables as row and column it might by more evident what your problem is.

  8. #8
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    i changed that to 12 :) i forgot to changed it when i posted it.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Changed what to 12? Did you solve your problem?

  10. #10
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    [code]
    public static int avgHighTemp(int[][] temp)
    {
    int sum;
    int avg= 0;

    for (int col = 0; col < temp[0].length; col++)
    {
    sum = 0;
    for (int row = 0; row < temp.length; row++)
    sum += temp[row][col];

    avg = sum / temp.length;
    }


    return avg;
    }

    [\code]

    thanks for the help al_Marshy_1981 and junky

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    That method will not give you an average

  12. #12
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    it didn't give me a right num :)

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    So work out why it doesn't. Try adding a bunch of print statements at various points so you can see what values your variables have. Hopefully one (or more) of them will jump out at you and you say "Why is it X when it should be Y?"

  14. #14
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    i printed out my sum and it prints both of sum of my columns.. the avg pickin up the last sum which is my low temp(2nd col) instead the first col that was why i have the wrong avg..

    thanks.. ill work on it

  15. #15
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    Java Code:
    	public static int avgHighTemp(int[][] temp)
    	{
    		int sum = 0;
    	 	int avg= 0;
    		
    		for (int col = 0; col < temp[0].length; col++)
    		{
    			sum = 0;
    			for (int row = 0; row < temp.length; row++)
    					sum += temp[row][0];
    					
    					System.out.println(sum);
    		}
    		
    		avg = sum / temp.length;
    		return avg;
    	}
    now im getting the right output

    Java Code:
         50     16
         47     -2
         65     35
         68     41
         73     50
         81     66
         93     73
        101     82
         90     75
         75     60
         67     53
         56     38
    866
    866      [U]//IT WAS 587 THE SUM OF MY 2ND COLUMN[/U]
    The average of the high temperature is 72
    Java Code:
    sum += temp[row][0];
    can u tell me why the [0] has zero instead of [col] ??? i just didnt get it, if u dont mind... it's ok if u dont feel like it...

    thankz :)

  16. #16
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Because highest temperatures were stored in column 1 of your array, but array access starts its count from 0. So 0 = column 1. The way you accessed the array was unconventional though.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    sum += temp[row][0];
    This still does not produce the correct answer. All you code does now is tally the first column row number of times.

    Oops!

    Misunderstood. I thought you were trying to average all the temperatures. If all you want is the high temps in the left column then you don't need nested loops. A single loop is all you need.

  18. #18
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

    Default

    @al: thanks and how can i make it simple?

    @junky: it did give me the right answer and all i need to sum up is the first col

    my code is not that professional... that is why im here for additional lecture :)

  19. #19
    the beginner is offline Member
    Join Date
    Jan 2011
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Sending Arrays into methods then dividing for an average.
    By freebirdcal in forum New To Java
    Replies: 8
    Last Post: 02-03-2011, 10:41 PM
  2. need some help finding average on java
    By jtw0812 in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 04:26 AM
  3. average
    By anjigadu in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 09:52 PM
  4. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-12-2010, 11:59 PM
  5. Replies: 20
    Last Post: 10-20-2008, 11:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •