Results 1 to 19 of 19
Thread: finding average in 2d arrays
 02242011, 02:15 AM #1Member
 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
 02242011, 02:23 AM #2Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
Great stuff, and your question/problem is?
 02242011, 02:50 AM #3Member
 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; }
 02242011, 03:02 AM #4Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
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 }
 02242011, 03:11 AM #5Member
 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...
 02242011, 03:13 AM #6Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
What value is temp.length?
 02242011, 03:15 AM #7
 02242011, 03:16 AM #8Member
 Join Date
 Jan 2011
 Posts
 21
 Rep Power
 0
i changed that to 12 :) i forgot to changed it when i posted it.
 02242011, 03:47 AM #9
Changed what to 12? Did you solve your problem?
 02242011, 04:32 AM #10Member
 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
 02242011, 04:33 AM #11Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
That method will not give you an average
 02242011, 04:45 AM #12Member
 Join Date
 Jan 2011
 Posts
 21
 Rep Power
 0
it didn't give me a right num :)
 02242011, 04:53 AM #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?"
 02242011, 05:03 AM #14Member
 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
 02242011, 05:14 AM #15Member
 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; }
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];
thankz :)
 02242011, 05:20 AM #16Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 5
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.
 02242011, 05:21 AM #17Java Code:
sum += temp[row][0];
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.
 02242011, 05:31 AM #18Member
 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 :)
 02242011, 05:32 AM #19Member
 Join Date
 Jan 2011
 Posts
 21
 Rep Power
 0
Similar Threads

Sending Arrays into methods then dividing for an average.
By freebirdcal in forum New To JavaReplies: 8Last Post: 02032011, 11:41 PM 
need some help finding average on java
By jtw0812 in forum New To JavaReplies: 2Last Post: 09292010, 04:26 AM 
average
By anjigadu in forum New To JavaReplies: 4Last Post: 09192010, 09:52 PM 
Need help getting average
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 09122010, 11:59 PM 
help...! about reading a text file and finding their average
By nemesis in forum New To JavaReplies: 20Last Post: 10202008, 11:02 AM
Bookmarks