# Thread: finding average in 2d arrays

1. Member
Join Date
Jan 2011
Posts
21
Rep Power
0

## 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. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
Great stuff, and your question/problem is?

3. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
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. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
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. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
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. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
What value is temp.length?

7. Originally Posted by the beginner
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. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
i changed that to 12 :) i forgot to changed it when i posted it.

9. Changed what to 12? Did you solve your problem?

10. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
[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. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
That method will not give you an average

12. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
it didn't give me a right num :)

13. 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. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
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. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
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. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
7
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. 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. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
@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. Member
Join Date
Jan 2011
Posts
21
Rep Power
0
and i appreciate y'all

#### Posting Permissions

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