Results 1 to 19 of 19
Thread: finding average in 2d arrays
- 02-24-2011, 01:15 AM #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
- 02-24-2011, 01:23 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Great stuff, and your question/problem is?
- 02-24-2011, 01:50 AM #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; }
- 02-24-2011, 02:02 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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}};[/CODE]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 }
- 02-24-2011, 02:11 AM #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...
- 02-24-2011, 02:13 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
What value is temp.length?
- 02-24-2011, 02:15 AM #7
- 02-24-2011, 02:16 AM #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.
- 02-24-2011, 02:47 AM #9
Changed what to 12? Did you solve your problem?
- 02-24-2011, 03:32 AM #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
- 02-24-2011, 03:33 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
That method will not give you an average
- 02-24-2011, 03:45 AM #12
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
it didn't give me a right num :)
- 02-24-2011, 03: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?"
- 02-24-2011, 04:03 AM #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
- 02-24-2011, 04:14 AM #15
Member
- Join Date
- Jan 2011
- Posts
- 21
- Rep Power
- 0
now im getting the right outputJava 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 72can 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...Java Code:sum += temp[row][0];
thankz :)
- 02-24-2011, 04:20 AM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 02-24-2011, 04:21 AM #17
This still does not produce the correct answer. All you code does now is tally the first column row number of times.Java 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.
- 02-24-2011, 04:31 AM #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 :)
- 02-24-2011, 04:32 AM #19
Member
- 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: 02-03-2011, 10:41 PM -
need some help finding average on java
By jtw0812 in forum New To JavaReplies: 2Last Post: 09-29-2010, 04:26 AM -
average
By anjigadu in forum New To JavaReplies: 4Last Post: 09-19-2010, 09:52 PM -
Need help getting average
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 09-12-2010, 11:59 PM -
help...! about reading a text file and finding their average
By nemesis in forum New To JavaReplies: 20Last Post: 10-20-2008, 11:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks